1

I have a .NET 4 WCF service (MEX and HttpGET).

For the HttpGET endpoint, I would like to override the default MessageFormatter.DeserializeRequest to map UriTemplate to strongly-typed objects.

Ideally, a custom attribute would be used to decorate the methods that should use this formatter, but I'm not sure whether I can switch the formatter in that regard.

Is this doable, and can someone walk me through the configuration needed in app.config?

Alfred
  • 800
  • 8
  • 16

1 Answers1

0

If you want fine grained formatting control for WCF REST, I'd suggest one of the following two options:

  1. Override WebHttpBehavior to specify your own message formatter. This gives you a lot of control, but requires a lot of legwork. http://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.getrequestclientformatter.aspx

  2. Use the new WCF Web API, which offers much more configurability for REST services. http://wcf.codeplex.com/wikipage?title=WCF%20HTTP.

However, if ALL you want to do is map certain query string parameters to strongly typed objects, you can just implement your own QueryStringConverter class :

http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.querystringconverter.aspx

and use that in your custom WebHttpBehavior: http://msdn.microsoft.com/en-us/library/system.servicemodel.description.webhttpbehavior.getrequestclientformatter.aspx ).

Jeff
  • 35,755
  • 15
  • 108
  • 220
  • Thanks for the links. 1. How do I wire up the configuration to specify to WCF to use my custom WebHttpBehavior? 2. The service needs to be available with the traditional mex endpoint as well. My understanding is that the WebAPI is only for Http. – Alfred Mar 15 '12 at 16:36
  • WebHttp/REST in general isn't meant to be used with a mex endpoint/WSDL. There is no WebHttp based WCF client. How are you intending to consume such a mex endpoint? – Jeff Mar 15 '12 at 16:40
  • Also, is there any way to determine the service method being called to determine which formatter to apply? – Alfred Mar 15 '12 at 16:43
  • It will be a windows service with both a mex endpoint and a webhttp endpoint. I have it working with the out-of-the-box GET/POST requests. But use of the UriTemplate requires string method parameters. We would like to strongly-type the method signature, so I was thinking this can be done with a custom MessageFormatter. However, I'm not sure how to go about wiring up something like that. – Alfred Mar 15 '12 at 16:47
  • A mex endpoint is purely for metadata. It is not cross compatible with a REST endpoint. If you want to host a mex endpoint, you should probably be hosting a SOAP endpoint which the mex corresponds to... – Jeff Mar 15 '12 at 18:17
  • sorry, not mex I meant whatever corresponds to wsHttpBinding...ws?. The REST interface would expose the same service methods for external clients. Need to be able to construct a friendly API and map the objects appropriately. Do you know of any examples for configuring the service in such a way? – Alfred Mar 15 '12 at 18:41
  • ohh. Ok. So you want two endpoints. One with WebHttpBinding and one with WsHttpBinding (though I'd stick to BasicHttpBinding for SOAP if it were me). You can add multiple endpoints with different bindings for the same service as described here (http://stackoverflow.com/questions/3036012/expose-webhttpbinding-endpoint-in-a-wcf-service). And you can use a custom QueryStringConverter for your WebHttp endpoint as described here (http://blogs.msdn.com/b/carlosfigueira/archive/2011/08/09/wcf-extensibility-querystringconverter.aspx) – Jeff Mar 15 '12 at 19:18
  • The QueryStringConverter is per key-value pair. I'd like to intercept the message before the querystring is deconstructed with the Formatter. Reason is to be able to map the parameters, not just type conversion. – Alfred Mar 16 '12 at 15:29