Published
Friday, May 18, 2007 1:38 AM
by
mtaulty
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 :-)