I have seen some confusions in my fellow developers when creating WCF clients to consume from WCF SOAP Service (Here why I have explicitly mentioned SOAP Service is if you are using a WCF REST Service and created a Proxy class for WCF Rest service using svcutil.exe or by Adding Service Reference, it doesn’t work. Here is a link to a great post on describing why).
Some seem to be bit confused in the term Proxy class. This is kind of like a basic topic and everyone should have a solid understanding about the Proxy class. So in this post I am going to explain about Proxy class in respect to WCF clients.
Some seem to be bit confused in the term Proxy class. This is kind of like a basic topic and everyone should have a solid understanding about the Proxy class. So in this post I am going to explain about Proxy class in respect to WCF clients.
A WCF client and a WCF SOAP Service can communicate using SOAP messages, which encapsulate the input and output parameters as XML. The Proxy class maps parameters to XML elements and then sends the SOAP messages over a network. There are two ways to add a proxy class to your client project using the Microsoft .NET Framework: with the svcutil.exeServiceModel Metadata Utility Tool (Svcutil.exe) in the .NET Framework, or by adding a Service reference in Microsoft Visual Studio.
For the demonstration, I will be using following WCF Service.
IService1.cs[ServiceContract]
public interface IService1
{
[OperationContract]
string SayHello(string name);
}
Service.cs
public class Service1 : IService1
{
public string SayHello(string name)
{
return string.Format("Hello {0}!", name);
}
}
ServiceModel Metadata Utility Tool (Svcutil.exe)
The ServiceModel Metadata Utility Tool (svcutil.exe) can be found at the Windows SDK installation location, specifically,
C:\Program Files\Microsoft SDKs\Windows\”yourversion”\binNow let’s see how to use svcutil.exe.
I am opening the Command Prompt, navigating to the above folder and issuing the following command.
svcutil.exe /d:D:\Temp http://localhost:64435/Service1.svc
Here /d parameter for mentioning the directory to create files in. Default will be the current directory. Since I don’t want my files to be created in current directory, I have given a path of a temp folder. (When you are using svcutil.exe, there are a lot of parameters you can pass in. It's up to you to learn those).
Running svcitul.exe |
Now when I examine my temp folder, I can see two files created. One is a config file and the other, the proxy class.
Now I am creating a Console Application and copy and pasting the content in config file into my projects’ app.config file. Then I am adding the proxy class to the project by right clicking on the project and Add->Existing Item.
Then adding a reference to System.ServiceModel.
Add Reference to System.ServiceModel |
using System;
namespace MyWcfClientUsingSvcUtil
{
class Program
{
static void Main(string[] args)
{
using (Service1Client client = new Service1Client())
{
Console.WriteLine(client.SayHello("Jaliya"));
}
}
}
}
Result |
Add Service Reference in Microsoft Visual Studio
Why take all the steps above when you have Visual Studio. You can just use Visual Studio add to Add Service Reference. Here I have created another Console Application and after project is created, I am right clicking on References and clicking on Add Service Reference.
Add Service Reference |
After entering the Service address and providing the namespace name, I am clicking on OK. Now I am ready to write the code.
using MyWcfClientUsingAddSerRef.svcService1;
using System;
namespace MyWcfClientUsingAddSerRef
{
class Program
{
static void Main(string[] args)
{
using (Service1Client client = new Service1Client())
{
Console.WriteLine(client.SayHello("Jaliya"));
}
}
}
}
Result |
Here is a link to one of my previous posts which shows how to create WCF clients without using a Proxy class.
Calling WCF Service using ChannelFactory from a Silverlight Application
I am uploading the sample to my SkyDrive.
Happy Coding.
Regards,
Jaliya
I am uploading the sample to my SkyDrive.
Happy Coding.
Regards,
Jaliya
No comments:
Post a Comment