如何在 WCF 消息检查器中获取调用的操作名称

发布于 2024-08-25 17:51:50 字数 432 浏览 8 评论 0原文

我正在 WCF 中做一个消息检查器:

public class LogMessageInspector :
    IDispatchMessageInspector, IClientMessageInspector

它实现了以下方法:

public object AfterReceiveRequest(ref Message request,
    IClientChannel channel, InstanceContext instanceContext)

我可以通过以下方式获取调用的服务的名称:

instanceContext.GetServiceInstance().GetType().Name

但是如何获取调用的操作的名称?

I'm doing a message inspector in WCF:

public class LogMessageInspector :
    IDispatchMessageInspector, IClientMessageInspector

which implements the method:

public object AfterReceiveRequest(ref Message request,
    IClientChannel channel, InstanceContext instanceContext)

I can get the name of the invoked service with:

instanceContext.GetServiceInstance().GetType().Name

But how do I get the name of the invoked operation?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

柏拉图鍀咏恒 2024-09-01 17:51:50

这并不漂亮,但这就是我为获取操作名称所做的操作:

var action = OperationContext.Current.IncomingMessageHeaders.Action;
var operationName = action.Substring(action.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1);

It's not pretty, but this is what I did to get the operation name:

var action = OperationContext.Current.IncomingMessageHeaders.Action;
var operationName = action.Substring(action.LastIndexOf("/", StringComparison.OrdinalIgnoreCase) + 1);
盛夏已如深秋| 2024-09-01 17:51:50
var operationName = OperationContext.Current.IncomingMessageProperties["HttpOperationName"] as string;
var operationName = OperationContext.Current.IncomingMessageProperties["HttpOperationName"] as string;
执笏见 2024-09-01 17:51:50

此方法与此处介绍的其他方法类似,但使用 Path.GetFileName

Path.GetFileName(OperationContext.Current.IncomingMessageHeaders.Action);

此方法的返回值和 path 字符串的格式在这种情况下工作得非常和谐:

路径中最后一个目录字符之后的字符。如果最后一个
路径字符是目录或卷分隔符,这个
方法返回 String.Empty。如果路径为空,则此方法返回
空。

This approach is similar to others presented here, but uses Path.GetFileName:

Path.GetFileName(OperationContext.Current.IncomingMessageHeaders.Action);

The return value of this method and the format of the path string work quite harmoniously in this scenario:

The characters after the last directory character in path. If the last
character of path is a directory or volume separator character, this
method returns String.Empty. If path is null, this method returns
null.

陌路终见情 2024-09-01 17:51:50
OperationContext.Current.IncomingMessageHeaders.Action.Split('/').ToList().Last();
OperationContext.Current.IncomingMessageHeaders.Action.Split('/').ToList().Last();
岁月静好 2024-09-01 17:51:50

聚会有点晚了,但我必须比这个问题的现有答案更深入地挖掘,因为它们似乎涉及获取操作名称而不是操作名称。 (它们通常是相同的,因此获取操作名称实际上就是获取操作名称。)

Microsoft 的 Application Insights SDK Labs 的 WCF 库使得 共同努力:

private string DiscoverOperationName(OperationContext operationContext)
{
    var runtime = operationContext.EndpointDispatcher.DispatchRuntime;
    string action = operationContext.IncomingMessageHeaders.Action;
    if (!string.IsNullOrEmpty(action))
    {
        foreach (var op in runtime.Operations)
        {
            if (op.Action == action)
            {
                return op.Name;
            }
        }
    }
    else
    {
        // WebHttpDispatchOperationSelector will stick the
        // selected operation name into a message property
        return this.GetWebHttpOperationName(operationContext);
    }

    var catchAll = runtime.UnhandledDispatchOperation;
    if (catchAll != null)
    {
        return catchAll.Name;
    }

    return "*";
}

private string GetWebHttpOperationName(OperationContext operationContext)
{
    var name = WebHttpDispatchOperationSelector.HttpOperationNamePropertyName;
    if (this.HasIncomingMessageProperty(name))
    {
        return this.GetIncomingMessageProperty(name) as string;
    }

    return "<unknown>";
}

Little late to the party but I had to dig a little deeper than existing answers on this question because they seem to involve getting the action name and not the operation name. (Frequently they are the same so getting the action name does, in fact, get the operation name.)

Microsoft's Application Insights SDK Labs' WCF library makes this concerted effort:

private string DiscoverOperationName(OperationContext operationContext)
{
    var runtime = operationContext.EndpointDispatcher.DispatchRuntime;
    string action = operationContext.IncomingMessageHeaders.Action;
    if (!string.IsNullOrEmpty(action))
    {
        foreach (var op in runtime.Operations)
        {
            if (op.Action == action)
            {
                return op.Name;
            }
        }
    }
    else
    {
        // WebHttpDispatchOperationSelector will stick the
        // selected operation name into a message property
        return this.GetWebHttpOperationName(operationContext);
    }

    var catchAll = runtime.UnhandledDispatchOperation;
    if (catchAll != null)
    {
        return catchAll.Name;
    }

    return "*";
}

private string GetWebHttpOperationName(OperationContext operationContext)
{
    var name = WebHttpDispatchOperationSelector.HttpOperationNamePropertyName;
    if (this.HasIncomingMessageProperty(name))
    {
        return this.GetIncomingMessageProperty(name) as string;
    }

    return "<unknown>";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文