Search code examples
c#web-serviceswcfproxy-classeschannelfactory

When is service class initialized?


If I've created a service reference using visual studio, and the following client was generated:

public partial class MyServiceClient : System.ServiceModel.ClientBase<MyType>

The question is: when is the service class being initialized? (when is "new MyService()" called) When I do this:

MyServiceClient client = new MyServiceClient();

or perhaps only here:

client.DoStuff()?

(The reason I'm asking is because I want to know how long the service object is still alive before being garbage collected)


Solution

  • That depends on the binding I think. For the common bindings (SOAP and NetTcp) the client does not use the network at all until the first call is made. This implies that the server object is not instantiated before that because the server is not notified.

    It is usually best to use PerCall instancing and have the service object be really cheap. That way you do not need to think about this issue at all and you don't even need the answer to this question.