SpriteHand
Module Border
  Blend and Silverlight with VS Dev Web Server
Module Border
Location: BlogsAndy'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

web design company,web designer,web design India,website design,web design    By Anonymous on 1/29/2009 1:19 AM
Your information is good.....
web design company, web designer, web design India,website design,web design


Title:
Comment:
Add Comment   Cancel 
Module Border Module Border
Module Border
  Subscribe
Module Border
RSS   Twitter
Module Border Module Border
Module Border
  Diversions
Module Border

TALKING RAGDOLL
This Windows Phone 7 App was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine. It gets interesting when you import your friends photos and have your way with them!

MORE INFO



DROPPYPOP
This Windows Phone 7 game was created using Silverlight, the  Physics Helper Library,  and the Farseer Physics Engine.
DEMO

MORE INFO



BOSS LAUNCH
This physics game won first place in the Server Quest Contest. Created using Silverlight 2, the Physics Helper Library,  and the Farseer Physics Engine.
PLAY IT

MORE INFO



DESTROY ALL INVADERS
A scrolling shooter game where the objective is to destroy the invading UFO's flying over a neighborhood of your choosing. Imagery provided by Microsoft Virtual Earth. Created using Silverlight 2.
PLAY IT

INFO AND CODE



PHYSICS HELPER DEMOS
These demos were created for the Physics Helper Library, which makes it easy to create physics games and simulations using Expression Blend, Silverlight, and the Farseer Physics Engine.
PLAY IT

INFO AND CODE



HOOK SHOT
This little basketball game took first place in the TeamZoneSports Silverlight Contest. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SORT THE FOOBARS
A game where you need to sort the good foobars from the bad ones. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



POLYGON PHYSICS DEMO
A demo showing polygon physics where the user draws physics objects with the mouse. Created using Silverlight 2 and the Farseer Physics engine.
PLAY IT

MORE INFO



SILVERLIGHT ROCKS!
Destroy the asteroids before they destroy your ship! Created using Silverlight 2.
PLAY IT

INFO AND CODE



FISH GAME
A simple game of harpoon-the-fish. Written using the AJAX Sprite Toolkit.
PLAY IT

INFO AND CODE

Module Border Module Border
Module Border
  Search_Blog
Module Border
Module Border Module Border
Module Border
  Blog_Archive
Module Border
Module Border Module Border
Copyright (c) 2013 andy.beaulieu.com - Login