WCF to consume other REST Services? - wcf

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.

Related

Should i use httphandlers or web services?

I have a simple scenario where i want to output only json to the user.Now should i use a simple httphandler, call it from jquery and get the json or create a WCf service?
I want to use WCf service but don't see any advantage, maybe someone can point few of them and scenarios.
Anything would be suffice. Your thoughts are in correct direction, why to create a wcf service and increase the overload(maintaince, deployment etc).
If you are using Ajax, Look in to PageMethods, which would even free you from creating a separate HTTPHandler and you could call your code behind methods directly in your javascript.
I am assuming you are using asp.net.

Inject own reply data in WCF restful service

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 ?

Exposing data from WCF services as oData

Is it possible to use oData with a WCF service application but not use WCF Data Services?
It will be great if someone could shed more light on oData. I have done some Googling on this topic, but whenever I search for "wcf odata", I get information about WCF Data Services.
Any help/links will be appreciated.
WCF Data Services is the Microsoft implementation of the general OData protocol. As such, only WCF Data Services are / support / implement OData - a "normal" WCF service does not (and can not).
You might need to elaborate exactly why you feel the need or urge to use OData but not use WCF Data Services. What's the issue / problem you have with that setup? WHY do you want to use only a "normal" WCF service??
Update: ok, so you want to have services that expose data in different fashions and with different methods. What you could do is create a regular WCF service that's exposing both SOAP endpoints as well as a webHttpBinding REST endpoint. This will work - but then it's a "regular" WCF service, with methods that take parameters and return some data structure. This is not WCF Data Service (OData).
OData is more of a "here's my data collection, you can browse around in it" kind of approach - it's more about exposing an entire data model to the outside world using REST. This doesn't mix and match with SOAP - which is a lot more procedure-oriented, e.g. LoadCustomer, SaveInvoice and so forth.
So while it's absolutely possible to have procedure-oriented WCF services exposing both SOAP and REST endpoints at the same time, I don't really see how you can mix and match the "expose-this-resource" kind of approach for WCF Data Services / OData with a SOAP binding - this just doesn't work, I believe.
So if you must expose your data model of WCF Data Services (OData), you will need to author a second, pretty different looking regular WCF services for the SOAP clients, that might be based on the same data in the end (access the same database, for instance), but it's "face" will look quite different.
There is a new(ish) project from Microsoft called WCF WebApi (NuGet Package), which is simplifying doing RESTful WCF. It really takes to heart the "Representation" part of REST, so that you can expose your data in many representations (JSON, XML, oData, PNG, etc) based on content negotiation (or optionally whatever convention you want) all from the same service contract operation. Currently baked into the framework is the ability to support oData GET queries, by simply returning a collection .AsQueryable().
While this isn't a pure WCF/oData solution, as your question is speaking to, I thought I would mention the project in case it fit your specific requirements. The current Go-Live licensing might prove prohibitive, as might the "preview" status of this code; but if not, this may be a solution for you.
* UPDATE * This project was rolled into ASP.NET (usually in conjunction with ASP.NET MVC) and is no longer under the WCF team. The new product is called ASP.NET WebAPI.

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

There is any WCF binding that accepts the data in a HTTP query string?

There is any WCF binding that accepts the data in a HTTP query string?
Well, WCF also supports REST, which sort of allows you to specify the parameters for your call in the URL.
MSDN WCF REST Developer Center - tons of goodies, including screen casts
An Introduction To RESTful Services With WCF
The new WCF Web Programming Model supports REST Design
Is that what you're looking for? I don't think there's any standard way to package up a SOAP envelope on the query string.....
webHttpBinding might be what you want... Clicky