我有一个WCF服务,我已经扩展了它的行为。
这是我的代码:
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) {
if (logger.IsInfoEnabled) {
// this is a header sent from client that I need to keep so I can return it in BeforeSendReply
var securityToken = request.Headers.GetHeader < string > (request.Headers.FindHeader("Token-ID", "SecurityInfo"))
var bufferedCopy = request.CreateBufferedCopy(int.MaxValue);
var sizeLog = string.Format("request message size: ~{0} KB", GetMessageLengthInKB(bufferedCopy.CreateMessage()));
logger.Info(sizeLog);
request = bufferedCopy.CreateMessage();
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState) {
if (logger.IsInfoEnabled) {
// here I want to get the securityToken value ???
var bufferedCopy = reply.CreateBufferedCopy(int.MaxValue);
var sizeLog = string.Format("response message size: ~{0} KB", GetMessageLengthInKB(bufferedCopy.CreateMessage()));
logger.Info(sizeLog);
reply = bufferedCopy.CreateMessage();
}
}
有关如何保留 securityToken 变量以便在将回复发送给客户端之前使用它的任何线索?
答案 0 :(得分:0)
您可以在AfterReceiveRequest方法中返回您的令牌(而不是返回null),它将作为correlationState对象传递给BeforeSendReply(将其转换为适当的类型,您可以使用它)