2

I'm trying to write WCF service in which one method will be catching all requests. Plan to host it within standalone executable. Here is the contract:

[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple, AddressFilterMode = AddressFilterMode.Any)]
public class Proxy
{
    [WebInvoke(UriTemplate = "*", Method = "*")]
    public string Test(Stream input)
    {
        return "Test";
    }
}

Here is the hosting code:

static void Main(string[] args)
{
    var uri = new Uri("http://localhost:2535/");
    var binding = new WebHttpBinding();

    var host = new ServiceHost(new Proxy(), uri);
    host.AddServiceEndpoint(typeof(Proxy), binding, uri);
    host.Open();
    Console.ReadKey();
}

But when I'm pointing my browser to the localhost:2535 i just see information about service and fact that metadata is not enabled. And when I getting something like localhost:2535/bla-bla-bla/ error rises:

The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).

I don't understand what I'm missing, to be frankly... Would be very grateful for helping me to get back on right track.

EDIT: Solved by explicitly adding WebHttpBehavior behavior to the endpoint. The resulting code become:

static void Main(string[] args)
{
    var uri = new Uri("http://localhost:2535/");
    var binding = new WebHttpBinding();

    var host = new ServiceHost(new Proxy(), uri);
    host.AddServiceEndpoint(typeof(Proxy), binding, uri).Behaviors.Add(new WebHttpBehavior());
    host.Open();
    Console.ReadKey();
}

I'm still looking for more detailed explanation why it's working that way...

shytikov
  • 9,155
  • 8
  • 56
  • 103

3 Answers3

1

Try add to your Endpoint's behaviour WebHttpBehavior, like this

host.AddServiceEndpoint(typeof(Proxy), binding, uri).Behaviours.Add(new WebHttpBehavior());
Taras Kravets
  • 1,443
  • 4
  • 14
  • 15
0

It looks a bit odd that your ServiceContract attribute is defined directly on the class that implements your service. Usually you'd define this on the interface that defines the service. Example here:-
MSDN ServiceContractAttribute

Nick Ryan
  • 2,662
  • 1
  • 17
  • 24
  • It is bad. But does it technically really matter? Does it not just only use reflection to parse the attributes? – Myrtle Jan 15 '13 at 14:36
  • 2
    I'm not sure, really you should have the method decorated with OperationContract as well. That could be the cause of your error. No service contract is 'claiming' your message. There is an SO article about this exception here: http://stackoverflow.com/questions/5487791/wcf-contractfilter-mismatch-at-the-endpointdispatcher-exception – Nick Ryan Jan 15 '13 at 14:48
0

To enable metadata exchange you need to add ServiceMetadataBehavior just like that

ServiceMetadataBehavior serviceBehaviour = new ServiceMetadataBehavior() { HttpGetEnabled = true, HttpGetUrl = new Uri(String.Format("{0}/mex", endpointUrl)) }; 
Host.Description.Behaviors.Add(serviceBehaviour);

And then use localhost:2535/mex to retrieve the service metadata. If it succeeds have a look if your Test method is included in the metadata. If it fails try to configure WCF tracing to get more detailed and user friendly error messages. Also make sure you marked you method with OperationContract attribute.

Hope it helps.

Maksym Strukov
  • 2,679
  • 1
  • 13
  • 17