Pages

Saturday, October 27, 2012

My last post was about netTcpBinding in WCF and I thought it's better if I write a more detailed post about endpoints and bindings in WCF.

Windows Communication Foundation gives us the ability to transmit messages using different transport protocols such as HTTP, TCP, and MSMQ (Microsoft Message Queuing) etc. As you already know all communication with a WCF service occurs through the endpoints of the service.

There can be more than one endpoint for a service (Multiple Endpoints) and these endpoints provide client's the access to functionality offered by a WCF service. These endpoints has many properties and there are three main attributes which are commonly known as ABC’s or Where, How and What of a service. The ABCs make up an endpoint in WCF and these endpoints need to be set for both the service and the client.

Address : The address uniquely identifies the endpoint and tells where the service is located.

Binding : The binding specifies how to communicate with the endpoint. This includes the transport protocol, encoding to use for the messages and the necessary security requirements.

Contract : The contract outlines what functionality the endpoint exposes to the client.

Today I will be focusing on the Address and the Binding attributes, because these two are very important.

For demonstration purposes I have created a WCF Service Application which is “WCFBindingApp” and I am using the IIS and not the Visual Studio Development server to host my service. Because we can’t use TCP bindings for services which are hosted in Visual Studio Development Server.

When we define endpoints for a service, there are numerous options for specifying the address. You can either use the IP address or the host name along with the port number. If no port number is mentioned the default port for the protocol is used. There are two techniques we can use to specify the address.

2. As I said before, when hosting in IIS, you don’t have to mention the base address. The base HTTP address is determined by the IIS virtual directory and WCF ignores any base addresses we may specify.

3. When hosting in IIS, if you do specify an absolute address for a particular endpoint, it must start with the base address corresponding to the virtual directory.

4. When exposing multiple endpoints with same contract and with different bindings, each endpoint address must be unique.

5. If multiple endpoints share the same binding with different contracts, you can use the same address across those endpoints.

6. You can have multiple base addresses for each service, but only one for each protocol.

Bindings of a Endpoint

The bindings is the key element of the WCF architecture.There are variety types of bindings available to use when defining endpoints.

Built-In Bindings

Within the Windows Communication Foundation programming model, bindings are represented by the “System.ServiceModel.Channels.Binding” class. All binding classes must derive from this base class. Following image shows all the binding classes which are available with WCF.

Binding class which are available out of the box

In here I will only list the features of most used ones.

basicHttpBinding

Designed for scenarios where interoperability is of utmost importance.

HTTP for the transport and text for the message encoding.

Message version is SOAP 1.1.

Capable of using transport or message security, but both are disabled by default.

wsHttpBinding

Designed for interoperability while incorporating with richer Web services protocols for security, reliable messaging, and transaction.

Message version is SOAP 1.2.

This is default binding in WCF.

wsDualHttpBinding

Supports all the feature of wsHttpBinding.

Used for Duplex service contracts.

Supports bidirectional communication.

netTCPBinding

Unlike the ws* specific bindings, the various "Net" bindings were not designed for interoperability. This explains why the binding names are prefixed with "Net"

Assumes you have the Microsoft .NET Framework 3.0 installed on both sides.

NetTcpBinding uses TCP for the transport, binary for the message encoding, and SOAP 1.2 for the message version.

Enables transport security by default and can support transactions if enabled.

netPeerTCPBinding

Provides a secure binding for peer-to-peer network applications.

netNamedPipeBinding

Provides a secure and reliable binding that is optimized for on-machine communication.

Can be only used over a single WCF computer.

netMSMQBinding

Represents a queued binding that is suitable for cross-machine communication.

Named pipes : Fast communication between the client and the server which runs on the same machine. Works only in WCF-WCF.

MSMQ : Microsoft Message Queue allows queuing of messages and very useful in disconnected communications between the client the server. Used when client wants to enqueue a message that a service can consume later.

Custom Protocols : Can define own protocols.

Now let’s move in to see how binding is specified in the configuration file. The basic way is as follows.

Here I have specified three bindings. And default settings are applied to each bindings. Now let’s say you want to change the wsHttpBinding defaults and apply new settings. You can easily do it this way.

Wednesday, October 24, 2012

I have wrote some posts about WCF and today I am going to write about configuring netTcpBinding in WCF as it needs some little extra work. WCF or Windows Communication Foundation is one my favorite areas in the .NET Framework and bindings is one of the things which I like the most in WCF.

Before starting off, there is a small check list to be checked. First is, you can’t configure netTcpBinding, if your service is running in the Visual Studio Development Server. The reason for this is Visual Studio Development Server only supports HTTP. And to configure netTcpBinding, we need IIS 7. This will not work on previous versions of IIS.

Next you have to make sure “Windows Communication Foundation Non-HTTP Activation” is available. Since I am using Windows Server 2008R2 operating system, it’s on the .NET Framework 3.5.1 Features under Features in Server Manager.

WCF Activation

Final item in the check list is, you have to make sure following service is started.

Net.Tcp Listener Adapter

Now I have created a WCF Service Application and I have changed the project properties as follows.

Use Local IIS Web Server

Then you can click on "Create Virtual Directory" button or if you run the project for the first time without clicking it, you will be asked before Visual Studio creating a directory for you. After creating the virtual directory you can see it under “Default Web Site” in the IIS.

Default Web Site in IIS

Now you have to do two configurations in IIS. Right click on “Default Web Site” and click on "Edit Bindings". Check whether net.tcp type is there and if not add it.

Default Web Site : Site Bindings

Now right click on created Virtual Directory and select "Advanced Settings" under Manage Applications. In Enabled Protocols add net.tcp.

Advanced Settings of created virtual directory

Now you are done with the IIS configurations. Now modify the web.config as follows.

<system.serviceModel><behaviors><serviceBehaviors><behavior><!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --><serviceMetadatahttpGetEnabled="false"/><!-- 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 --><serviceDebugincludeExceptionDetailInFaults="false"/></behavior></serviceBehaviors></behaviors><services><servicename="WcfApplication.Service1"><endpointaddress=""contract="WcfApplication.IService1"binding="netTcpBinding"></endpoint><endpointaddress="mex"contract="IMetadataExchange"binding="mexTcpBinding"></endpoint><host><baseAddresses><addbaseAddress="http://localhost/WcfApplication/Service.svc"/></baseAddresses></host></service></services><serviceHostingEnvironmentmultipleSiteBindingsEnabled="true"/></system.serviceModel>

And that's it. In here please note that I have put httpGetEnabled to "false". Now right click on the Service and view it in your browser. You will get something like this.

Created Service

Now you can test this service by adding a service reference from another project or by using WCF Test Client which is located at,

Wednesday, October 17, 2012

Sometimes there can be situations where you need to update a gridview inside a ASP.NET page without refreshing the page. This is how you can do it with Update Panel in AJAX Extensions. In here I have two gridviews and one is inside the Update Panel and the other one is out of the Update Panel. I am using the timer control to update the gridview in a timely manner.

Before creating the Data-driven subscription make sure SQL Server Agent service is up and running. That’s because what really happens when you create Data-driven subscription is, there will be job created in SQL Server.

SQL Server Agent

Without much information, I am pasting the code down here with all the comments. I have added a service reference to ReportService2010 web service.