Objective:
This article is targeted to beginners in WCF. This is a absolute theoretical article.
It will explain all theoretical concept of an EndPoint in WCF.
It will explain Address in WCF
It will explain Binding and where, which binding should be used.
It will explain Contract.
ABC of Endpoint in WCF
The three elements constitute an endpoint starts with letters that makeup a mnemonic phrase
"ABC of Endpoints"
Address of an Endpoint(A)
Address specifes where the service is residing.
This is an URL( Uniform Resource Locator)
Address URL identifies , location of the service.
Address should folllow the Web Service Addressing(WS-Addressing) standard.
The Address depends on whether it is hosted in IIS or managed application or WAS. This also depends on BINDING being used.
Example of different type of Addresses
Binding of an Endpoint (B)
Binding says , how the service could be used. Binding specifies
Which protocol to be used
Which encoding to be used.
What type of security requeiremnet to be used like SSL or SOAP message security.
System provided Bindings
basicHttpBinding
This is interoperable binding.
This is commonly used as replacement for earler web service based on ASMX.
It supports HTTP & HTTPS protocols.
It supports MTOM encoding.
It supports text encoding.
wsHttpBinding
This is a secure binding.
This is interoperable binding.
This uses SOAP over HTTP.
This supports reliability over Internet.
This supports transaction over Internet.
This supports security over Internet.
This supports HTTP/HTTPS proptcol
This supports text and MTOM encoding.
This is default binding provided by WCF.
wsDualHttpBinding
This supports all the feature of wsHttpBinding.
This is mainly used for DUPLEX SERVICE CONTRACTS.
This supports bidirectional communication.
webHttpBinding
This is a secure binding.
This is a interoperable binding.
It supports Http/Https protocol.
It does not use SOAP message format.
wsFederationHttpBinding
This is a secure Binding.
This is interoperable binding.
This supports federated security
This supports Https/Https protocols.
This uses text/MTOM encoding.
netTCPBinding
This is a secure Binding.
This could only be used if client is also a WCF machine.
This is used to send Binary encoded SOAP message from one WCF computer to another.
This uses TCP (Transimission Control Protocol)
This supports reliability.
This supports transaction,
This supports security.
netNamedPipeBinding
This is a secure Binding.
This could be used over a single WCF computer.
This sends Binary encoded SOAP message over named pipes.
netMSMQBinding
This is a queued Binding.
This uses Binary encoded SOAP message.
This sends message over MSMQ.
Here communication should occur between two computers.
netPeerTCPBinding
This is a secure Binding.
This uses TCP overe peeer to peer.
Communication should occur between two or more computers.
msmqIntegrationBinding
basicHttpContextBinding
This Binding is same as of basicHttpBinding with more attributes , like below
It supports HTTP cookies
It eanble SOAP haeders to excahnge context.
This binding is mainly used for Durable services.
netTCPContextBinding
This Binding is same as of netTCPBinding with more attributes , like below
It eanble SOAP haeders to excahnge context.
This binding is mainly used for Durable services.
wsHttpContextBinding
This Binding is same as of wsHttpBinding with more attributes , like below
It eanble SOAP haeders to excahnge context.
This binding is mainly used for Durable services.
Example :
<system.serviceModel>
<services>
<service name="WcfService2.Service1" behaviorConfiguration="WcfService2.Service1Behavior">
<!-- Service Endpoints -->
<endpoint address="" binding="wsHttpBinding" contract="WcfService2.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService2.Service1Behavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Contract of and EndPoint (C)
Contract should be an Interacfe
Contract could be a class also but better approach is interface.
In config file this is preceded by prpject name space name
<endpoint address="" binding="wsHttpBinding" contract="WcfService2.IService1">
Difference b/w Web services & WCF
Introduction
In this post I will explain the Difference between ASP.NET web service and programming WCF services like ASP.NET web services. It also discusses how we use the both technologies for developing the web services.
The development of web service with ASP.NET relies on defining data and relies on the XmlSerializer to transform data to or from a service.
Key issues with XmlSerializer to serialize .NET types to XML
Only Public fields or Properties of .NET types can be translated into XML.
Only the classes which implement IEnumerable interface.
Classes that implement the IDictionary interface, such as Hash table can not be serialized.
The WCF uses the DataContractAttribute and DataMemeberAttribute to translate .NET FW types in to XML.
[DataContract]
public class Item
{
[DataMember]
public string ItemID;
[DataMember]
public decimal ItemQuantity;
[DataMember]
public decimal ItemPrice;
}
The DataContractAttribute can be applied to the class or a strcture. DataMemberAttribute can be applied to field or a property and theses fields or properties can be either public or private.
Important difference between DataContractSerializer and XMLSerializer.
A practical benefit of the design of the DataContractSerializer is better performance over XMLserialization.
XMLSerialization does not indicate the which fields or properties of the type are serialized into XML where as DataCotratSerializer Explicitly shows the which fields or properties are serialized into XML.
The DataContractSerializer can translate the HashTable into XML.
Developing Service
To develop a service using ASP.NET we must add the WebService attribute to the class and WebMethodAttribute to any of the class methods.
Example
[WebService]
public class Service : System.Web.Services.WebService
{
[WebMethod]
public string Test(string strMsg)
{
return strMsg;
}
}
To develop a service in WCF we will write the following code
[ServiceContract]
public interface ITest
{
[OperationContract]
string ShowMessage(string strMsg);
}
public class Service : ITest
{
public string ShowMessage(string strMsg)
{
return strMsg;
}
}
The ServiceContractAttribute specifies that a interface defines a WCF service contract, OperationContract Attribute indicates which of the methods of the interface defines the operations of the service contract.
A class that implements the service contract is referred to as a service type in WCF.
Hosting the Service
ASP.NET web services are compiled into a class library assembly and a service file with an extension .asmx will have the code for the service. The service file is copied into the root of the ASP.NET application and Assembly will be copied to the bin directory. The application is accessible using url of the service file.
WCF Service can be hosted within IIS or WindowsActivationService.
• Compile the service type into a class library
• Copy the service file with an extension .SVC into a virtual directory and assembly into bin sub directory of the virtual directory.
• Copy the web.config file into the virtual directory.
Client Development
Clients for the ASP.NET Web services are generated using the command-line tool WSDL.EXE.
WCF uses the ServiceMetadata tool(svcutil.exe) to generate the client for the service.
Message Representation
The Header of the SOAP Message can be customized in ASP.NET Web service.
WCF provides attributes MessageContractAttribute , MessageHeaderAttribute and MessageBodyMemberAttribute to describe the structure of the SOAP Message.
Service Description
Issuing a HTTP GET Request with query WSDL causes ASP.NET to generate WSDL to describe the service. It returns the WSDL as response to the request.
The generated WSDL can be customized by deriving the class of ServiceDescriptionFormatExtension.
Issuing a Request with the query WSDL for the .svc file generates the WSDL. The WSDL that generated by WCF can customized by using ServiceMetadataBehavior class.
Exception Handling
In ASP.NET Web services, Unhandled exceptions are returned to the client as SOAP faults.In WCF Services, unhandled exceptions are not returned to clients as SOAP faults. A configuration setting is provided to have the unhandled exceptions returned to clients for the purpose of debugging.
What is Proxy and how to generate proxy for WCF Services?
Answer:
The proxy is a CLR class that exposes a single CLR interface representing the service contract. The proxy provides the same operations as service's contract, but also has additional methods for managing the proxy life cycle and the connection to the service. The proxy completely encapsulates every aspect of the service: its location, its implementation technology and runtime platform, and the communication transport.
The proxy can be generated using Visual Studio by right clicking Reference and clicking on Add Service Reference. This brings up the Add Service Reference dialog box, where you need to supply the base address of the service (or a base address and a MEX URI) and the namespace to contain the proxy.
Proxy can also be generated by using SvcUtil.exe command-line utility. We need to provide SvcUtil with the HTTP-GET address or the metadata exchange endpoint address and, optionally, with a proxy filename. The default proxy filename is output.cs but you can also use the /out switch to indicate a different name.
SvcUtil http://localhost/MyService/MyService.svc /out:Proxy.cs
When we are hosting in IIS and selecting a port other than port 80 (such as port 88), we must provide that port number as part of the base address:
SvcUtil http://localhost:88/MyService/MyService.svc /out:Proxy.cs
WCF Test Client (WcfTestClient.exe)
WCF Service Host Starts WCF Test Client with Multiple Services
You can also use WCF Test Client to help debug a service project that contains multiple services. When WCF Test Client opens, it automatically iterates the list of services in your project and opens them for testing.
Outside Visual Studio
You can also invoke the WCF Test Client (WcfTestClient.exe) outside Visual Studio to test an arbitrary service on the Internet. To locate the tool, go to the following location:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\
To use the tool, double-click the file name to open it from this location, or launch it from a command line.
WCF Test Client takes an arbitrary number of URIs as command line arguments. These are the URIs of services that can be tested.
wcfTestClient.exe URI1 URI2 …
After the WCF Test Client window is opened, click File->Add Service, and enter the endpoint address of the service you want to open.
WPF Controls using C#
- WPF Programming using C#
Silverlight Programming using C#- Silverlight 4 Linq to Sql and RIA sevice for CRUD operations
- Styles in Silverlight 3 Application
- Navigation in Silverlight 3
- Navigation in Silverlight Without Using Navigation Framework
- Asynchronous Programming Model in Silverlight 4 with WCF RIA
- Host Silverlight in ASP.NET
- Data Binding in Silverlight Accordion Control using XML
- Image Slider in Silverlight 4 using XML Data
- Photo Slideshow in Silverlight 4
- Integrating Silverlight in SharePoint 2010
- Introduction to Validation in Silverlight 4
Silverlight Controls using C#