gpt4 book ai didi

c# - 如何在 IClientMessageInspector 中获取调用的操作名称?

转载 作者:太空狗 更新时间:2023-10-29 21:34:34 26 4
gpt4 key购买 nike

我实现了一个 IClientMessageInspector在我的应用程序中“拦截”传出的 Web 服务调用。是否可以从 BeforeSendRequestAfterReceiveReply 中找出正在调用的操作?

这里也有类似的问题,How do i get the invoked operation name within a WCF Message Inspector ,这是针对服务器端(接收请求的一方)。我试图做类似的事情,例如

    public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
var v = OperationContext.Current.OutgoingMessageProperties["HttpOperationName"];
return null;
}

public void AfterReceiveReply(ref Message reply, object correlationState)
{
var v = OperationContext.Current.OutgoingMessageProperties["HttpOperationName"];
}

但在传出请求期间,OperationContext.Current 似乎为空,因此我无法使用它。知道如何获得它吗?知道如何干净地完成它(而不是解析 SOAP xml)吗?

最佳答案

根据您询问如何使用 IParameterInspector 完成此操作的评论.操作名称是 Before/AfterCall 方法的一部分。

只是添加到我对使用哪个检查器的评论中。来自 Carlos Figueira's blogs :

The message inspectors, described in the previous post of this series, allows you complete control over the message going through the WCF stack. They’re very powerful, but you have to know how to deal with the Message object, which is not the most desirable way of programming. If the service model in WCF hides all the messaging framework by allowing us to define our services in terms of strongly-typed operations (i.e., using nice primitive and user defined types), there should be a way of intercepting requests / responses after all the processing to extract those parameters from incoming messages (or before they’re packaged in outgoing messages) is done. The IParameterInspector is exactly that – before and after each call, the inspector gets a chance to inspect the operation inputs, outputs and return value, in the same types as defined by the operation contract, no conversion needed (the only thing needed is a cast, since the parameters are passed as objects).

这是一个完整的命令行程序,演示了:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace WCFClientInspector
{
public class OperationLogger : IParameterInspector
{
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
Console.WriteLine("Completed operation:" + operationName);
}

public object BeforeCall(string operationName, object[] inputs)
{
Console.WriteLine("Calling operation:" + operationName);
return null;
}
}

public class OperationLoggerEndpointBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}

public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
foreach (ClientOperation operation in clientRuntime.ClientOperations)
{
operation.ClientParameterInspectors.Add(new OperationLogger());
}
}

public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
}

public void Validate(ServiceEndpoint endpoint)
{
}
}


[ServiceContract]
public interface ISimple
{
[OperationContract]
void DoSomthing(string s);
}

public class SimpleService : ISimple
{
public void DoSomthing(string s)
{
Console.WriteLine("Called:" + s);
}
}

public static class AttributesAndContext
{
static void Main(string[] args)
{
ServiceHost simpleHost = new ServiceHost(typeof(SimpleService), new Uri("http://localhost/Simple"));
simpleHost.Open();

ChannelFactory<ISimple> factory = new ChannelFactory<ISimple>(simpleHost.Description.Endpoints[0]);
factory.Endpoint.EndpointBehaviors.Add(new OperationLoggerEndpointBehavior());
ISimple proxy = factory.CreateChannel();

proxy.DoSomthing("hi");

Console.WriteLine("Press ENTER to close the host.");
Console.ReadLine();

((ICommunicationObject)proxy).Shutdown();

simpleHost.Shutdown();
}
}

public static class Extensions
{
static public void Shutdown(this ICommunicationObject obj)
{
try
{
obj.Close();
}
catch (Exception ex)
{
Console.WriteLine("Shutdown exception: {0}", ex.Message);
obj.Abort();
}
}
}
}

它应该给出输出:

  Calling operation:DoSomthing 
Called:hi
Completed operation:DoSomthing
Press ENTER to close the host.

关于c# - 如何在 IClientMessageInspector 中获取调用的操作名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16029873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com