简体   繁体   English

连接切断后如何在WCF可靠消息传递中从服务器接收响应

[英]How to receive a response from server in WCF reliable messaging after connection cuts

So I created Client/Server WCF. 所以我创建了Client / Server WCF。 What I want is when I send message to the server from this client and the connection cuts for any reason , client shuts down for example, how could this client get the response when it is availabe again ? 我想要的是当我从这个客户端向服务器发送消息并且因任何原因切断连接时,客户端关闭,例如,当客户端再次可用时,该客户端如何获得响应?

Is it possible to set a session or something like between the client and the server? 是否可以在客户端和服务器之间设置会话等?

My client code is : 我的客户代码是:

private static void Main(string[] args)
{
    var client = new FlipCaseServiceClient("ReliableMessageService");
    var sd = new StringData { FirstName = "Turgut", LastName = "Kançeltik" };

    var fullName = client.GetFullName(ref sd);

    Console.WriteLine(fullName);
}

My Server code is : 我的服务器代码是:

[DeliveryRequirements(RequireOrderedDelivery = true)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Single)]
public class FlipCaseService : IFlipCaseService
{
    public string GetFullName(ref StringData stringData)
    {
        var fullName = $"{stringData.FirstName} {stringData.LastName}";

        stringData.FullName = fullName;
        return fullName;
    }
}

And server configurations in summary : 和服务器配置摘要:

<service behaviorConfiguration="ServiceBehaviorMetaData" name="FlipCaseService.FlipCaseService" >
  <endpoint name="ReliableMessageService" address="flipcase/wsAddress" binding="wsHttpBinding" bindingConfiguration="BindingReliableMessaging" contract="FlipCaseService.IFlipCaseService" >
     <identity>
        <dns value="localhost" />
     </identity>
  </endpoint>
</service>

<bindings>
  <wsHttpBinding>
    <binding name="BindingReliableMessaging">
      <reliableSession enabled="true" inactivityTimeout="00:10:00"/>
    </binding>
  </wsHttpBinding>      
</bindings>

<behavior name="ServiceBehaviorMetaData">
  <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8080/flipcase/metadata" />
  <serviceDebug includeExceptionDetailInFaults="true" />
</behavior>

One reasonable approach here is to use asynchronous request-response. 这里一个合理的方法是使用异步请求 - 响应。 That means client does not wait for server to complete it's work, just fires request and forgets. 这意味着客户端不会等待服务器完成它的工作,只是激发请求并忘记。 When server is done, he calls client back with the results of operation. 服务器完成后,他会调用客户端返回操作结果。 Specifically WCF has duplex contracts to achieve that: http://www.codeproject.com/Articles/491844/A-Beginners-Guide-to-Duplex-WCF . 特别是WCF有双工合同来实现: http//www.codeproject.com/Articles/491844/A-Beginners-Guide-to-Duplex-WCF

When server response is ready, it tries to deliver it to the client. 当服务器响应准备就绪时,它会尝试将其传递给客户端。 If that fails, server can retry again later, until success or some timeout is reached. 如果失败,服务器可以稍后再次重试,直到达到成功或超时。

If you follow this pattern, client should have some unique identifier, so that even if connection is restored - server knows it is the same client and it knows which responses are awaited by this client. 如果你遵循这种模式,客户端应该有一些唯一的标识符,这样即使连接恢复 - 服务器知道它是同一个客户端,它知道这个客户端正在等待哪些响应。

Another approach would be to cache results on server for some (limited) time. 另一种方法是在一些(有限的)时间内将结果缓存在服务器上。 You can provide unique id for each request, then on server check if request with such id was completed already and if yes - deliver the results immediatly. 您可以为每个请求提供唯一ID,然后在服务器上检查具有此类ID的请求是否已经完成,如果是,则立即提供结果。 Otherwise, process response and cache it for limited time, in case client will retry a bit later. 否则,处理响应并在有限时间内缓存它,以防客户端稍后重试。 On client - just retry on failure. 在客户端 - 只需重试失败。

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

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