在邮件检查器(IClientMessageInspector
或IDispatchMessageInspector
)中,只有一种方法会收到IClientChannel
参数。我希望在另一个方法中提供一些通道数据,因此我创建了一个用作相关状态对象的类:
public void AfterReceiveReply(ref Message reply, object correlationState)
{
var typedCorrelationState = (CorrelationState)correlationState;
/* ... */
}
public object BeforeSendRequest(ref Message request, System.ServiceModel.IClientChannel channel)
{
var correlationState = new CorrelationState
{
SoapCorrelationId = Guid.NewGuid(),
EndpointAddress = channel.RemoteAddress.Uri.ToString()
};
/* ... */
return correlationState;
}
private class CorrelationState
{
public Guid SoapCorrelationId { get; set; }
public string EndpointAddress { get; set; }
}
可以像这样使用相关状态,还是有更合适的方式来传递这些数据?
答案 0 :(得分:1)
你做的很完美。
权威示例:WCF Extensibility – Message Inspectors。如果您在博客文章中搜索" correlationState"你会看到他以同样的方式传递URI。