Elczar Adame's Shared Points on SharePoint


 

WCF in Windows Server 2008

Part I of II : HTTP Transport

Groundwork

This piece intends to share with the community an elemental and high-level thought on Windows Communication Foundation in Windows Server 2008. It is composed of two parts, where Part I focuses on HTTP transport, and Part II focuses on non-HTTP transports implementation.

Application Server Foundation

Application Server Foundation, or also known as Application Server Core, is constituted of Application Server technologies provided in Windows Server 2008. The Application Server Core adjoins Microsoft .NET Framework 3.0, in addition to the baseline of .NET Framework 2.0. As an expanded server role that offers an integrated environment for business application, the Application Server is a composite of the following components:

1.    Windows Communication Foundation (WCF)

2.    Windows Workflow Foundation (WF)

3.    Windows Presentation Foundation (WPF)

Windows Communication Foundation is drawn to put forward a unified programming shape to intercommunicate and interoperate with other applications through a new service-oriented programming model. Moreover, Windows Workflow Foundation is the programming model and engine for developing workflow-enabled applications, while Windows Presentation Foundation is designed for building Windows smart-client applications.

Windows Communication Foundation

Windows Communication Foundation is a runtime and a set of APIs designed for sending and receiving messages across applications. As we have mentioned, it is a service-oriented programming model for intercommunication and interoperation with other systems, and considered as a great facilitator of SOA.

Based on concept of message-based communication, the Windows Communication Foundation is architecturally layered into contract, service runtime, messaging, activation and hosting.

To appreciate these layers, we need to put into consideration that communication in Windows Communication Foundation happens through endpoints. An endpoint contains of address, binding, and contract. An address is a network location where the endpoint is located, a binding defines how the endpoint communicates, and a contract defines what the endpoint is communicating.

In the topmost layer of Windows Communication Foundation are the Contracts. This layer provides a high-level APIs including data contract, message contract, service contract, and the policy and binding of our service, where the data contract defines the message parameter, the message contact characterizes every message part, the service contact delineates the service method signature, and the policy and binding specifies the rules of communication. Being defined in XSD document, every message parameter of a service can be exposed to or consumed by any system that understands XML.

The Service Runtime defines the runtime behavior of the service, including throttling behavior, error behavior, metabase behavior, instance behavior, message inspection, transaction behavior, dispatch behavior, concurrency behavior, and parameter filtering of the service.

While a service message is communicated between endpoints, the Messaging stratum is composed of channels, also know channel stack, that processes this message. Zooming in on this layer, we have the transport protocols (HTTP, TCP, PIPE, and MSMQ), encoding (Binary, MTOM, Text, and XML), security and reliability (WS-Security, and WS-Reliability).

Lastly, a service could be a self-hosted service or can be hosted by an agent, the Internet Information Services. In its Hosting and Activation architectural layer, a WCF application is also designed run as an executable, a Windows service, or a COM+ component.

WCF and IIS 7.0

In hosting a distributed application built in Windows Communication Foundation, we need the services of Internet Information Services 7.0. The IIS 7.0, as the Web server of Windows Server 2008, employs Windows Process Activation Service (WAS) to facilitate activation and communication over network protocols supported by WCF – e.g. HTTP, NET.TCP, NET.PIPE, and NET.MSMQ.

For a brief paper on Internet Information Services 7.0, please click here.

Demonstration

To complete this piece, we are going to walk in the course of HTTP implementation and we will attempt to ponder on non-HTTP implementation on my subsequent post. With the following considerations, we will be creating a WCF Service that defines our business logic and a WPF Windows Application that references our service.

1.    We have installed Application Server and Web Server roles on our Windows Server 2008.

2.    We have activated the IIS 6 Metabase Compatibility service of our Web Server.

3.    For our purpose, we will be using the default components of WCF Service and Windows Application (WPF) templates in Microsoft Visual Studio 2005 as installed through WCF and WPF extensions.

 

Now, we will start by creating our WCF Service.

1.       Let us open our Microsoft Visual Studio 2005. Start > All Programs > Microsoft Visual Studio 2005 > right-click Microsoft Visual Studio, then click Run as Administrator.

2.       To create our WCF Service, let us click File > New > Web Site.

3.       In the New Web Site dialog box, let us select WCF Service template, and our location to http://localhost/WCFService.  Below is the illustration.

 

  New

 

4.       If you have not installed some of necessary Application Server and Web Server services, you will be prompted by a message as illustrated below.

 

Error

 

 

5.       Our WCF Service consists of contact, an interface class, and a configuration file that defines the services and service behaviors. And to make it personable, we will edit some items as:

 

In the Service.cs file:

[ServiceContract()]

public interface IService

{

    [OperationContract]

    string Operation(string Value);

    [OperationContract]

    string DataOperation(Profile Value);

}

 

public class Service : IService

{

    public string Operation(string Value)

    {

        return "Hello: " + Value;

    }

    public string DataOperation(Profile Value)

    {

        return "Hello: " + Value.FirstName + " " + Value.LastName;

    }

}

[DataContract]

public class Profile

{

    string firstname;

    string lastname;

 

    [DataMember]

    public string FirstName

    {

        get { return firstname;}

        set { firstname = value;}

    }

    [DataMember]

    public string LastName

    {

        get { return lastname;}

        set { lastname = value;}

    }

}

 

In the Service.svc file:

<% @ServiceHost Language=C# Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>

 

In the web.config file:

<system.serviceModel>

    <services>

      <service name="Service" behaviorConfiguration="returnFaults">

        <endpoint contract="IService" binding="wsHttpBinding"/>

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="returnFaults" >

          <serviceDebug includeExceptionDetailInFaults="true" />

              <serviceMetadata httpGetEnabled="true" />

            </behavior>

        </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

6.       Now its time for us to build of service. If it is successfully built, let us browse http://localhost/WCFService/Service.svc and we expect a page, as illustrated, that notifies us the metabase publishing for this service is currently disabled.

Metabase

7.       To enable the metabase publishing of our service, we need to add <serviceMetadata httpGetEnabled="true" /> inside the <system.serviceModel> tag of our configuration file. It would result as

 <behaviors>

      <serviceBehaviors>

        <behavior name="returnFaults" >

          <serviceDebug includeExceptionDetailInFaults="true" />

              <serviceMetadata httpGetEnabled="true" />

            </behavior>

        </serviceBehaviors>

 </behaviors>

8.       By browsing again our WCF service at http://localhost/WCFService/Service.svc, we will notice that it is already ready to be referenced.

 

At this time, as the final step, we will create our WPF Windows Application that references our WCF Service.

1.       Let us open our Microsoft Visual Studio 2005. Start > All Programs > Microsoft Visual Studio 2005 > right-click Microsoft Visual Studio, then click Run as Administrator.

2.       To create our WCF Service, let us click File > New > Project.

3.       In the New Project dialog box, let us select Windows Application (WPF) template, and name it WPFClient. Below is the illustration.

4.       Then, we will add a service reference pointed to the WCF Service we have created. Right-click our solution in the Solution Explorer, then click Add Service Reference. Write http://localhost/WCFService/Service.svc  in the Service URL  and name it Proxy. Below is the illustration.

 

Reference

 

9.       Add Button and name it as ButtonRetrieve.

10.  In our XAML file, let us add Click="ButtonClick" inside our <Button> tag.

11.   Lastly, we will add this method in the xaml.cs file of our WPF application:

 

void ButtonClick(object sender, RoutedEventArgs e)

        {

       WPFClient.Proxy.ServiceClient service = new WPFClient.Proxy.ServiceClient();

            string Message = service.Operation("Paul");

 

            MessageBox.Show(Message);

  }

 

12.   Let us build our application and we will have a WPF application that consumes a WCF service.

And that’s it. Hope to share you something.

Advertisements

Comments on: "WCF in Windows Server 2008 – Part I" (4)

  1. nice article.

  2. Thanks Arthur for the appreciation…

  3. nice article. what syntax highlighting do you use? it\’s kinda cool!

  4. Thanks randz. Hope it may serve as your reference in hosting WCF service in IIS 7.0.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Tag Cloud

%d bloggers like this: