Yarons評論讓我在正確的方向前進
一個ParameterInspector翻出的價值和使用cusom的OperationContext擴展
public class TestParameterInspector : IParameterInspector
{
public object BeforeCall(string operationName, object[] inputs)
{
return null;
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
OperationContext.Current.Extensions.Add(new ContextSessionExtension() {SomeData = "testme"});
}
}
public class ContextSessionExtension : IExtension<OperationContext>
{
public void Attach(OperationContext owner)
{
}
public void Detach(OperationContext owner)
{
}
public string SomeData { get; set; }
}
值是將其放置到的OperationContext然後拉出並使用MessageInspector
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
var test = OperationContext.Current.Extensions.Find<ContextSessionExtension>().SomeData;
object httpRequestMessageObject;
HttpRequestMessageProperty httpRequestMessage;
if (reply.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
if (string.IsNullOrEmpty(httpRequestMessage.Headers["MYTEST"]))
{
httpRequestMessage.Headers["MYTEST"] = test;
}
}
else
{
httpRequestMessage = new HttpRequestMessageProperty();
httpRequestMessage.Headers.Add("MYTEST", test);
reply.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
}
}
放入HTTP頭
也許這可以幫助,不知道100%http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/26/wcf-extensibility-iparameterinspector.aspx –
乾杯亞龍,博客讓我前進在正確的方向上,如果其他人有興趣這樣做,我在下面添加了答案 –