您可以以编程方式进行,但我相信这样做的方式不适合生产环境。微软在 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。但这并不能回答您的直接问题。