The*_*ght 1 c# asp.net wcf extensibility
在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)
{
}
}
Run Code Online (Sandbox Code Playgroud)
如何提取webmethod的siteName输入参数的值?我的所有webmethod都有siteName输入参数.
消息和参数之间的转换由消息格式化程序完成.你可以做到这一点,但一旦你阅读邮件来获取参数的值,已被消息所用,所以你需要将它传递给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();
}
}
Run Code Online (Sandbox Code Playgroud)