WCF Service calls in Silverlight
Call WCF Service in Silverlight
A very simple thing, but there is a chance for the people who are new to Silverlight application to forget this and get the 'Service Not found' exception. (I don't want to say that this is the only condition, wherein you will receive this error)
Ok, say, I have a WCF service, say MyTestService in the server and I have added this service as a service reference in the silverlight application. I named it as TestService. I need to call a method, GetCurrencies from MyTestService.
First of all, I should create an object for the WCF sevice. I will name it as testServiceClientProxy;
Now, we need to initialize it and call the web methods. This is simple and it will work without any issues while debugging. But when you move into production, BHOOM... it will fail. The exception thrown will be 'Service not found'
How do you solve this. Its easy. Add the endpoint while you initalize the object of the service
But what is CustomBinding_MyTestService and endPointAddress? You can get CustomBinding_MyTestService from the ServiceReferences.ClientConfig, which is created automatically when you add a web/WCF service to the silverlight application and the endpointAddress is the object of EndpointAddress, which comes from the class System.ServiceModel
e.g.:
Again, ApplicationHelper.GetEndpointAddress is a custom created method that gets the url as the endpoint address from the server.
Now, the final call;
Done!
A very simple thing, but there is a chance for the people who are new to Silverlight application to forget this and get the 'Service Not found' exception. (I don't want to say that this is the only condition, wherein you will receive this error)
Ok, say, I have a WCF service, say MyTestService in the server and I have added this service as a service reference in the silverlight application. I named it as TestService. I need to call a method, GetCurrencies from MyTestService.
First of all, I should create an object for the WCF sevice. I will name it as testServiceClientProxy;
Now, we need to initialize it and call the web methods. This is simple and it will work without any issues while debugging. But when you move into production, BHOOM... it will fail. The exception thrown will be 'Service not found'
How do you solve this. Its easy. Add the endpoint while you initalize the object of the service
TestService.MyTestServiceClient myTestServiceClientProxy = new TestService.MyTestServiceClient("CustomBinding_MyTestService", endpointAddress);
But what is CustomBinding_MyTestService and endPointAddress? You can get CustomBinding_MyTestService from the ServiceReferences.ClientConfig, which is created automatically when you add a web/WCF service to the silverlight application and the endpointAddress is the object of EndpointAddress, which comes from the class System.ServiceModel
e.g.:
System.ServiceModel.EndpointAddress endpointAddress = ApplicationHelper.GetEndPointAddress("../Webservices/MyTestService");
Again, ApplicationHelper.GetEndpointAddress is a custom created method that gets the url as the endpoint address from the server.
////// Get Endpoint address for the web service /// /// Web Service Url ///Endpoint address for the service url public static EndpointAddress GetEndPointAddress(string serviceRelativeUrl) { EndpointAddress endPointAddress = null; try { string completeUrl = Application.Current.Host.Source.AbsoluteUri; if (completeUrl.Contains("/")) completeUrl = completeUrl.Substring(0, completeUrl.LastIndexOf('/') + 1); Uri requestUri = new Uri(completeUrl + serviceRelativeUrl); endPointAddress = new EndpointAddress(requestUri); } catch (Exception ex) { MessageBox.Show(ex.Message); } return endPointAddress; }
Now, the final call;
System.ServiceModel.EndpointAddress endpointAddress = ApplicationHelper.GetEndPointAddress("../Webservices/MyTestService"); TestService.MyTestServiceClient myTestServiceClientProxy = new TestService.MyTestServiceClient("CustomBinding_MyTestService", endpointAddress); myTestServiceClientProxy.GetCurrenciesCompleted += OnGetCurrenciesCompleted; myTestServiceClientProxy.GetCurrenciesAsync(null);
Done!
Comments
Post a Comment