以编程方式从WCF Web HTTP服务帮助页面中删除方法?

时间:2015-01-29 23:20:49

标签: .net wcf

如何以编程方式从WCF Web HTTP Service Help Page中删除服务方法?我仍然希望显示“帮助”页面,但我需要从中删除特定方法 - 而不更改ServiceContract。

我尝试通过自定义IEndpointBehavior从ServiceEndpoints中删除服务方法,如下所示:

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        var operationsToRemove = endpointDispatcher.DispatchRuntime.Operations.Where(op => this.IsDeprecated(op)).ToList();
        foreach (var operation in operationsToRemove)
        {
            endpointDispatcher.DispatchRuntime.Operations.Remove(operation);
        }
    }

...当调用一个弃用的方法时,我得到一个"方法不允许"错误(根据需要)。

我还尝试按照this article中的步骤忽略WSDL /自动生成的客户端中的服务方法,但这似乎不会影响“帮助”页面。

1 个答案:

答案 0 :(得分:3)

您可以通过编程方式进行,但我相信这样做对生产环境不利。 Microsoft在System.ServiceModel.Web.dll中隐藏的HelpPage的实现,并且没有可扩展点来更改此行为。

但我们知道当前的实现,我们可以使用反射来实现HelpPage的方法的动态管理。但MS可以更改合同和实施细节,我们的实施将被打破。所以,我强烈建议不要在真实环境中使用

这是一个自定义BadCustomHelpPageWebHttpBehavior(继承自WebHttpBehavior)。构造函数采用一系列方法从“帮助页面”中排除:

public class BadCustomHelpPageWebHttpBehavior : WebHttpBehavior
{
    /// <summary>
    /// Creates BadCustomHelpPageWebHttpBehavior
    /// </summary>
    /// <param name="ignoredMethodNames">Array of method names to ignore in Help Page</param>
    public BadCustomHelpPageWebHttpBehavior(string[] ignoredMethodNames)
    {
        m_ignoredMethodNames = ignoredMethodNames;
    }

    /// <summary>
    /// Remove methods to display in Help Page by names passed in the constructor
    /// </summary>
    public override void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        base.ApplyDispatchBehavior(endpoint, endpointDispatcher);

        if (m_ignoredMethodNames == null || m_ignoredMethodNames.Length == 0)
            return;

        DispatchOperation helpOperation = endpointDispatcher.DispatchRuntime.Operations.FirstOrDefault(o => o.Name == "HelpPageInvoke");
        if(helpOperation == null)
            return;

        IOperationInvoker helpInvoker = helpOperation.Invoker;

        Type helpInvokerType = CreateInternalSystemServiceWebType("System.ServiceModel.Web.HelpOperationInvoker");
        FieldInfo helpPageFieldInfo = helpInvokerType.GetField("helpPage",
            BindingFlags.Instance | BindingFlags.NonPublic);
        if (helpPageFieldInfo != null)
        {
            object helpPage = helpPageFieldInfo.GetValue(helpInvoker);

            Type helpPageType = CreateInternalSystemServiceWebType("System.ServiceModel.Dispatcher.HelpPage");
            Type operationHelpInformationType =
                CreateInternalSystemServiceWebType("System.ServiceModel.Dispatcher.OperationHelpInformation");

            Type dictionaryType = typeof (Dictionary<,>);
            Type[] operationInfoDictionaryGenericTypes = {typeof (string), operationHelpInformationType};
            Type operationInfoDictionaryType = dictionaryType.MakeGenericType(operationInfoDictionaryGenericTypes);

            FieldInfo operationInfoDictionaryFieldInfo = helpPageType.GetField("operationInfoDictionary",
                BindingFlags.Instance | BindingFlags.NonPublic);
            if (operationInfoDictionaryFieldInfo != null)
            {
                object operationInfoDictionary = operationInfoDictionaryFieldInfo.GetValue(helpPage);
                object operationInfoDictionaryReplaced = RemoveHelpMethods(operationInfoDictionary,
                    operationInfoDictionaryType);
                operationInfoDictionaryFieldInfo.SetValue(helpPage, operationInfoDictionaryReplaced);
            }
        }
    }

    private object RemoveHelpMethods(object operationInfoDictionary, Type operationInfoDictionaryType)
    {
        Debug.Assert(m_ignoredMethodNames != null);

        var operationInfoDictionaryReplaced = Activator.CreateInstance(operationInfoDictionaryType);

        var operationInfoDictionaryAsEnumerable = operationInfoDictionary as IEnumerable;
        if (operationInfoDictionaryAsEnumerable != null)
        {
            foreach (var operationInfoEntry in operationInfoDictionaryAsEnumerable)
            {
                object key = operationInfoEntry.GetType().GetProperty("Key").GetValue(operationInfoEntry);
                object value = operationInfoEntry.GetType().GetProperty("Value").GetValue(operationInfoEntry);

                string name = value.GetType().GetProperty("Name").GetValue(value) as string;

                if (m_ignoredMethodNames.Contains(name) == false)
                {
                    operationInfoDictionaryReplaced.GetType()
                        .GetMethod("Add")
                        .Invoke(operationInfoDictionaryReplaced, new[] {key, value});
                }
            }
        }

        return operationInfoDictionaryReplaced;
    }

    private static Type CreateInternalSystemServiceWebType(string requestedType)
    {
        return typeof (WebServiceHost).Assembly.GetType(requestedType);
    }

    private readonly string[] m_ignoredMethodNames;
}

要使用此类,只需将此行为添加到您的端点:

host.Description.Endpoints[0].Behaviors.Add(new BadCustomHelpPageWebHttpBehavior(new[] { "EchoWithGet" })
{
    HelpEnabled = true
});

此示例的完整源代码(包括简单的WCF HTTP服务器)可在此处找到: https://github.com/knyu15/BadCustomHelpPageWebHttpBehavior

可能更好的方法是将WCF HelpPage替换为您的自定义页面。详细的例子可以在这里找到: Customize the WebHttp Help Output in WCF。但是,这并没有回答你当前的问题。