Inject own reply data in WCF restful service - wcf

I want to use the content negotiation feature of restful WCF and when content-type==text/xml then return own my html data reply rather than the data that would be normally returned.
I think that it could be done when implementing IDispatchMessageFormatter and SerializeReply method.
Do You know other method to do this ?

Related

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.

WCF to consume other REST Services?

I'm trying to figure out if the best way to consume and use a 3rd party API that is REST is to use WCF or just use System.Runtime.Serialization.Json Namespace or the WebClient object in .NET and create my own methods to send and receive json objects to and from the REST service I'm consuming.
So far I've only seen consuming REST json of an existing WCF service. Can you use WCF to consume and work with (request/response) any json based REST service outside of .NET?
Yes, you can consume Flickr services using WCF as described here. You just need to change the ResponseFormat in the WebGet (and WebInvoke) attributes to be Json.
However, my experience is that it is rather painful when you deal with stuff like error handling or complex authentication schemes. I found it simpler to manually write the client using the WebRequest class.

Can I use Json serialization in WCF service?

I am going develop a WPF windows based application. I want to work with Entity Framework Self Tracking Entities and WCF. I was wondering if using Json is possible/recommended? If yes, please assist me; is there any tutorial that can help?
You can use the DataContractJsonSerializer to serialize the messages. You will have to use a REST based service (WebHttpBinding) as SOAP mandates XML as the message payload.
You can tell WCF to use the DatcontractJsonSerializer on the service side by settings in the WebGet and WebInvoke attributes but on the client side you will have to manually use this serializer as REST doesn;t have a metadata standard and therefore you have to create the requests and manage responses in a more manual fashion
Here is a reasonable guide to using Json and REST support in WCF
However, what is your driver to using Json? WCF is much more geared to SOAP based interaction currently (although the WCF 4.5 WebApi is going to address that to quite a degree). As your client is WPF you don't seem to gain alot from using Json

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/

How to pass Client Objects to WCF Service

I want to pass list of entitiy objects from Client to WCF Service,
but my WCF Service has no knowledge of the structure of these entity objects.
One way could be to pass them in an XML file.
What could be the other possible ways to pass such objects to WCF service?
Please guide.
Thank you!
Basically, you need to make your WCF service aware of the structure.
Remember: calling a WCF service is passing a message (WCF is serialising an object, stuffing it into an envelope, and sending it away; this is not a remote procedure call or some object remoting!) and you need to make this message so that the caller and the callee can serialize and deserialize it!
Create DataContracts for your object classes being sent back and forth - that's the easiest way.
You can also work with untyped messages in WCF - but it's a lot more manual work, and I'd strongly recommend investigating the DataContract route first!
See a blog post and the MSDN docs on how to deal with untyped messages in WCF.
Marc
I would recommend against this since WCF is contract based. I would map the entities onto DataContracts in the service and work with them from there. Let me know if I'm missing something..