Thursday, July 29, 2010

WCF Client Sample code

static void Main(string[] args)
{
using (WCFServiceClient client = new WCFServiceClient("WSDualHttpBinding_IWCFService"))
{
string output = client.MyOperation1("Manoj");
Console.WriteLine(output);
DataContract1 dc = new DataContract1();
dc.FirstName = "Shankar";
dc.LastName = "Mahadevan";
output = client.MyOperation2(dc);
Console.WriteLine(output);
Console.ReadLine();
}

Configuration of WCF in Host

Service.cs
[OperationContract]
[System.ServiceModel.Web.WebGet(UriTemplate = "/MyOperation/MyOperation1/{myValue1}", ResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json, BodyStyle = System.ServiceModel.Web.WebMessageBodyStyle.WrappedResponse)]

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)]

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

Generating a proxy for the client
svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config http://localhost:2409/WCF_Host/Service.svc

Configuration of webHttpBinding and wsDualHttpBinding

< ?xml version="1.0"?>
< configuration>
< system.serviceModel>
< serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
< behaviors>
< endpointBehaviors>
< behavior name="webHttpEnablingBehavior">
< webHttp />
< /behavior>
< /endpointBehaviors>
< serviceBehaviors>
< behavior name="webHttpEnablingBehavior">
< serviceMetadata httpGetEnabled="true"/>
< serviceDebug includeExceptionDetailInFaults="true" />
< /behavior>
< /serviceBehaviors>
< /behaviors>
< services>
< service name="WCFService" behaviorConfiguration="webHttpEnablingBehavior">
< endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
< endpoint address="" binding="webHttpBinding" bindingConfiguration="default" contract="IWCFService" behaviorConfiguration="webHttpEnablingBehavior" />
< endpoint address="MyOperation" binding="wsDualHttpBinding" bindingConfiguration="default" contract="IWCFService" />
< /service>
< /services>
< client />
< bindings>
< webHttpBinding>
< binding name="default" />
< /webHttpBinding>
< wsDualHttpBinding>
< binding name="default" />
< /wsDualHttpBinding>
< /bindings>
< /system.serviceModel>
< system.web>
< compilation debug="true"/>
< /system.web>
< /configuration>