Sunday, 30 May 2010

Configurable WCF Service url in Silverlight project side

WCF service gets url and binding properties from “ServiceReferences.ClientConfig “ file in silverlight project (client side). When WCF service initialize, it gets all property from this file. There is no problem reading data from that file. But when web site get deployment url are change. So manually you have to change url of WCF services from that file. <endpoint/> tag address property hold WCF url address.

To get rid of this hassle, we can configure it to read the url from web.config file. We can do followings things.

Create a class as an example ServiceFactory. All the initialization related things which is take place in “ServiceReferences.ClientConfig” file, have to be ServiceFactory class. See the sample code.

BasicHttpBinding _BasicHttpBinding;

      EndpointAddress _EndpointAddress;

public ServiceFactory()


    _BasicHttpBinding = new System.ServiceModel.BasicHttpBinding();

            _BasicHttpBinding.MaxReceivedMessageSize = 2147483647;

            _BasicHttpBinding.MaxBufferSize = 2147483647;

            _BasicHttpBinding.OpenTimeout = new TimeSpan(0, 20, 0);

            _BasicHttpBinding.CloseTimeout = new TimeSpan(0, 20, 0);

            _BasicHttpBinding.ReceiveTimeout = new TimeSpan(0, 20, 0);

            _BasicHttpBinding.SendTimeout = new TimeSpan(0, 20, 0);           

Now when we need to make the instance of WCF service we can create a method in ServiceFactory class. So we can bind BasicHttpBinding and EndpointAddress class property within WCF service instance. Look at sample method below.

public PCBuilderServiceClient GetPCBuilderServiceClient()


            _EndpointAddress = new System.ServiceModel.EndpointAddress(ServiceUrlManager.GetValue(ConstantClass.SERVICE_SERVER_URI) + ServiceUrlManager.GetValue(ConstantClass.PC_BUILDER_SERVICE));

                _PCBuilderServiceClient = new PCBuilderServiceClient(_BasicHttpBinding, _EndpointAddress);

            return _PCBuilderServiceClient;


Here PCBuilderServiceClient is the service name. _EndpointAddress object is binding service url. There are two things one is the server url and another is the exact location and name of the WCF service file in server. So both need to cocate to get the exact location of the WCF service. Initially we have kept thos information inside IDictionary object during starting of the web site. Now just fetching the value from IDictionary by key.

No comments:

Post a Comment