我在自定义频道上实现了IDuplexSessionChannel,因为我使用了tcpTransport。在该自定义通道中,我缓存服务调用响应(客户端缓存)。但它有错误。在IRequestChannel中,它工作正常。如何在TryMessage方法中更改消息。我的代码:
public Message Receive(TimeSpan timeout)
{
Message response = null;
response = CommunicationCacheManager.Get(_request.Headers.Action, _request);
if (response == null)
{
response = this.InnerChannel.Receive(timeout);
int cacheTimeout = 0;
if (response.Headers.FindHeader(Constants.CacheTimeOutHeader.NAME, Constants.CacheTimeOutHeader.NAMESPACE) > -1)
{
cacheTimeout = response.Headers.GetHeader<int>(Constants.CacheTimeOutHeader.NAME, Constants.CacheTimeOutHeader.NAMESPACE);
}
if (cacheTimeout > 0 && response != null &&
!response.IsFault &&
!response.IsEmpty)
{
CommunicationCacheManager.Add(_request.Headers.Action, cacheTimeout, ref response);
}
}
return response;
}
public Message Receive()
{
return this.InnerChannel.Receive();
}
public bool TryReceive(TimeSpan timeout, out Message message)
{
ThrowIfDisposedOrNotOpen();
message = null;
bool timedout = false;
try
{
message = this.Receive(timeout);
}
catch (TimeoutException)
{
timedout = true;
}
return (!timedout);
}
CacheManager有效。我得到了响应缓存。但是当我看消息时,tryReceive再次运行。消息已关闭。我该如何解决?
答案 0 :(得分:0)
问题解决了。
Tcp Binding将RelatesTo Header添加到Message。所以代码改为
public Message Receive(TimeSpan timeout)
{
Message response = null;
response = CommunicationCacheManager.Get(_request.Headers.Action, _request);
if (response == null)
{
response = this.InnerChannel.Receive(timeout);
int cacheTimeout = 0;
if (response.Headers.FindHeader(Constants.CacheTimeOutHeader.NAME, Constants.CacheTimeOutHeader.NAMESPACE) > -1)
{
cacheTimeout = response.Headers.GetHeader<int>(Constants.CacheTimeOutHeader.NAME, Constants.CacheTimeOutHeader.NAMESPACE);
}
if (cacheTimeout > 0 && response != null &&
!response.IsFault &&
!response.IsEmpty)
{
CommunicationCacheManager.Add(_request.Headers.Action, cacheTimeout, ref response);
}
}
else
{
response.Headers.RelatesTo=_request.Header.MessageId;
}
return response;
}