Tur*_*tik 5 c# wcf ws-reliablemessaging wshttpbinding wcf-wshttpbinding
所以我创建了Client/Server WCF.我想要的是当我从这个客户端向服务器发送消息并且因任何原因切断连接时,客户端关闭,例如,当客户端再次可用时,该客户端如何获得响应?
是否可以在客户端和服务器之间设置会话等?
我的客户代码是:
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);
}
Run Code Online (Sandbox Code Playgroud)
我的服务器代码是:
[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;
}
}
Run Code Online (Sandbox Code Playgroud)
和服务器配置摘要:
<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>
Run Code Online (Sandbox Code Playgroud)
一种合理的方法是使用异步请求-响应。这意味着客户端不会等待服务器完成其工作,只是触发请求并忘记。当服务器完成后,他将操作结果回调给客户端。具体来说,WCF 有双工合约来实现这一点:http://www.codeproject.com/Articles/491844/A-Beginners-Guide-to-Duplex-WCF。
当服务器响应准备好时,它会尝试将其传递给客户端。如果失败,服务器可以稍后重试,直到成功或达到超时。
如果遵循这种模式,客户端应该有一些唯一的标识符,这样即使连接恢复,服务器也知道它是同一个客户端,并且知道该客户端正在等待哪些响应。
另一种方法是将结果缓存在服务器上一段(有限的)时间。您可以为每个请求提供唯一的 ID,然后在服务器上检查具有此类 ID 的请求是否已完成,如果是,则立即交付结果。否则,处理响应并将其缓存有限的时间,以防客户端稍后重试。在客户端 - 失败时重试。
| 归档时间: |
|
| 查看次数: |
659 次 |
| 最近记录: |