简体   繁体   English

是否可以启动从Web服务而不是客户端终止的连接?

[英]Is it possible to initiate a connection ending from the web service, instead of the client?

I have a SOAP WCF web service which forwards some XML message to a TCP port on another machine (via an HttpWebRequest post). 我有一个SOAP WCF Web服务,该服务将一些XML消息转发到另一台计算机上的TCP端口(通过HttpWebRequest发布)。 The reason we use this web service is that it can be configured easily to make use two way SSL with client and server certificates. 我们使用此Web服务的原因是它可以轻松配置为使用两种方式将SSL与客户端和服务器证书结合使用。 It is working fine except for the part that there is no FIN message sent by my web service to the client as soon as the HttpWebRequest ends (because a FIN was received on that side). 正常运行,除了HttpWebRequest结束后我的Web服务没有立即向客户端发送FIN消息的部分(因为在那一侧接收到FIN)。 The connection between the web service and the client ends only because the client will send a FIN after a timeout (when KeepAlive is true) or immediately after it received the answer (when KeepAlive is false). Web服务与客户端之间的连接仅会终止,因为客户端将在超时后(当KeepAlive为true时)或在收到答案后立即发送FIN(当KeepAlive为false时)发送FIN。 So the connection ending is always initiated by the client. 因此,连接结束始终由客户端发起。

Is it possible to let the web service do the ending of the connection (let the web service send the FIN to the client)? 是否可以让Web服务结束连接(让Web服务将FIN发送到客户端)?

I already tried to use an event handler like this without luck: 我已经尝试在没有运气的情况下使用这样的事件处理程序:

OperationContext.Current.OperationCompleted += DisconnectClientImmediatelyAfterResponseEventHandler;

private void DisconnectClientImmediatelyAfterResponseEventHandler(object sender, EventArgs e)
{
    OperationContext.Current.Channel.Close();
}

This is the complete code of my Post action: 这是我的Post操作的完整代码:

public Message Post(Message postRequest)
{
      string bodyOfPostRequest = GetBodyContents(postRequest);            

      IHttpWebRequestWrap remoteRequest = CreateHttpWebRequest();
      try
      {
          using (Stream remoteResponseStream = remoteRequest.Post(bodyOfPostRequest, RemoteRequestContentType))
          {
              string responseMessage = StreamToString(remoteResponseStream);                    
              Message reply = CreateReplyMessage(postRequest.Version, postRequest.Headers.Action, responseMessage);

              return reply;
          }
      }
      catch (Exception ex)
      {
          _log.Fatal("The following exception occured.", ex);
          throw;
      }
}

The remoteRequest.Post(...) is an extension method which does the actual post. remoteRequest.Post(...)是执行实际发布的扩展方法。

In the end it was not so hard to accomplish this: 最后,完成此任务并不难:

In the web.config the aspNetCompatibilityEnabled attribute of the serviceHostingEnvironment must be set to true: 在web.config中,必须将serviceHostingEnvironment的aspNetCompatibilityEnabled属性设置为true:

<system.serviceModel>
  ...
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

Then the connection can be closed immediately after sending the response by doing this: 然后可以通过执行以下操作在发送响应后立即关闭连接:

private void DisconnectClientImmediatelyAfterResponseEventHandler(object sender, EventArgs e)
{
    HttpContext.Current.Response.Close();
}

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

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