一个REST Web服务创建一个WCF代理创建一个、REST、Web、WCF

2023-09-03 22:38:04 作者:往事如风过

我有一个复杂的WCF休息服务,采取多路输入和对象。我不能简单地把它做一个HTTP POST的小提琴手,因为有太多的数据来提供(我可以,但它会带我永远)。所以我想使用代理来做到这一点在code。有没有一种方法来生成一个.NET 4 WCF REST服务代理?否则,你有什么建议,让我轻松地测试服务?

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?

感谢。

推荐答案

有创建代理的WCF REST服务(没有WSDL的REST,一是新兴的标准,WADL没有标准的方式,没有被广泛采用, WCF不支持它)。出于测试目的,你应该分享与客户端的接口,既可以使用的ChannelFactory< T> - ,并设置适当的行为在工厂的端点属性的,或者使用辅助类WebChannelFactory< T>它会为你。

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.

假设接口称为ITEST,这是你有什么:

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));