0

我已经构建了一个自定义绑定,以便能够从其他来源接收 HTTP 消息。但是,它还不是没有错误的。

我观察到,一旦处理了第一个请求,我的服务就会将 CPU 使用率提高到 100%,并且随着请求的数量越来越多,服务变得越来越慢。在将日志记录命令插入每个命令后,可以看到这种行为的原因绑定的功能。

直到第一个请求进入之前,一切正常:

ChannelListener: OnBeginAcceptChannel
ChannelListener: OnAcceptChannel

然后,完成第一条消息的处理:

Channel: static constructor
Channel: constructor
ChannelListener: OnEndAcceptChannel (completes)
ChannelListener: Uri get
ChannelListener: OnBeginAcceptChannel
ChannelListener: OnAcceptChannel
Channel: OnOpen
Channel: BeginTryReceiveRequest
Channel: TryReceiveRequest
Channel: WaitForRequest
Channel: ReceiveRequest
Context: constructor
Channel: EndTryReceiveRequest (completes)
Context: RequestMessage get
`Channel: BeginTryReceiveRequest`
Context: Reply noTimeout
Context: Reply
Context: Close noTimeout
`Channel: TryReceiveRequest`
`Channel: WaitForRequest`
`Channel: ReceiveRequest (hangs)`
`Channel: EndTryReceiveRequest (doesn't complete since receive hangs)`
`Channel: BeginTryReceiveRequest`
`and so on...`

通道实现了 IReplyChannel 接口,因此只能获取请求,回复它,然后关闭通道。ServiceModel 不只是关闭通道,而是继续在已使用的通道上发送垃圾邮件 TryReceiveRequest,而不管过去是否已使用该通道。

有什么方法可以正确解决这个问题吗?为什么ServiceModel在关闭回复上下文后不关闭通道,尽管在使用后保持通道打开是没有用的?

4

1 回答 1

1

所以,也许有人仍然需要解决这个问题。在BeginTryReceiveRequestEndTryReceiveRequest检查通道的State属性。

......

public IAsyncResult BeginTryReceiveRequest(TimeSpan timeout, AsyncCallback callback, object state)
        {
            if (State == CommunicationState.Closed)
            {
                return null;
            }
            return new TryReceiveRequestAsyncResult(timeout, this, callback, state);
        }

......

public bool EndTryReceiveRequest(IAsyncResult result, out RequestContext context)
            {
                if (State == CommunicationState.Closed)
                {
                    context = null;
                    return false;
                }
                return TryReceiveRequestAsyncResult.End(result, out context, this);
            }
于 2013-12-25T11:19:10.133 回答