Mike Taulty's Blog
Bits and Bytes from Microsoft UK
WebHttpBinding and WCF in the Orcas B1 Bits
Mike Taulty's Blog

Mike's Badges

Follow on Twitter
View mike's profile on slideshare
Add to Technorati Favorites
CW Blog Awards

I struggled a lot with the WebHttpBinding yesterday for WCF in the Orcas B1 bits so I thought that I'd share.

I find that if I have a simple project such as;

using System;
using System.ServiceModel;

[ServiceContract]
public class Service
{
  [OperationContract]
  public void Print(string s)
  {
    Console.WriteLine(s);
  }
}

class Program
{
  static void Main(string[] args)
  {
    ServiceHost host = new ServiceHost(typeof(Service));
    host.Open();
    Console.ReadLine();
  }
}

and I configure it with;

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Service">
        <endpoint address="http://localhost:9091/service"
                  binding="webHttpBinding"
                  contract="Service"/>
      </service>
    </services>
    <extensions>
      <bindingExtensions>
        <add name="webHttpBinding" type="System.ServiceModel.Configuration.WebHttpBindingCollectionElement, System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </bindingExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

Then I get an exception;

Additional information: Configuration binding extension 'system.serviceModel/bindings/webHttpBinding' could not be found. Verify that this binding extension is properly registered in system.serviceModel/extensions/bindingExtensions and that it is spelled correctly.

I spent ages on this - just couldn't figure it out until I worked out that if I alter the config file slightly (not to change anything you understand) by doing;

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="Service">
        <endpoint address="http://localhost:9091/service"
                  binding="webHttpBinding"
                  contract="Service"
                  bindingConfiguration="config"/>
      </service>
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="config"/>
      </webHttpBinding>
    </bindings>
    <extensions>
      <bindingExtensions>
        <add name="webHttpBinding" type="System.ServiceModel.Configuration.WebHttpBindingCollectionElement, System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </bindingExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

Then it works just fine - notice that I've simply added a binding configuration for webHttpBinding that doesn't do anything at all but it "feels" like the configuration code for webHttpBinding does not like the config file in the first instance whereas it likes the second one.

Taking it a bit further...if I then alter my service a little;

 

[ServiceContract]
public class Service
{
  [OperationContract]
  [HttpTransferContract(Method="GET")]
  public void Print(string s)
  {
    Console.WriteLine(s);
  }
}

Then I get an exception when I invoke it from IE with http://localhost:9091/service/Print?s=hello;

Additional information: The message with To 'http://localhost:9091/service/Print?s=5' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.

Now, I think this is expected because I haven't added the HttpTransferEndpointBehavior to my service description and if I do;

 

class Program
{
  static void Main(string[] args)
  {
    ServiceHost host = new ServiceHost(typeof(Service));

    HttpTransferEndpointBehavior transfer = new HttpTransferEndpointBehavior();

    host.Description.Endpoints[0].Behaviors.Add(transfer);

    host.Open();    
    Console.ReadLine();
  }
}

Then that works fine - what I haven't been able to find just yet is how I can add that HttpTransferEndpointBehavior via configuration - seem to have to add it via code right now unless Reflector is lying to me :-)


Posted Fri, May 18 2007 1:38 AM by mtaulty

Comments

» WebHttpBinding and WCF in the Orcas B1 Bits wrote &raquo; WebHttpBinding and WCF in the Orcas B1 Bits
on Fri, May 18 2007 2:30 AM
Softwaremaker wrote Threading and Throttling differences between WCF and ASMX
on Fri, Oct 12 2007 6:42 PM
I know I havent been posting deep technical stuff that I used to do. Contrary to what people think my
(C) Mike Taulty, 2009. All rights reserved. The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems