简体   繁体   English

wcf单向非阻塞操作

[英]wcf oneway non blocking operation

I need such scenario: client sends message to server, not waiting for response, and don't care, if message was send properly. 我需要这样的场景:客户端将消息发送到服务器,而不等待响应,并且不在乎消息是否正确发送。

using(host.RemoteService client = new host.RemoteService())
{
client.Open();

cliend.SendMessage("msg");
}

in scenario when firewall is on, or there is no connection to the internet, client dies at "SendMessage". 在启用防火墙或没有Internet连接的情况下,客户端会在“ SendMessage”处死亡。 I mean program stops to respond. 我的意思是程序停止响应。 I wish program don't care about the result. 我希望程序不在乎结果。 I mean if there is no connection, i wish program to go further, omitting "SendMessage" or sth like that. 我的意思是,如果没有连接,我希望程序能走得更远,省略“ SendMessage”或类似的内容。

What should I do, is there any solution for non blocking method? 我该怎么办,非阻塞方法有解决方案吗?

Try something like this in your service contract: 在您的服务合同中尝试以下操作:

[OperationContract(IsOneWay=true)]
void Send(string message);

See the following link: 请参阅以下链接:

One Way Operation in WCF WCF中的单向操作



Edit: OP was already using my suggested solution. 编辑:OP已经在使用我建议的解决方案。

Suggested approaches to solve the issue - taken from MSDN ( One-Way Services ): 建议的解决问题的方法-摘自MSDN( 一站式服务 ):

Clients Blocking with One-Way Operations 客户阻止单向操作

It is important to realize that while some one-way applications return as soon as the outbound data is written to the network connection, in several scenarios the implementation of a binding or of a service can cause a WCF client to block using one-way operations. 重要的是要意识到,尽管某些单向应用程序将出站数据写入网络连接后立即返回,但是在某些情况下,绑定或服务的实现可能导致WCF客户端使用单向操作进行阻塞。 In WCF client applications, the WCF client object does not return until the outbound data has been written to the network connection. 在WCF客户端应用程序中,只有将出站数据写入网络连接后,WCF客户端对象才会返回。 This is true for all message exchange patterns, including one-way operations; 对于所有消息交换模式(包括单向操作)都是如此。 this means that any problem writing the data to the transport prevents the client from returning . 这意味着将数据写入传输中的任何问题都会阻止客户端返回 Depending upon the problem, the result could be an exception or a delay in sending messages to the service. 根据问题,结果可能是异常,也可能是向服务发送消息的延迟。

You can mitigate some of this problem by inserting a buffer between the client object and the client transport's send operation. 您可以通过在客户端对象和客户端传输的send操作之间插入缓冲区来缓解某些问题。 For example, using asynchronous calls or using an in-memory message queue can enable the client object to return quickly . 例如, 使用异步调用或使用内存中消息队列可以使客户端对象快速返回 Both approaches may increase functionality, but the size of the thread pool and the message queue still enforce limits. 两种方法都可以增加功能,但是线程池和消息队列的大小仍然会限制。

It is recommended, instead, that you examine the various controls on the service as well as on the client, and then test your application scenarios to determine the best configuration on either side. 相反,建议您检查服务以及客户端上的各种控件,然后测试您的应用程序方案,以确定双方的最佳配置。 For example, if the use of sessions is blocking the processing of messages on your service, you can set the System.ServiceModel.ServiceBehaviorAttribute.InstanceContextMode property to PerCall so that each message can be processed by a different service instance, and set the ConcurrencyMode to Multiple in order to allow more than one thread to dispatch messages at a time. 例如,如果使用会话阻止了服务上消息的处理,则可以将System.ServiceModel.ServiceBehaviorAttribute.InstanceContextMode属性设置为PerCall以便可以由不同的服务实例处理每条消息,并将ConcurrencyMode设置为Multiple以允许多个线程在同一时间发送消息。 Another approach is to increase the read quotas of the service and client bindings. 另一种方法是增加服务和客户端绑定的读取配额。

修改属性

[OperationContract(IsOneWay=true)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM