C# 如何异步调用我的 WCF 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/400798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:02:10  来源:igfitidea点击:

How to make a call to my WCF service asynchronous?

c#.netwcfwindows-services

提问by Blankman

I have a WCF service that I call from a windows service.

我有一个从 Windows 服务调用的 WCF 服务。

The WCF service runs a SSIS package, and that package can take a while to complete and I don't want my windows service to have to wait around for it to finish.

WCF 服务运行一个 SSIS 包,该包可能需要一段时间才能完成,我不希望我的 Windows 服务必须等待它完成。

How can I make my WCF service call asynchronous? (or is it asynchronous by default?)

如何使我的 WCF 服务调用异步?(或者默认是异步的?)

采纳答案by Perpetualcoder

All your needs will be satisfied in the following articles from MSDN:

MSDN 的以下文章将满足您的所有需求:

Implementing an Async Service Operation

实现异步服务操作

Calling WCF Service Async

调用 WCF 服务异步

Designing Service Contracts

设计服务合同

回答by Andrew Harry

the WCF Proxy inside of your client (Windows Service?) needs to be specified at creation that you wish to have Asynchronous operations available.

您的客户端(Windows 服务?)内的 WCF 代理需要在创建时指定,您希望异步操作可用。

You can modify an existing WCF Proxy by right clicking on it and choosing 'Configure Service Reference' From here you just need to check the tick box next to 'Generate asynchronous operations'

您可以通过右键单击它并选择“配置服务引用”来修改现有的 WCF 代理,从这里您只需选中“生成异步操作”旁边的复选框

Ok so that is the WCF Proxy side of things taken care of. Now you need to use APM (Asynchronous Programming Model) with the Proxy inside of your client.

好的,这就是 WCF 代理方面的事情。现在您需要在客户端内部使用带有代理的 APM(异步编程模型)。

回答by Bahamut

On Visual Studio 2010, on the Add Service Reference > click Advanced button > check the Generate Asynchronous Operationscheckbox.

在 Visual Studio 2010 上,在Add Service Reference > click Advanced button > check the Generate Asynchronous Operations复选框上。

After doing so, the Async operations will be added and be available for your use.

执行此操作后,将添加异步操作并可供您使用。

回答by Stas BZ

Service side:

服务端:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    bool DoWork(int i);
}

Client side:

客户端:

[ServiceContract(Name = nameof(IMyService))]
public interface IMyServiceClient : IMyService
{
    [OperationContract]
    Task<bool> DoWorkAsync(int i);
}