How to add REST support in existing server ? - wcf

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.

Related

ASP.NET Web Api and Data Contracts

I want to create a client for this public WEB API. I am new to this and I was going through this MSDN tutorial. I noticed that there are no Data Contracts defined and i cannot add service reference in my project. In the tutorial in some point it says create this class (Product) as data object that HttpClient will use.
How can i find/create this class when using the public API. In WCF i can add service reference and i will get client classes created from the service data contract. How is this done in Web Api?
As illustrated in the MSDN article, since there's no defined contract (unlike WCF) you're gonna need to create the appropriate Types first in order to be able to consume it using REST client.
However, there are some shortcuts you can use, see here:
Generate Contracts for REST objects
Also, you may find RestSharp easier to use than the official WebApi client.

Using WCF Extensibility at Client Side

I am using a web application as a client to invoke WCF methods using proxy.
For each request being made by client object, I need to populate few properties (declared inside request class)
Is it possible to hook a method, just before making the actual web service call.
I can't modify service code right now, Can WCF extensibility points could be leveraged in this case?
Thanks for your help.
If you want to change the properties of the method parameters, you can use an IParameterInspector to do that, since at that point you'll get an array with all parameters to be sent to the server.
If you need to change other parts of the request (such as transport or SOAP headers), an IClientMessageInspector may be the best way to go.
For more information on many extensibility points at the client side, you can check the blog series at http://blogs.msdn.com/b/carlosfigueira/archive/2011/03/14/wcf-extensibility.aspx.

Custom WCF MessageFormatter

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 ).

WCF - how to add additional data to each call

I want to add a complex poco that will pass itself within each wcf call. Whats the bast practice for this case?
Typically, the best way to do something like this is passing such "meta-information" in a WCF header. You can easily create a message inspector to extend WCF (it's really not that scary and hard to do!) which would inject the POCO class (or what of it is necessary) into every outgoing request from the client, and retrieve it from the header and validate it on the server side.
There are a number of pretty good blog post out there showing you how to create a message inspector:
Richard Hallgren's WCF postings
Writing a WCF message inspector
Automatic Culture Flowing with WCF by using Custom Behaviour
Check out the two relevant interfaces to implement:
IClientMessageInspector on the client side, which has a BeforeSendRequest and AfterReceiveReply message to implement
IDispatchMessageInspector on the server side, which has a AfterReceiveRequest and BeforeSendReply method to implement
Here you go, check this out...
https://kinnrot.github.io/passing-complex-type-through-wcf/

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).