Thursday, June 18, 2009

Dynamic WCF Connection with Silverlight

When you are using Silverlight sometimes the WCF Web service you are connecting to can be different on each deployment. To overcome the hardcoding proxy issue you can hand code your connection.

Here I have a static class that i access on every call to the WCF web service.

this is how I use this to communicate with WCF webservice

ActionServiceClient proxy = ServiceUtil.GetActionServiceClient();
proxy.YourMethodCompleted += new EventHandler(proxy_GetPositionListCompleted);
proxy.GetPositionListAsync(request);



public static ActionService.ActionServiceClient GetActionServiceClient()
{
BasicHttpBinding binding = new BasicHttpBinding(System.Windows.Browser.HtmlPage.Document.DocumentUri.Scheme.ToLower() == "https" ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
binding.SendTimeout = new TimeSpan(0, 2, 0);
binding.ReceiveTimeout = new TimeSpan(0, 2, 0);
return new ActionService.ActionServiceClient(binding, new EndpointAddress(
new Uri(Application.Current.Resources["ActionServiceURL"].ToString())));
}

The only thing of note is the endpoint address, its added as a resource to the silverlight application in the init parameters in the ASPX page that launches silverlight. I will explain in another blog how to use init parameters in silverlight.