Professional Documents
Culture Documents
Page 1 of 19
Table of contents
Introduction 3
The ABC of Windows Communication Foundation!!! 3
Configuring System-Provided Bindings and Types of Binding 4
How to: Specify a Service Binding in Configuration 8
To specify the BasicHttpBinding to use to configure the service 8
To specify in code to use the BasicHttpBinding for the service 10
How to: Specify a Client Binding in Configuration 12
To specify a custom binding in code and Binding configuration Summary 16
References 19
Page 2 of 19
Introduction:
This Whitepaper gives a brief explanation on the basic concepts of the Communication part in the WCF
(Windows Communication Foundation).In order to communicate with a WCF service, the client needs to
know simple details like ABC of the service.
Figure 1
This Endpoint needs to set the ABC attributes of the WCF service as shown in Figure 2.
Figure 2
Thus in the WCF world, ABC is an abbreviation of Address, Binding and Contract attributes of an
Endpoint.
Where, address is the network address of the service, binding specifies transport protocol (HTTP, TCP,
etc.) selected for the service and contract is the interface the service implements.
For the purpose of this Whitepaper, we are going to emphasize on the Binding part of the WCF
communication mechanism.
Page 3 of 19
The Binding is an attribute of an endpoint and it lets you configure transport protocol, encoding and
security requirements as shown in Figure 3
Figure 3
Protocol channel binding elements, which determine the security, reliability, context flow settings,
or user-defined protocols to use with messages that are sent to the endpoint.
Transport channel binding elements, which determine the underlying transport protocol to use
when sending messages to the endpoint, for example, TCP or HTTP.
Message encoding binding elements, which determine the wire encoding to use for messages
that are sent to the endpoint, for example, text/XML, binary, or Message Transmission
Optimization Mechanism (MTOM).
Types of Binding
The goal of WCF is to unify the way distributed systems are developed prior to release of .Net Framework
3.0. WCF offers a single development framework for all scenarios where distributed solutions were
implemented using different technologies such as ASMX web services, .Net Remoting, COM+, etc. WCF
achieves this by configuring binding attributes of an endpoint. WCF lets you choose HTTP or TCP
transport protocol, encoding, etc. just by tweaking the value of binding attribute for an endpoint.
To cater to different transport protocols, WCF lets you select HTTP, TCP and MSMQ binding types.
BasicHttpBinding:
This type of binding exists in new .Net world only to support backward compatibility with ASMX based
clients (WS-Basic Profile 1.1). Basic http binding sends SOAP 1.1 messages and is used when there is a
requirement for the WCF services to communicate with non WCF based systems. Thus, providing an
endpoint with basicHttpBinding makes interoperating with other basic implementations of web services a
great choice.
Note: All other bindings except basicHttpBinding support WS* specifications including security, reliable
messaging and transaction support, where appropriate.
Let us examine how to setup binding for an endpoint in the web.config file.
Page 4 of 19
Step 1: Choose basicHttpBinding as a value in the binding attribute of an endpoint.
<endpoint
address="http://localhost:8731/EmployeeWCFService.ServiceImplementation.Manager/"
binding="basicHttpBinding"
bindingConfiguration="basicBinding"
contract=" EmployeeWCFService.ServiceContract.IEmployee">
Step 2: This step is optional and is only required if the binding's default properties need to be modified, as
shown in the example below. In this case, name the binding same as bindingConfiguration attribute in the
endpoint section.
<bindings>|
<basicHttpBinding>
<binding name="basicBinding"
textEncoding="utf-8"
openTimeout="00:03:00"
closeTimeout="00:03:00"
/>
</basicHttpBinding>
</bindings>
Note: All other types of binding are setup in the same way.
wsHttpBinding
This binding sends SOAP 1.2 messages and implements WS* specifications to support enterprise
requirements of security, reliability, ordered delivery and transaction management.
netTcpBinding
This binding sends SOAP 1.2 messages, provides binary encoding and optimized communication
between WCF services and WCF clients on Windows network. This binding is the fastest binding
amongst all WCF binding options between different nodes in the TCP network. Unlike http bindings, the
TCP binding does not offer interoperability but is highly optimized for .Net 3.0 and above clients. Thus,
in .Net version 3.0 and above, providing an endpoint with netTcpBinding is an easy option to development
of distributed systems and can replace COM+ and .Net Remoting model.
netNamedPipeBinding
This binding is used to provide secure and reliable Named Pipe based communication between WCF
services and WCF client on the same machine. It is the ideal choice for communication between
processes on the same machine.
Page 5 of 19
(ASMX)-based services. This binding
uses HTTP as the transport and
text/XML as the default message
encoding.
Page 6 of 19
cross-machine communication
between WCF applications.
If you require your service to be consumed by clients compatible with SOAP 1.1, use
basicHttpBinding for interoperability
If you require your service to be consumed within the corporate network, use netTCPBinding for
performance
If you require your service to be consumed over the internet and the client is a WCF compatible,
use wsHttpBinding to reap full benefits of WS* specifications
If you require your service to be accessible only in the same machine, use netNamedPipeBinding
If you require your service to act as server as well as client in a peer to peer environment, utilize
netPeerTcpBinding setting
It is usually the best practice to specify the binding and address information declaratively in configuration
rather than imperatively in code. Defining endpoints in code is usually not practical because the bindings
Page 7 of 19
and addresses for a deployed service are typically different from those used while the service is being
developed. More generally, keeping the binding and addressing information out of the code allows them
to change without having to recompile or redeploy the application.
All of the following configuration steps can be undertaken using the Configuration Editor Tool
(SvcConfigEditor.exe).
For the source copy of this example, see Basic Binding Samples.
Note:
Address or binding information is not specified inside the implementation of the service. Also, code
does not have to be written to fetch that information from the configuration file.
Page 8 of 19
}
1.Create a Web.config file to configure an endpoint for the CalculatorService that uses the
WSHttpBinding.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name=" CalculatorService" >
<endpoint
<-- Leave the address blank to be populated by default-->
<--from the hosting environment,in this case IIS, so -->
<-- the address will just be that of the IIS Virtual -->
<--Directory.-->
address=""
<--Specify the binding type -->
binding="wsHttpBinding"
<--Specify the binding configuration name for that -->
<--binding type. This is optional but useful if you -->
<--want to modify the properties of the binding. -->
<--The bindingConfiguration name Binding1 is defined -->
<--below in the bindings element. -->
bindingConfiguration="Binding1"
contract="ICalculator" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="Binding1">
<-- Binding property values can be modified here. -->
<--See the next procedure. -->
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
1. Create a Service.svc file that contains the following line and place it in your Internet Information
Services (IIS) virtual directory.
It is usually the best practice to specify the binding and address information declaratively in configuration
rather than imperatively in code. Defining endpoints in code is usually not practical because the bindings
and addresses for a deployed service are typically different from those used while the service is being
developed. More generally, keeping the binding and addressing information out of the code allows them
to change without having to recompile or redeploy the application.
For a description of how to configure this service using configuration elements instead of code, see How
to: Specify a Service Binding in Configuration.
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
2. Implement the service contract in a service class.
The procedure outlined assumes that the calculator service is running. For information about how to build
the service, see How to: Specify a Service Binding in Configuration. It also uses the ServiceModel
Metadata Utility Tool (Svcutil.exe) that Windows Communication Foundation (WCF) provides to
automatically generate the client components. The tool generates the client code and configuration for
accessing the service.
The client is built in two parts. Svcutil.exe generates the ClientCalculator that implements the
ICalculator interface. This client application is then constructed by constructing an instance of
ClientCalculator.
It is usually the best practice to specify the binding and address information declaratively in configuration
rather than imperatively in code. Defining endpoints in code is usually not practical because the bindings
and addresses for a deployed service are typically different from those used while the service is being
developed. More generally, keeping the binding and addressing information out of the code allows them
to change without having to recompile or redeploy the application.
You can perform all of the following configuration steps by using the Configuration Editor Tool
(SvcConfigEditor.exe).
Page 11 of 19
For the source copy of this example, see the Basic Binding sample.
2. The client that is generated contains the ICalculator interface that defines the service contract
that the client implementation must satisfy.
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel",
"3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://Microsoft.ServiceModel.Sa
mples", ConfigurationName="Microsoft.ServiceModel.Samples.ICalculator")]
public interface ICalculator
{ [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Sam
ples/ICalculator/Add",
ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
double Add(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Sampl
es/ICalculator/Subtract",
ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
double Subtract(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Sampl
es/ICalculator/Multiply",
ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
double Multiply(double n1, double n2);
[System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Sampl
es/ICalculator/Divide",
ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
double Divide(double n1, double n2);
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class CalculatorClient :
System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>,
Microsoft.ServiceModel.Samples.ICalculator
{
public CalculatorClient()
Page 12 of 19
{
}
4. Svcutil.exe also generates the configuration for the client that uses the BasicHttpBinding class.
When using Visual Studio, name this file App.config. Note that the address and binding
information are not specified anywhere inside the implementation of the service. Also, code does
not have to be written to retrieve that information from the configuration file.
<client>
<endpoint
name=""
address="http://localhost/servicemodelsamples/service.svc"
binding="basicHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />
</client>
<bindings>
<basicHttpBinding/>
</bindings>
</system.serviceModel>
</configuration>
5. Create an instance of the ClientCalculator in an application, and then call the service operations.
using System;
using System.ServiceModel;
namespace Microsoft.ServiceModel.Samples
{
//Closing the client gracefully closes the connection and cleans up resources
client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
}
}
6. Compile and run the client.
In this example, a client is created to use a calculator service and the binding for that client is specified
imperatively in code. The client accesses the CalculatorService, which implements the ICalculator
interface, and both the service and the client use the BasicHttpBinding class.
This procedure assumes that the calculator service is running. For information about building the service,
see How to: Specify a Service Binding in Configuration. It also uses the ServiceModel Metadata Utility
Tool (Svcutil.exe) Windows Communication Foundation (WCF) provides to automatically generate the
client components. The tool generates the client code for accessing the service.
The client is built in two parts. Svcutil.exe generates the ClientCalculator that implements the
ICalculator interface. This client application is then constructed by constructing an instance of
ClientCalculator and then specifying the binding and the address for the service in code.
For the source copy of this example, see the Basic Binding sample.
2. The client that is generated contains the ICalculator interface that defines the service contract
that the client implementation must satisfy.
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}
3. The generated client also contains the implementation of the ClientCalculator.
Page 15 of 19
public class CalculatorClient :
System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>,
Microsoft.ServiceModel.Samples.ICalculator
{
public CalculatorClient()
{
}
Page 16 of 19
//Client implementation code.
class Client
{
static void Main()
{
//Specify the binding to be used for the client.
BasicHttpBinding binding = new BasicHttpBinding();
//Closing the client gracefully closes the connection and cleans up resources
client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
}
}
5.Compile and run the client.
Page 17 of 19
Binding configuration summary
Following table shows parameters such as security, transport protocol, encoding and hosting environment
choices available with different binding for a WCF service. This summary should help choose an
appropriate binding for your project environment.
Can I have multiple endpoints of the same binding type e.g. multiple endpoints of basicHttpBinding?
Answer: Yes
Can I have multiple endpoints of different binding types to serve different types of clients e.g. an endpoint
with basicHttpBinding, an endpoint with wsHttpBinding and an endpoint with netTcpBinging?
Answer: Yes
Summary
As you have seen, Windows Communication Foundation provides a framework to configure the transport,
security and encoding needs to cater various communication requirements. The WCF framework keeps
the development API same irrespective of the way it is going to be consumed by the clients & the
endpoint is provided to meet the communication requirements, and you can have one or many of them to
cater to different clients and communication requirements. This eliminates the need to learn and
implement diverse set of communication models to support HTTP or TCP communication infrastructures.
References:
http://www.c-sharpcorner.com/UploadFile/pjthesedays/bindwcf05032009012251AM/bindwcf.aspx
Page 18 of 19
http://msdn.microsoft.com/en-us/library/ms731092.aspx
Page 19 of 19