Custom WCF MessageFormatter - wcf

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?

If you want fine grained formatting control for WCF REST, I'd suggest one of the following two options:
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
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 ).

Related

Injecting Services In Deserialized Data Contract Proxies

I'm using Autofac with WCF integration in a project. I'm trying to figure out a way to lazy initialize data contract proxy collection properties to avoid transferring entire object graphs across the wire.
My current plan is to inject the WCF service in each deserialized data contract so they call the service, get the collection property data and initializes it.
My question is: Is there a way to tell Autofac to inject services in each data contract proxy deserialized at the client? Like some tweaking at the DataContractSerializer or something.
(No service locator, please...)
Thanks!
Unfortunately there is nothing like this available "out of the box" with Autofac. There is something similar in Autofac's MVC integration, but that's because MVC has a more specific integration point for that sort of thing (IActionInvoker).
You might be able to write a custom client-side behavior that intercepts certain known types (like collections) on the client and swaps in a lazy-initialized collection. There's a similar question here asking about how to swap the DataContractSerializer out at runtime. You could use a mechanism like that.

How to add REST support in existing server ?

I wrote some WCF server that support SOAP.
Now, i need to add some new request ... to add support in REST in some of the method that are supported SOAP.
I don't know how to do it.
Actually one of those method need to change to support REST.
How to do it ?
Add WebHttpBinding and a new service contract interface with the relevant REST methods annotated with UriTemplate. Encapsulate your business logic in a class that is used by soap service class and the rest service class both.

Validating parameters with WCF, Unity and VAB

I am developing an application that exposes a WCF service using the Message/Response pattern for service methods. The application is using Unity 2.0 for dependency injection and the Validation Application Block from MS Patterns & Practices. I've already gotten Unity tied into WCF using a custom HttpModule I picked up from several website a while back and everything works great.
In my service interface I have a method such as:
DoSomethingResponse DoSomething(DoSomethingRequest request)
I can easily attach VAB attributes to the service contract to verify that 'request' is never null but I also want to validate the contents of the request object.
To do this, I inject the validator into the DoSomethingRequest constructor and include an internally scoped IsValid property which handles interacting with the VAB validator. Unfortunately, this constructor doesn't get called because WCF deserializes the object and constructors aren't used.
Without getting into the merits of having the request object be a simple DTO versus having some server-side business logic, is there a way to cleanly inject dependencies into an object passed into WCF service as an argument?
If I'm understanding your issue correctly, you have properties on DoSomethingRequest that are instances of some other classes (data contracts) and you want to validate your data contracts as well? Is there some reason you can't just apply validation attributes to your data contract classes as well? This is the approach I've used when using WCF with VAB integration and it's worked out quite nicely.
So it turns out that adding the validation attributes to my DataContract actually works with no additional code. Unfortunately, it doesn't work if validation is defined in the app's config file (app.config or web.config).
As a result, I've stripped out the constructor injection and IsValid property on my DataContract (request object) which makes it more of an annotated DTO which I think is preferred anyway. I only wish that it would work the same with the XML configuration.

How to change Wcf to use a different serializer?

By default WCF use DataContractSerialization so if we can change it then my question is how to change it and when should we need which serialization on wcf ?
You can use the XmlSerializerFormatAttribute attribute on your service contract to force WCF to use the XmlSerializer.
WCF has a nice feature that a method can return Message or a Stream (see Returning raw json (string) in wcf and How to set Json.Net as the default serializer for WCF REST service as examples). The corresponding code which you need to write can be more easy as if you will use more advance techniques Extending Encoders and Serializers. So it is very easy to implement Streaming Message Transfer for example or just returning JPG or Excel file as a result of some WCF method.
The default choice of DataContractSerializer is good for most purpose. You can also use the DataContractJsonSerializer specially for REST type services and if the client expects Json content type. The other option is XmlSerializer for interoperability purpose if you need more control over the generated XML. DataContractSerializer is more efficient than XmlSerializer.
In 3rd party options you can use protobuf-net from Google which is more efficient than DataContract Serializer.

WCF: parameters handled in custom channel not present in generated WSDL

I have some special parameters to all my wcf service methods that are handled inside a custom channel and are not exposed in the service method parameter list. This works fine for json/xml endpoints, but the I don't know how to use a SOAP endpoint with this setup because the generated WSDL doesn't include fields that are not in the service call parameter list.
Is there a way I can centralize the handling of the special parameters that apply to all service methods (authentication, locale and other contextual information) and provide a SOAP endpoint that Just Works (tm)?
Hand editing wsdl files is not an option.
Provide something that implements IWsdlExportExtension to modify the WSDL as it is generated to contain the extra information you want. (Your custom channel BindingElement might be a good place to do this).