I have a complex WCF Rest service which take multiple inputs and objects. I cannot simply call it by doing an HTTP POST in Fiddler because there is too much data to provide (I could, but it will take me forever). So I would like to do it in code using a proxy. Is there a way to generate a proxy f开发者_Go百科or a .NET 4 WCF Rest Service? Otherwise, what do you propose to allow me to easily test the service?
Thanks.
There's no standard way of creating a proxy for a WCF REST service (there's no WSDL for REST, one emerging standard, WADL, isn't widely adopted, and WCF doesn't support it). For testing purposes, you should share the interface with the client, and either use the ChannelFactory<T> - and set the appropriate behavior in the factory's Endpoint property, or use the helper class WebChannelFactory<T> which does it for you.
Assuming that the interface is called ITest, this is what you'd have:
Uri serviceUri = new Uri("http://my.service.com/endpoint");
WebChannelFactory<ITest> factory = new WebChannelFactory<ITest>(serviceUri);
ITest proxy = factory.CreateChannel();
Assert.AreEqual(9, proxy.Add(4, 5));
Though there is currently no standard way to create a proxy with a WCF REST service, you can do this with the "Paste XML as Types" tool in the REST Starter kit. This works off the xml shown in the default WCF /help page and produces a C# class which matches the structure and can be used. Also, check out this video to see it in action - Consumer Twitter in 3 minutes.
You can create a proxy using the same steps you would for a non-RESTful WCF service:
// Create the proxy
ChannelFactory<IContract> channelFactory = new ChannelFactory<IContract>("endpointName");
var restfulProxy = factory.CreateChannel();
// Invoke a method
var response = proxy.MyRestfulMethod("param1", "param2");
svcutil.exe will create a proxy class that you can consume in your calling application that will allow you to call the appropriate methods and pass the parameters in code
精彩评论