Create a WCF Proxy for a Rest Web Service

≡放荡痞女 提交于 2019-12-06 15:22:00

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!