Any easy way to create a WCF REST client to send URL requests?

This is a duplicate of http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/94503037-9cc4-494d-88af-4e97fcb9cdcc , but I did not have much success getting fruitful answers there. :(

I am trying to find information about getting a specific REST service client (proxy) from ChannelFactory.

As I understand it, you can install attirubtes on a client interface that tells WCF to use XML (DataContract or XmlSerialization) or JSON for responses, and HTTP requests with HTTP URLs or HTTP XML / JSON messages. [edit from original] And, of course, providing various binding configurations either programmatically or via config. [/ edit from original]

I would like to add some attributes to the service interface telling WCF to create a POST with a formatted URL, without an XML header, and then get a response in XML. (For example) [edit from original] And / or providing BCL bindings that support follosing [/ edit from original]

The sleepy scenario will be as follows:

public interface IRestClient { [WebInvoke] AuthResponse Authorize( [HeaderParameter] string someHeader, string someData, int someInt) } // ... var client = channelFactory.CreateChannel<IRestClient>(); // whatever bindings I'd need for this var response = client.Authorize("abc", "def", 123); 

And you have a client. Authorize actually

 HTTP POST /authorize someHeader: abc someData=def&someInt=123 

And deserialize

 <AuthResponse> <Message>Hi there!</Message> </AuthResponse> 

As far as I learned from googling searches and the forum, there is no way to do this unless you are doing a huge amount of plumbing, writing interceptors, formers and gods who know what.

WCF contribution library, WCF samples, etc. et al. is too academic and has too much plumbing and / or too little intuitive documentation for me to do this without pain. :)

Any clues where I can find the easiest way to do this?

(And please do not tell me to use HttpRequest or WebClient , so we have ChannelFactory ))

[update]

Correct me if I am wrong, but as I understand it, many so-called REST APIs require URL formatted requests, not Json or Xml.

The interpretation of WCF web interfaces in service descriptions reflects the WebGet and WebInvoke in method definitions to determine how each "method" is called in "services". (Let me call it a method and service for simplicity)

The WebInvoke attribute takes the value of the WebRequestFormat enumeration to determine how to serialize requests. In this listing, only Json and Xml.

It seems really stupid to me that this is an enumeration, and not some kind of pointer to a factory formatter / serializer, or even a specific one of these.

So, the question really comes down to the simplest, preferably already implemented, redefinition of query serialization.

[/ update]

+4
source share
2 answers

REST does not have a metadata standard. Thus, you will have to manually roll up your client code using one of the many client-side REST toolkits or the basic HTTP APIs in BCL. You can use WebChannelFactory to define interafce with WebInvoke, WebGet, but you have to write it yourself or take a copy of the service one.

As a rule, in the REST API, the API should be dynamically detected in terms of URIs, since resources should also return appropriate URIs and verbs that are applicable to the current state of your interaction or resource

+2
source

Make sure the request and response are in your favorite format.

 var client = channelFactory.CreateChannel<IRestClient>(); // whatever bindings I'd need for this System.ServiceModel.Description.WebHttpBehavior behaviour = new System.ServiceModel.Description.WebHttpBehavior(); behaviour.DefaultOutgoingRequestFormat = System.ServiceModel.Web.WebMessageFormat.Json; behaviour.DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json; client.Endpoint.Behaviors.Add(behaviour); m_Proxy = client.CreateChannel(); var response = m_Proxy.Authorize("abc", "def", 123); 
0
source

Source: https://habr.com/ru/post/1397454/


All Articles