Monday, February 28, 2011

How to Debug WCF

I’m trying to share you a problem we got while consuming WCF Service and how did we solve
it. Which will help you debugging WCF.

While consuming WCF service we got error message “The remote server returned an
unexpected response: (400) Bad Request.”.

In that Message I didn’t understand what Bad means. It would be helpful if we are having more detailed information on what Bad means?

To know this we need to enable tracing at WCF Service end.
To enable tracing add below config entries in your service config file under configuration.
      <source name="System.ServiceModel" switchValue="Warning">
          <add name="WcfLogSample" />
      <add name="WcfLogSample" type="System.Diagnostics.XmlWriterTraceListener"
             initializeData="D:\log\Traces2.svclog" traceOutputOptions="Callstack" />
   <trace autoflush="true" />

Then try to reproduce the error from client again and open File "D:\log\Traces2.svclog" .where you can see detailed error message  as below

“The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element.”"

Then simply go ahead and increase value MaxReceivedMessageSize. Now the problem is solved

What I’m trying to say here is, make your life easy while debugging by enabling tracing
in WCF when we get weird error.

Hope this helps

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.

            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
<endpointcontract="IMetadataExchange"binding="mexHttpBinding"address="mex" />

<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<!-- 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 -->
<serviceHostingEnvironmentmultipleSiteBindingsEnabled="true" />

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