在WCF消息检查中,如何在Message对象中查找输入参数的值?
我正在使用MessageInspector并拥有以下类:
public class MyMessageInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
// TODO: how to find the value of the siteName parameter if exists?
Console.WriteLine("Incoming request: {0}", request);
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
如何提取webmethod的siteName输入参数的值?我的所有webmethod都有siteName输入参数。
答案 0 :(得分:7)
消息和参数之间的转换由消息格式化程序完成。您可以这样做,但是一旦您阅读消息以获取参数的值,消息已经使用,因此您需要在将其传递给WCF之前重新创建它(这就是为什么在消息检查器,请求通过引用传递,因此如果确实需要使用它,则可以替换它。)
正如@burning_LEGION所提到的,参数检查器可能是您的方案的最佳选择。在检查器本身上它没有参数名称,但是您可以从操作说明中获取它们,您将在行为中使用它来添加参数检查器。下面的代码显示了如何完成它的一种方式。
public class StackOverflow_15637994
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string Echo(string text);
[OperationContract]
int Execute(string op, int x, int y);
[OperationContract]
bool InOutAndRefParameters(int x, ref int y, out int z);
}
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
public int Execute(string op, int x, int y)
{
return x + y;
}
public bool InOutAndRefParameters(int x, ref int y, out int z)
{
z = y;
y = x;
return true;
}
}
public class MyInspector : IParameterInspector
{
string[] inputParameterNames;
string[] outputParameterNames;
public MyInspector(string[] inputParameterNames, string[] outputParameterNames)
{
this.inputParameterNames = inputParameterNames;
this.outputParameterNames = outputParameterNames;
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
Console.WriteLine("Operation: {0}", operationName);
Console.WriteLine(" Result: {0}", returnValue);
for (int i = 0; i < outputs.Length; i++)
{
Console.WriteLine(" [out] {0}: {1}", this.outputParameterNames[i], outputs[i]);
}
}
public object BeforeCall(string operationName, object[] inputs)
{
Console.WriteLine("Operation: {0}", operationName);
for (int i = 0; i < inputs.Length; i++)
{
Console.WriteLine(" {0}: {1}", this.inputParameterNames[i], inputs[i]);
}
return null;
}
}
public class MyBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
foreach (var operation in endpoint.Contract.Operations)
{
string[] inputParamNames = operation.Messages[0].Body.Parts
.OrderBy(mpd => mpd.Index)
.Select(mpd => mpd.Name)
.ToArray();
string[] outputParamNames = null;
if (operation.Messages.Count > 1)
{
outputParamNames = operation.Messages[1].Body.Parts
.OrderBy(mpd => mpd.Index)
.Select(mpd => mpd.Name)
.ToArray();
}
MyInspector inspector = new MyInspector(inputParamNames, outputParamNames);
endpointDispatcher.DispatchRuntime.Operations[operation.Name].ParameterInspectors.Add(inspector);
}
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
endpoint.Behaviors.Add(new MyBehavior());
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
ITest proxy = factory.CreateChannel();
proxy.Echo("Hello");
proxy.Execute("foo", 2, 5);
int z;
int y = 2;
proxy.InOutAndRefParameters(3, ref y, out z);
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
答案 1 :(得分:2)
您可以反序列化Message
并使用任何字段,但使用IParameterInspector会更好