微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何使用WebInvokeAttribute异步调用WCF服务方法?

我有这种方法的WCF服务.该方法具有WebInvoke属性.我怎么能异步调用它?

[WebInvoke(UriTemplate = "*",Method = "*")]
public Message HandleRequest()
{
    var webContext = WebOperationContext.Current;
    var webClient = new WebClient();

    return webContext.CreateStreamResponse(webClient.OpenRead("http://site.com"),"text/html");
}

解决方法

您可以通过将以下值一起传递给ServiceBehavior属性来为服务类定义异步行为:

> InstanceContextMode = InstanceContextMode.Single,
> ConcurrencyMode = ConcurrencyMode.Multiple.

生成代码可能如下所示:

[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyService
{
    [WebInvoke(UriTemplate = "*",Method = "*")]
    public Message HandleRequest()
    {
        var webContext = WebOperationContext.Current;
        var webClient = new WebClient();

        return webContext.CreateStreamResponse(webClient.OpenRead("http://site.com"),"text/html");
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐