views:

81

answers:

3

I have a WCF service and methods are exposed as below:

public interface IService
{
   [OperationContract]
   bool Read();
   [OperationContract]
   bool Write();
}

public class MyService : IService
{

    //Constructor

    MyService()
    {
        //Initialization
    }

    public bool Read()
    {
        //Definition
    }

    public bool Write()
    {
        //Definition
    }
}

I have a desktop based application that consumes the Web service through URL. This web service can be deployed at multiple location so user can connect to any web service by choosing a url from the combo box.

In the client application I create a Service client dynamically as shown below:

ServiceReference1.DXMyServiceClient _client = null;

_client = new DXMyServiceClient ();

_client.Endpoint.Address = new System.ServiceModel.EndpointAddress(url);

Questions While debugging I notice whenever I call any methods of web service each time the constructor of MyService is invoked ( if I am connected to the same service).

like for example when I do:

_client.Read();//MyService () constructor is called 

_client.Write();//MyService () constructor is called

The problem is I have to do all the initialization again.. like if I connecting to the database then I have to again build the connection string and all stuff..

Is this the natural behavior or I am doing something wrong?

Secondly, I want to validate user for the valid url ( of web service ). If it is connecting to the valid url or not.. I am doing that through Ping command.. What is the best approach for that!!

A: 

You should look at the ServiceBehaviorAttribute class and it's InstanceContextMode property. It controls the lifetime of your service object.

Mike Two
A: 

The problem is I have to do all the initialization again.. like if I connecting to the database then I have to again build the connection string and all stuff..

Is this the natural behavior or I am doing something wrong?

By default InstanceContextMode is set to PerSession and ConcurrencyMode is set to Single. However, if you don't use session, basically every time you call service new instance is created. This is desired behavior because it is considered more scalable. If it is a problem for you, you should implement session between subsequent calls then for every session you will get one instance of your service.

Here is a guide how to do that: Using Sessions.

empi
+1  A: 

Questions While debugging I notice whenever I call any methods of web service each time the constructor of MyService is invoked (if I am connected to the same service). The problem is I have to do all the initialization again.. like if I connecting to the database then I have to again build the connection string and all stuff..

Yes, that's the default behavior, and the recommended behavior. You should NOT rely on any state on your service side! That is generally not a good idea and can lead to a multitude of problems.

In its recommended "per-call" mode, a WCF service has a ServiceHost() class instance running, which will listen for incoming requests / messages. Each time a request comes in, a new, fresh instance of the service class (that implements your service contract) is constructed to handle the request - just like each time you hit a URL in ASP.NET, your page class is instantiated to handle the request.

Yes, of course - this means you should keep your service classes simple and lean and not do a lot of initialization / state management. Anything that needs to be persisted between service calls should be put in a persistence store, like a database, anyway.

marc_s