Monday, January 28, 2008

WCF and Callbacks - How to generate proxy using svcutil tool

Many of us tried to implement even a small simple of WCF service and a callback implementation, which (at least to me) I couldn't
get it to work. I've looked here,
there,
and some many others and I couldn't create a proxy which I use in my client.

In order to make the WCF service able to make a Callback to clients (if some client fired some events.., or even if the service decides to
because of an operation it monitors), you have to create a proxy (on service standards) so the client can be able to implement the ICallback interface.

How can I create this proxy?
There is two solutions for this,


  • First: Use a tool called SvcUtil.exe to generate a proxy.cs and app.config file from the service, so I can use both in my client.

  • Second: After making the service, start it, in the client add a service reference to thats you have just started.


Generate proxy.cs and app.config files
To use the svcutil.exe tool follow this:

  • Start > All Programs > Visual Studio 200x > Visual Studio Tools > Visual Studio 200x Command Prompt

  • type cd.. and switch to this path C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin

  • type sn.exe -Vr svcutil.exe Run svcutil tool

  • now we can use svcutil, switch to the path where your service is Where is svcutil.exe

  • if the service is .svc start it then type svcutil http://localhost:8080/CallbackAppNAME/YOURSERIVCENAME.svc?wsdl
    Use svcutil tool


  • if the service is .exe type svcutil YOURSERVICE.exe

  • there is gonna be file .wsdl and .xsd, type svcutil fileName.wsdl filename.xsd /language:C# /out:Proxy.cs /config:app.config



The generated files can be found here C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin (this where the svcutil tool is).
Note: There are more options you can use with svcutil tool like:
merging new configuration file with a one already exists, use this /mergeConfig.
generating a proxy with asynchronous methods, use this /a

Add a service reference
You can just add a service reference from your client:

  • With Visual Studio, start your ready .svc service.

  • In the client solution right click to add a service reference and just point to the service.

  • This will make a reference to call the service directly.