TryReceiveRequest被无限次调用

时间:2009-08-11 13:29:13

标签: wcf wcf-binding extensibility transport

我已经构建了一个自定义绑定,以便能够从其他源接收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不会关闭通道,尽管在通道消耗后保持通道打开是没用的?

1 个答案:

答案 0 :(得分:1)

所以,也许有人仍然需要解决这个问题。检查 BeginTryReceiveRequest EndTryReceiveRequest 的频道的属性。

.....

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);
            }