Generating Proxies for Silverlight accessing a Silverlight-Enabled WCF Service

  Generating Proxies
  I am writing this post because there just isn’t good information on generating your own proxies for Silverlight accessing a WCF Service. The problem is that svcutil.exe is the wrong tool. It will lead you to the front door of success but not let you into the building.  The point is this, I wanted to understand how to connect to a WCF service without right-mouse clicking and choosing “Add Service Reference.” Why do it?
  • First, I like knowing what "Add Service Reference" really does. It adds references to System.ServiceModel. It adds a proxy. It creates a configuration file.
  • Second, I'd like to be able to re-use the output files in other projects without having to always add a service reference.
  • Third, there's only two files to look at. If you add a service reference, a multitude of files are generated, which I think is very confusing.
  • Finally, I had some errors with Windows Phone 7 clients that went away using the proxy.

 

The real trick is to use SLSvcUtil.exe, not SvcUtil.exe.

Did you know that? Depending on whether you want to generate for Windows Phone 7 or traditional Silverlight, here is where you can find the utilities:

bullet

C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Tools\SLSvcUtil.exe

bullet

C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Tools\SlSvcUtil.exe


  The Silverlight-Enabled WCF Service we want to connect to
  Here is the project we are trying to connect to from the Silverlight project. Note that we do not want to “Add Service Reference.”

image


  Before connecting the Silverlight project to the Silverlight–enabled WCF Service, set some references
  Before actually generating the proxies and the configuration files, you need to first set some references as seen below. Notice we have System.Runtime.Serialization and System.ServiceModel. image

  Start the Visual Studio Command Prompt as administrator MyImage Here is the command we are going to run, assuming SLSVCUTIL is in your path.  slsvcutil https://localhost/WCFService/ServiceWCF.svc /out:Proxy.cs The final command window looks like this: MyImage Notice the following: (1) Two files were generated Proxy.cs and ServiceReferences.ClientConfig (2) SLSVCUtil was the command that was called (3) The url for the SL-enabled WCF service is https://localhost/WCFService/ServiceWCF.svc If the SLSVCUTIL is not in your path, go to the previous directory (C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Tools\SLSvcUtil.exe )

  The key step is upon us – to add the generated two files to the project. image
  That’s it. That is the magic recipe that solved a couple hours of torture.