Improve Your Technology

Just another blog for techology



By George Shepherd


George Shepherd is an instructor for Develop Mentor, focusing on ASP.NET and Windows Presentation Foundation. He has written several books on Microsoft technology including MFC Internals from Addison Wesley and Microsoft ASP.NET 2.0 Step By Step from Microsoft Press. To learn more about this book, visit Microsoft Learning.


Included in this article:

1.  Remoting Circa 2005

2.  What is WCF?

3.  How WCF Plays with ASP.NET

4.  Creating an ASP.NET-based WCF Service

5.  Consuming WCF Services


This article provides an overview of Windows Communication Foundation from the ASP.NET point of view. You’ll learn what WCF brings to the table and how it works with ASP.NET.


Remoting circa 2005


We’ve been able to call functions “over there” from “over here” for a while now. Networking on the Microsoft platform is available using Remote Procedure Calls, the Distributed Component Object Model, raw sockets, and Hypertext Transfer Protocol. As .NET development became the norm for most applications on the Microsoft platform, .NET remoting and XML Web Services emerged as the most popular connection protocols. Applications requiring the highest performance and security criteria usually rely on .NET remoting. Applications requiring reach and connection flexibility rely on XML Web Services. Before there was WCF, developers had to select their remoting choices upfront since these two methods of remoting stand at odds with each other in many respects.


What is WCF?


The Windows Communication Foundation (WCF) represents one of three main pillars of .NET 3.x. These three specific highly leverage able technologies include Windows Workflow Foundation, Windows Presentation Foundation, and Windows Communication Foundation. Each of these technologies redefines programming within a certain idiom. Windows Workflow Foundation unifies the business work flow model. Windows Presentation Foundation redefines writing user interfaces, whether for Windows desktop applications or for the Web (using Silver light). Finally, Windows Communication Foundation unifies the distributed 2 programming model for the Microsoft platform. Clearly unifying these fragmented programming models is the main theme of .NET 3.5.


WCF Endpoints


Endpoints define the originators and recipients of WCF communications. Microsoft has come up with a clever acronym for defining endpoints: ABC. That is, WCF endpoints are defined by an address, a binding, and a contract.



The address identifies the network location of the endpoint. WCF endpoints use the addressing style of the transport moving the message. WCF is addressing supports using both fully qualified addresses and relative addresses. For example, a fully qualified Internet protocol address looks like the following: http://someserver/someapp/mathservice.svc/calculator. WCF supports relative addressing by using a base address and then a relative address. Base addresses are registered with the service, and WCF can find services relative to the base address of the service. For example, an endpoint might comprise a whole address using a base address such as http://someserver/someapp/mathservice.svc and a relative address of calc.




WCF bindings specify how messages are transmitted. Rather than being identified simply by a transport and wire format coupled together (à la DCOM), WCF bindings are composed from a stack of binding elements that, at a minimum, include a protocol, transport, and encoder.



The final element defining an endpoint is the contract. The contract specifies the primary agreement between the client and the service as to what the service can do for the client. The contract specifies the information to be exchanged during a service call.


WCF expresses a Service Contract as a .NET interface adorned with the [ServiceContract] attribute. Methods within the WCF contract interface are annotated with the [OperationContract] attribute. WCF interfaces may pass data structures as well. Data members within the structures are exposed as properties and adorned with the [DataMember] attribute.




WCF channels represent the message transmission system. WCF defines protocol channels and transport channels. Protocol channels add services such as security and transactions independently of transport. Transport channels manage the physical movement of bytes between endpoints. For example, WCF uses protocols such as MSMQ, HTTP, P2P, TCP, or Named Pipes. WCF uses a factory pattern to make creating channels consistent.



In WFC, the service contract defines what the service will do. The service contract implementation specifies exactly how the service contract functionality works. However, one of the hallmarks of a distributed system is that it usually requires some add-on functionality that may not necessarily be tied to contract implementation. For example, when securing a Web service, authenticating and authorizing the client may be necessary, but it’s usually not part of the service contract. WFC implements this kind of add-on functionality through behaviors. Behaviors implement the SOA higher-order notion of policy and are used to customize local execution.


Behaviors are governed by attributes—the main two of which are the ServiceBehaviorAttribute and the OperationBehaviorAttribute. The ServiceBehaviorAttribute and OperationBehaviorAttribute attributes control the following aspects of the service execution:



   Concurrency and synchronization support

   Transaction behavior

   Address filtering and header processing

   Serialization behavior

   Configuration behavior

   Session lifetime

   Metadata transformation

   Instance lifetimes


Applying these attributes to modify the server execution is easy. Just adorn a service or operation implementation with the appropriate attribute and set the properties. For example, to require that callers of an operation support impersonation, adorn a service operation with the OperationBehavior attribute and set the Impersonation property to ImpersonationOption.Require.




The final element of WCF is the actual message. WCF messages are modeled on SOAP messages. They are composed of an envelope, a header, a body, and addressing information. Of course, messages also include the information being exchanged. WCF supports three Message Exchange Patterns: one-way, request-response, and duplex. One-way messages are passed from the transmitter to the receiver only. Messages passed using the request response pattern are sent from the transmitter to the receiver, and the receiver is expected to send a reply back to the originator. Messages using the request response pattern block until the receiver sends a response to the originator. When using the duplex messaging, services can call back to the client while executing a service requested by the client. The default Message Exchange Pattern is request-response.


How WCF Plays with ASP.NET


Although WCF applications may be hosted by manually written servers, ASP.NET makes a perfectly good host. You can either write your own Windows Service to act as a host, or you can take advantage of a readily available Windows Service, IIS, and consequently ASP.NET. WCF and ASP.NET may co-exist on a single machine in two different modes—side-by-side mode and ASP.NET compatibility mode. Here’s a rundown of these two modes.


Side-by-Side Mode


When running in side-by-side mode, WCF services hosted by Internet Information Services (IIS) are co-located with ASP.NET applications composed of .ASPX files and ASMX files (and ASCX and ASHX files when necessary). ASP.NET files and WCF services reside inside a single, common Application Domain (AppDomain). When run this way, ASP.NET provides common infrastructure services such as AppDomain management and dynamic compilation for both WCF and the ASP.NET HTTP runtime. WCF runs in side-by-side mode with ASP.NET by default.


When running in side-by-side mode, the ASP.NET runtime manages only ASP.NET requests. Requests meant for a WCF service go straight to the WCR-based service. Although the ASP.NET runtime does not participate in processing the requests, there are some specific ramifications of running in side-by-side mode.


First, ASP.NET and WCF services can share AppDomain state. This includes such items as static variables and public events. Although it shares an AppDomain with ASP.NET, WCF runs independently—meaning some features you may count on when working with ASP.NET become unavailable. Probably the major restriction is that there’s no such thing as a current HttpContext from within a WCF service (despite WCF’s architectural similarity to ASP.NET’s runtime pipeline). Architecturally speaking, WCF can communicate over many different protocols, including but not limited to HTTP, so an HTTP-specific context may not even make sense in many given scenarios. Second, authentication and authorization can get a bit tricky.

Even though WCF applications do not interfere with ASP.NET applications, WCF applications may access various parts of the ASP.NET infrastructure such as the application data cache.


ASP.NET Compatibility Mode


WCF is designed primarily to unify the programming model over a number of transports and hosting environments. However, there are times when a uniform programming model with this much flexibility is not necessary and the application may desire or even require some of the services provided by the ASP.NET runtime. For those cases, WCF introduces the ASP.NET compatibility mode. WCF’s ASP.NET compatibility mode lets you run your WCF application as a full-fledged ASP.NET citizen, complete with all the functionality and services available through ASP.NET.


WCF services that run using ASP.NET compatibility mode have complete access to the ASP.NET pipeline and execute through the entire ASP.NET HTTP request life cycle. WCF includes an implementation of IHttpHandler that wraps WCF services and fosters them through the pipeline when run in ASP.NET compatibility mode. In effect, a WCF service running in ASP.NET compatibility mode looks, tastes, and feels just like a standard ASP.NET Web service (that is, an ASMX file).


WCF applications running under the ASP.NET compatibility mode get a current HttpContext with all its contents—the session state, the Server object, the Response object, and the Request object. WCF applications running as ASP.NET compatible applications may secure themselves by associating Windows Access Control Lists (ACLs) to the service’s .svc file. In this manner, only specific Windows users could use the WCF service. ASP.NET URL authorization also works for WCF applications running as ASP.NET compatible applications. The pipeline remains arbitrarily extensible for WCF applications running as ASP.NET applications because service requests are not intercepted as with the general purpose side-by-side mode—they’re managed by ASP.NET for the entire request life cycle.

You can turn on WCF’s ASP.NET compatibility mode at the application level through the application’s Web.config file. You can also apply ASP.NET compatibility to a specific WCF service implementation.


Creating an ASP.NET-based WCF Service


Visual Studio 2008 provides a template for crating an ASP.NET-based WCF Service. To create an ASP.NET-based WCF application, select New from the File menu, and then select Website from the main menu. You’ll see a list of available Web Site projects, including a WCF project:


Visual Studio will create a WCF Site for you. However, instead of including .aspx or .asmx files, the project contains a service description file (Service.svc), an interface definition (IService.cs) and implementation (Service.cs) within the App_Code folder.


Open the IService. cs file. You’ll find a simple default interface defined within. The interface represents the service’s contract. WCF requires that the interface be adorned with specific attributes. The interface itself needs the [ServiceContract] attribute. Specific methods require the [OperationContract] attribute. Rich data structures meant to be passed through the interface are adorned with the [DataContract] attribute, and members of the structure are adorned with the [DataMember] attribute.


public interface IService



   string GetData(int value);


   CompositeType GetDataUsingDataContract(CompositeType composite);



When you open the Service.cs file, you’ll see that Visual Studio creates a sample structure type named CompositeType so you can see how the data structure attributes work.



public class CompositeType


  bool boolValue = true;

  string stringValue = “Hello “;  [DataMember]

  public bool BoolValue


     get { return boolValue; }

     set { boolValue = value; }



  public string StringValue


     get { return stringValue; }

     set { stringValue = value; }



public class Service : IService


  public string GetData(int value)


  return string.Format(“You entered: {0}”, value);

 } public CompositeType GetDataUsingDataContract(CompositeType composite)


  if (composite.BoolValue)


    composite.StringValue += “Suffix”;


  return composite;




Visual Studio also pumps out a Web.config file that you may adjust if you decide to change the name of the service or specify different remoting parameter.

Consuming WCF Services

Consuming a WCF Service is very like consuming an XML Web Service. Visual Studio will generate a proxy for you. Visual Studio will build both synchronous and asynchronous versions of the proxy that match the signatures found in the service’s contract.


To create a client-side proxy for a WCF Service, right click on select “Add Service Reference” from Solution Explorer.




WCF unifies the remoting story for .NET.WCF separates decisions about the transport and wire format from the actual service. This allows for more decoupled systems that adhere to Service Oriented Architecture principles.

Thanks to Microsoft for providing this valuable information on their official site.



September 5, 2008 - Posted by | ASP.NET and WCF, Technology | , ,


  1. Thanks for sharing, it really helped me get started with WCF


    Comment by Kürşat Konak | March 24, 2011 | Reply

  2. What’s up, this weekend is nice designed for me, as this time i am reading this impressive informative post here at my home.

    Comment by check This out. more info | January 3, 2013 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: