Windows Communication Foundation (WCF) may be a framework for building service-oriented applications.Using WCFyou can send data as asynchronous messages from one service endpoint to a different .

A service endpoint are oftenpart of a continuously available service hosted by IIS, or it are often a service hosted in an application.

The messagescan be as simple as one character or word sent as XML, or as complex as a stream of binary data.

Getting started sample

The service describes the operations it performs in a service contract that it exposes publicly as metadata.

 // Define a service contract. [ServiceContract(Namespace="http://StackOverflow.ServiceModel.Samples")]
 public interface ICalculator 
{  [OperationContract] 
 double Add(double n1, double n2); } 

The service implementation calculates and returns the appropriate result, as shown in the following example code.

 // Service class that implements the service contract.
 public class CalculatorService :
 ICalculator {  public double Add(double n1, double n2) 
 {  return n1 + 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">
< endpoint  address=""  binding="wsHttpBinding"  contract="StackOverflow.ServiceModel.Samples.ICalculator" / >
</services>
</services>

The framework doesn’t expose metadata by default.

<system.serviceModel> <services>
<service
name="StackOverflow.ServiceModel.Samples.CalculatorService"
GoalKicker.com – C# Notes for Professionals 770 
 behaviorConfiguration="CalculatorServiceBehavior" >
 ...
 <--the mex endpoint is explosed at  http:
//localhost/servicemodelsamples/service.svc/mex-->
<endpoint  
address="mex"
binding="mexHttpBinding" 
contract="IMetadataExchange"/>
</service>
</service>
<!--For debugging purposes set the includeExceptionDetailInFaults  attribute to true-->
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors> 
</system.serviceModel>

The client communicates using a given contract type by using a client class that is generated by the ServiceModel Metadata Utility Tool (Svcutil.exe). 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 therefore the contract, as shown within the following example.

<client>
<endpoint  
address="http://localhost/servicemodelsamples/service.svc"
binding="wsHttpBinding" 
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.

 // Create a client.
 CalculatorClient client = new CalculatorClient();
 // Call the Add service operation.
 double value1 = 100.00D;
 double value2 = 15.99D;
 double result = client.Add(value1, value2);
 Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
 //Closing the client releases all communication resources.
 client.Close();  

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Windows Communication Foundation in C#

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password