如何使用 WCF 异步消费基于 REST 的服务?
我想创建一个支持针对基于 REST 的服务的异步操作的代理类。
为了便于讨论,假设我有一个服务,IService
:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "{a}/{b}")]
void Action(string a, string b);
}
我可以轻松地创建一个同步代理类:(
class Client: ClientBase<IService>, IService
{
public void Action(string a, string b)
{
Channel.Action(a, b);
}
}
该技术来自此 文章)
是否有类似的直接制作方法代理支持异步操作(BeginAction
/EndAction
或 ActionAsync
模式)?或者是我自己手动推出的最佳实践?
请注意,我无法在 Visual Studio 中添加服务引用,因为没有可用的元数据。
I want to create a proxy class that supports asynchronous operations against a REST-based service.
For the sake of discussion, let's say I have a service, IService
:
[ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate = "{a}/{b}")]
void Action(string a, string b);
}
I am able to easily create a synchronous proxy class with:
class Client: ClientBase<IService>, IService
{
public void Action(string a, string b)
{
Channel.Action(a, b);
}
}
(this technique is from this article)
Is there a similarly straightforward way to make the proxy support asynchronous operations (either the BeginAction
/EndAction
or ActionAsync
patterns)? Or is the best practice to roll my own manually?
Note that I am unable to add a Service Reference in Visual Studio because there is no metadata available.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果将操作合约替换为相应的开始/结束对,它也应该适用于基于 REST 的合约。您甚至可以在客户端上同时拥有同步和异步版本的操作(但在这种情况下,您只需要在同步版本上拥有 [WebGet] 属性)。
If you replace the operation contract with a corresponding Begin/End pair, it should work for REST-based contracts as well. You can even have both sync and async versions of the operation on the client (but in this case you need to have the [WebGet] attribute on the synchronous version only).