none
Any effortless way to create a REST client?

    Question

  • Hi!

    I've been trying to find information about getting a nice concrete REST service client from ChannelFactory.

    As far as I've figured out, you can set attirubtes on a client interface that tells WCF to use XML (DataContract or XmlSerialization) or JSON for responses, and URL formatted HTTP get requests or HTTP post of XML/JSON.

    What I'd like is to put some attributes on the service interface telling WCF to create an URL formatted POST, without an XML header, and then get a response in XML. (For instance)

    A dream scenario would be this:

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

    And have client.Authorize actually do

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

    And deserialize

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

    As far as I've found out from googling and forum thrashing, there's no way to do this unless you do an immense amount of plumbing, writing interceptors, formatters and god knows what.

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

    Any hints to where I can find the simplest possible way to do this?

    (And please don't tell me to do a manual HttpRequest, that's why we've got WCF! ;) )


    Lars-Erik MCPD ASP.NET 2.0

    Friday, February 17, 2012 11:17 AM

All replies

  • Hi,

    I don't know if I can help you in your special scenario, but if you want to build WCF REST services you should have a look at the WCF Web API (since yesterday known as ASP.NET Web API). In my opinion that's the easiest way to create REST-based services with WCF. That implies that I recommend the HttpClient class instead of the ChannelFactory class. Here are two nice sites to learn more if that meets your needs:


    Best Regards. Please Mark Answered, If my solution solves your problem.

    Friday, February 17, 2012 1:45 PM
  • Thanks, I'll go read those at first chance, and possibly mark your post as answer. :)

    If anyone wants to add to this thread, I'd like to specify that the scenario is to have the WCF factor(ies?) create WCF clients for thirdparty REST services most likely not ASP.NET without having to write a lot of plumbing. (REST services are easy to create with asp.net anyway, it's the client API that is chaotic atm.)


    Lars-Erik MCPD ASP.NET 2.0

    Friday, February 17, 2012 2:15 PM
  •  That implies that I recommend the HttpClient class instead of the ChannelFactory class.

    Just a small addition:
    The quoted is part of what I don't want.
    The whole point of using the ChannelFactory is to abstract away the fact that I'm using HTTP.


    Lars-Erik MCPD ASP.NET 2.0

    Monday, February 20, 2012 10:57 AM
  • @Horizon_Net: I've read your links, and the second one mentions they are working on the WebClient class.

    This does not meet my expectations/requirements though, but I might end up having to use it.

    In the end it doesn't answer my question, so just leaving you with a helpful vote. ;)

    Thanks.


    Lars-Erik MCPD ASP.NET 2.0

    Monday, February 20, 2012 3:46 PM