Create a WCF Proxy for a Rest Web Service

2019-05-21 19:27发布

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 for a .NET 4 WCF Rest Service? Otherwise, what do you propose to allow me to easily test the service?

Thanks.

4条回答
Viruses.
2楼-- · 2019-05-21 19:31

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

查看更多
来,给爷笑一个
3楼-- · 2019-05-21 19:35

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");
查看更多
姐就是有狂的资本
4楼-- · 2019-05-21 19:50

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.

查看更多
小情绪 Triste *
5楼-- · 2019-05-21 19:54

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));
查看更多
登录 后发表回答