 |
|
|
|
Blend and Silverlight with VS Dev Web Server
|
|
|
 |
|
Location: Blogs Andy's Blog |
|
| Posted by: host |
1/5/2009 8:56 PM |
Recently I was working with a designer on a Silverlight project that accessed a WCF web service. We discovered that the project would work fine when launched from Visual Studio, but when launched from Expression Blend, it would fail on web service calls. As a coder, I generally run and debug from inside Visual Studio and only hop into Blend to do quick design, so I had never seen this issue before.
As it turns out, it's an issue with how Blend and Visual Studio interact with the Cassini Development Web Server. If you've done much Web Service development with Silverlight, you've probably learned that it's a good practice to set a static port value for the Development Web Service so that the port doesn't change between runs and cause errors.
To set a static port, you select Project/Properties in VS and then select the "Specific Port" option like below:

Unforunately (and here is where the problem lies), Blend does not honor this setting. So if you run your project from Blend (using F5 or the Project/Test Solution menu option), Blend will start a new instance of the Cassini Web Server on a different port, and web service calls will subsequently fail.
Fortunately, there is a pretty easy work-around for this problem. Since the WCF web services we call usually live in the same relative path as our Silverlight application, we can use the Url of the Silverlight application to compute the location of the WCF web service.
First we add a small method to get a relative resource (such as a WCF .svc file) based on the source location of our Silverlight app:
public static string GetUrlForResource(string resourcePage) { string webUrl = System.Windows.Browser.HtmlPage.Document.DocumentUri.ToString();
string containerPage = webUrl.Substring(webUrl.LastIndexOf("/") + 1);
webUrl = webUrl.Replace(containerPage, resourcePage);
return webUrl;
}
Then we can use this method to get the relative path of the WCF service (this will conveniently include the Port of the development web server), and create a manual binding to this Url:
string webServiceUrl = GetUrlForResource("MyService.svc");
System.ServiceModel.BasicHttpBinding binding = new System.ServiceModel.BasicHttpBinding();
webSvc = new MyService.MyServiceClient(binding, new System.ServiceModel.EndpointAddress(webServiceUrl));
This is actually a good practice for other reasons too, such as switching between environments like test/beta/production - which I alluded to in a previous blog post.
|
|
| Permalink |
Trackback |
Comments (2)
Add Comment
|
Re: Blend and Silverlight with VS Dev Web Server
|
By Anonymous on
1/6/2009 3:25 PM
|
Hi Andy
I actually did a similar thing in my current project. After seeing your post I desided to post my solution as an alternative to yours :)
http://laumania.net/post/Dynamically-changing-urlendpoint-for-WCF-service-in-Silverlight-20.aspx
Best regards, Mads
|
|
|
|
|
 |
|
 |
|
|