Thursday, February 24, 2011

How to host WCF Service in WAS

Here I tried to illustrate how to host WCF Service in WAS with minimum required steps.

Prerequisites
            Need Windows server 2008, IIS7 and WAS
            Readers must be aware of WCF basics

Create Basic WCF Service

1.  Create New Project, select WCF Service Application as Template with Name “SampleWCFHosting”
2.  Replace below config entries in web.config file
<system.serviceModel>
<services>
<servicename="SampleWCFHosting.Service1"behaviorConfiguration="servicebehavior">
<endpointaddress="netPipe"contract="SampleWCFHosting.IService1"binding="netNamedPipeBinding"/>
<endpointaddress="netTcp"contract="SampleWCFHosting.IService1"binding="netTcpBinding"/>
<endpointaddress="basicHttp"contract="SampleWCFHosting.IService1"binding="basicHttpBinding"/>
<endpointcontract="IMetadataExchange"binding="mexHttpBinding"address="mex" />
</service>
</services>

<behaviors>
<serviceBehaviors>
<behaviorname="servicebehavior">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadatahttpGetEnabled="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 -->
<serviceDebugincludeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironmentmultipleSiteBindingsEnabled="true" />
</system.serviceModel>

3.  Build Solution and publish it as shown below


Install Service in WAS
1.  Open IIS Manager
2.  Expand Sites, right click and click on “Edit Bindings” of Web site under which you want to create application, then add entries as below

3.  Right click and click on “Add Application“ on above Web site and Alias and Physical path as below

4.  Right Click “WCFOnWAS” Application and click on Manage Applicationà Advanced Settings and Update Enabled Protocols with “http, net.tcp, net.pipe”

Now you have completed hosting WCF on WAS
Just to see whether our Service is accessible browse http://localhost/WCFOnWAS/Service1.svc

Test Service from Client
Let us test it using some client application
1. Create Console Application
2. Add service Reference usinghttp://localhost/WCFOnWAS/Service1.svc
3. After adding service reference app.config is updated with different bindings available
4. Add below code in Main to test WCF

//get Binding Name from app.config and pass as parameter for Service1Client
//Change different Binding Names to test different Bindings
ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client("NetTcpBinding_IService1");
string s = proxy.GetData(1);

Get different binding names from app.config and pass as parameter for serviceclient to test different bindings.

Hope this helps
Vital

4 comments:

  1. Very Good Posting..!! It will be more useful if you provide remaining types of hosting information including with the security settings also.

    ReplyDelete
  2. Thanks sreelaxmi..I will try to post something on security and how to choose between different hosting options

    ReplyDelete
  3. Really a good article which saved my time a lot. Thanks.

    ReplyDelete