Tuesday, September 20, 2011

Generating WCF Proxy using SvcUtil.exe

Svcutil.exe is a command line tool that helps to generate proxy class in C#.Net. It is very easy to call the WCF service using Visual studio like the way below: Right click on the project then go to Add service reference and then the Add Service Reference dialog will appear as shown in the figure below.

Then in the address bar give the address of the WCF service and click on Go. Example:
Optionally you can change the Namespace and click on Ok. Now you can access your WCF service.But what will happen if you are going to call that WCF in the production environment where there will be no Visual studio, you can not go for the above (Add Service Reference approach). Here it comes the SvcUtil.exe. To use that go to Start -> All Programs -> Microsoft Visual Studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010). Then type

svcutil /language:cs /out:generatedproxy.cs /config:app.config
svcutil /language:cs /out:generatedproxy.cs /config:app.config
generatedproxy:It is the class name, you can provide any name.
language:cs: This is to select the language, for Vb.Net you can type language:VB
After this SvcUtil will generate 2 files one with the class name you have provided above and other is the app.config file. The location of the files are:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\generatedproxy.cs
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\app.config
You can copy the generatedproxy.cs class file and app.config file and paste in the project directory. But If you are calling the WCF service inside a Web Site or Web Application, then you must have a web.config file. Then copy from <system.serviceModel> to </system.serviceModel> and paste it in the web.config before </configuration>.
Now you can create a object of the proxy class and use that.

1 comment:

  1. If any one need help in wcf deployment to windows service let me know !!! I will try to help you.