Manipulating Service References in Silverlight 2

When developing a Silverlight 2 project that uses WCF service references or ADO.NET Data Services Astoria references, and you want to push the builds to a test server, there are a few changes that need to be made so the references point to the test server and not the dev box.

Shawn Wildermuth has a great post on this topic that explains the problem and also explains that when dealing with WCF service references, the ServiceReference.ClientConfig file can either be simply edited or a better option is to use compilation symbols to make the changes at compile time. For example, Shawn points out that you can create 2 endpoints in code in the ServiceReference.ClientConfig file in the Silverlight project: 1 for dev box and 1 for the test server. Then, in code the service can be instantiated using the following code (from Shawn’s post):

#if DEBUG  
MyTestServiceClient svc = new MyTestServiceClient("TestEndpoint");  
#else  
MyTestServiceClient svc = new MyTestServiceClient("RealEndpoint");  
#endif

This same technique can be adapted for Astoria by flipping the service reference’s context object’s instantiation as shown below:

#if DEBUG  
protected MyEntities Context  
{
get  
{
if (_context  null)  
_context = new MyEntities(  
new Uri("MyService.svc", UriKind.Relative));  
return _context;  
}
}
#else  
protected MyEntities Context  
{
get  
{
if (_context  null)  
_context = new MyEntities(  
new Uri("http://testbox/foo/MyService.svc";,  
UriKind. Absolute));  
return _context;  
}
#endif

Now simply choose the compilation options for when building and you are all set! Quite simple and effective.

Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders

Tags:
Silverlight