Chapter 164: Windows CommunicationFoundation

No Comments

WindowsCommunicationFoundation(WCF)isaframeworkforbuildingservice-orientedapplications.UsingWCF, you can send data as asynchronous messages from one service endpoint to another. A service endpoint can be partofacontinuouslyavailableservicehostedbyIIS,oritcanbeaservicehostedinanapplication.Themessages can be as simple as a single character or word sent as XML, or as complex as a stream of binary data.

Section164.1:Gettingstartedsample

Theservicedescribestheoperationsitperformsinaservicecontractthatitexposespubliclyasmetadata.

//Defineaservicecontract. [ServiceContract(Namespace=”http://StackOverflow.ServiceModel.Samples”)]publicint

erfaceICalculator

{

[OperationContract]

doubleAdd(doublen1,doublen2);

}

Theserviceimplementationcalculatesandreturnstheappropriateresult,asshowninthefollowingexamplecode.

//Serviceclassthatimplementstheservicecontract.

publicclassCalculatorService:ICalculator

{

publicdoubleAdd(doublen1,doublen2)

{

returnn1+n2;

}

}

The service exposes an endpoint for communicating with the service, defined using a configuration file (Web.config), as shown in the following sample configuration.

<services>

<service

name=”StackOverflow.ServiceModel.Samples.CalculatorService”behaviorConfiguration=”CalculatorServiceBehavior”>

<!–ICalculatorisexposedatthebaseaddressprovidedby host:http://localhost/servicemodelsamples/service.svc.                     –>

<endpointaddress=””binding=”wsHttpBinding”

contract=”StackOverflow.ServiceModel.Samples.ICalculator”/>

</service>

</services>

Theframeworkdoesnotexposemetadatabydefault.Assuch,theserviceturnsontheServiceMetadataBehavior andexposesametadataexchange(MEX)endpointathttp://localhost/servicemodelsamples/service.svc/mex.The following configuration demonstrates this.

<system.serviceModel>

<services>

<service

name=”StackOverflow.ServiceModel.Samples.CalculatorService”

behaviorConfiguration=”CalculatorServiceBehavior”>

<!–themexendpointisexplosedat http://localhost/servicemodelsamples/service.svc/mex —>

<endpointaddress=”mex”

binding=”mexHttpBinding”contract=”IMetadataExchange”/>

</service>

</services>

<!–FordebuggingpurposessettheincludeExceptionDetailInFaults attributetotrue–>

<behaviors>

<serviceBehaviors>

<behaviorname=”CalculatorServiceBehavior”>

<serviceMetadatahttpGetEnabled=”True”/>

<serviceDebugincludeExceptionDetailInFaults=”False”/>

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

TheclientcommunicatesusingagivencontracttypebyusingaclientclassthatisgeneratedbytheServiceModel Metadata Utility Tool (Svcutil.exe).

RunthefollowingcommandfromtheSDKcommandpromptintheclientdirectorytogeneratethetypedproxy:

svcutil.exe/n:”http://StackOverflow.ServiceModel.Samples,StackOverflow.ServiceModel.Samples”http://localhost/servicemodelsamples/service.svc/mex/out:generatedClient.cs

Like the service, the client uses a configuration file (App.config) to specify the endpoint with which it wants to communicate. The client endpoint configuration consists of an absolute address for the service endpoint, the binding, and the contract, as shown in the following example.

<client>

<endpoint

address=”http://localhost/servicemodelsamples/service.svc”binding=”wsHttpBin

ding”contract=”StackOverflow.ServiceModel.Samples.ICalculator”/>

</client>

The client implementation instantiates the client and uses the typed interface to begin communicating with the service, as shown in the following example code.

//Createaclient.

CalculatorClientclient=newCalculatorClient();

//CalltheAddserviceoperation.

doublevalue1=100.00D;

doublevalue2=15.99D;

doubleresult=client.Add(value1,value2);

Console.WriteLine(“Add({0},{1})={2}”,value1,value2,result);

//Closingtheclientreleasesallcommunicationresources.

client.Close();

About us and this blog

We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

Request a free quote

We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

Subscribe to our newsletter!

More from our blog

See all posts

Leave a Comment