I give up. It seems like too much effort to do this with WCF.
I've got the authentication working, but I had to jump through some hoops.
The service doesn't do basic authentication properly (no challenge
response, see
here<http://stackoverflow.com/questions/1462256/double-request-while-making-pox-rest-call-using-wcf-with-webhttpbinding-set-to-b>
-
from what I've read this seems to be common with Apache) so I had to
implement a IClientMessageInspector to manually add the authentication
header to each outbound message.
Next challenge, use a UriTemplate like "foo/{bar}/xyz" where {bar}
corresponds to an int parameter. There doesn't seem to be a way around
this - UriTemplate can only handle parameters typed as string in the path
section of the template, ints are ok in the query string. Only option
(since the server is out of my control) is to change the parameter type to
string.
Next challenge, handle errors. The service sends back most errors with
HTTP status code 400 and details are in the response stream. The response
stream is wrapped in a WebException that is wrapped in a Protocol
Exception. Ideally, I'd like to have my message inspector mentioned above
be able to inspect the response and somehow cause a corresponding fault /
exception to be thrown containing the detail provided in the response.
Next challenge, who knows - I've wasted enough time on this already...
On Wed, Jan 18, 2012 at 10:07 PM, Matt Siebert <[email protected]> wrote:
> Hi everyone,
>
> I have a requirement to consume a RESTful service in a .NET 4.0 app. I
> have some sample code provided by the client that uses WebRequests to talk
> to the service but I thought WCF would be a better fit since I'm using .NET
> 4.0.
>
> I saw the WCF Web API <http://wcf.codeplex.com/wikipage?title=WCF%20HTTP>on
> CodePlex, but the
> FAQ <http://wcf.codeplex.com/wikipage?title=WCF%20Web%20API%20FAQ> says
> it can't be used in production. Then there's it's predecessor, the REST
> Starter Kit Preview 2 <http://aspnet.codeplex.com/releases/view/24644>.
> The "Preview 2" bit doesn't fill me with confidence and I can't find
> anything that clearly indicates whether it can be deployed to end users in
> a desktop app. Maybe I need to look harder.
>
> I threw together a quick sample app making use of the WebGet attribute in
> System.ServiceModel.Web...
>
> [ServiceContract]
> public interface IService
> {
> [WebGet(UriTemplate = "api/projects")]
> void GetProjects();
> }
>
> class Program
> {
> static void Main(string[] args)
> {
> using (var channelFactory = new ChannelFactory<IService>(new
> WebHttpBinding(WebHttpSecurityMode.Transport), "https://test.example.com"))
> {
> channelFactory.Endpoint.Behaviors.Add(new WebHttpBehavior());
> var channel = channelFactory.CreateChannel();
> channel.GetProjects();
> }
> }
> }
>
> This appears to be making the proper call to the service but it requires
> basic HTTP authentication so it fails. I have the username and password
> but how do I set it?
>
> The other issue I have is how to process the data returned by this call
> (just using void above while I get it talking). I have documentation about
> the structure of the XML data returned but any call could also return an
> error which is a different structure. I think I read that I just need to
> create a data contract and it will be automagically deserialized, but I
> don't understand how to handle an error with a different structure that
> can't be deserialized with the same data contract.
>
> Is there a better way?
>
> Any help would be appreciated.
>
> Cheers.
>