我在一个ServiceHost中实现了多个ServiceHost。服务可访问网络的内部和外部,并通过基于IP的方法属性授予访问权限:
[OperationContract]
[IPAuthentication(RequiredPermission = PermissionLevels.ExternalRead)]
bool Ping();这很好,但是客户机看到所有方法时都很困惑,只有几个方法没有访问限制,其他方法则抛出一个HttpStatusCode.Unauthorized异常。
如何继承、扩展或更改ServiceContractAttribute以实现客户端WSDL中的过滤方法列表?
发布于 2015-12-02 13:03:31
您可以通过IWsdlExportExtension控制WSDL生成。
这里有一个很好的例子:
http://blogs.msdn.com/b/carlosfigueira/archive/2011/10/06/wcf-extensibility-wsdl-export-extension.aspx
发布于 2015-12-04 09:43:43
不要忘记遍历所有wsdl文件来触摸任何对象:
public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
{
foreach (ServiceDescription wsdl in exporter.GeneratedWsdlDocuments)
{
List<string> messageNamesToRemove;
RemoveOperationsFromPortTypes(wsdl, out messageNamesToRemove);
List<string> policiesToRemove;
RemoveOperationsFromBindings(wsdl, out policiesToRemove);
RemoveWsdlMessages(wsdl, messageNamesToRemove);
RemoveOperationRelatedPolicies(wsdl, policiesToRemove);
}
}如果隐藏了所有方法,则从PortTypes和绑定中删除空合同:
private void RemoveOperationsFromPortTypes(ServiceDescription wsdl, out List<string> messageNamesToRemove)
{
messageNamesToRemove = new List<string>();
var emptyPorts = new List<System.Web.Services.Description.PortType>();
foreach (System.Web.Services.Description.PortType portType in wsdl.PortTypes)
{
for (int i = portType.Operations.Count - 1; i >= 0; i--)
{
...
if (portType.Operations.Count == 0)
emptyPorts.Add(portType);
}
}
foreach (var emptyPort in emptyPorts)
{
wsdl.PortTypes.Remove(emptyPort);
}
}
private void RemoveOperationsFromBindings(ServiceDescription wsdl, out List<string> policiesToRemove)
{
policiesToRemove = new List<string>();
var emptyBindings = new List<System.Web.Services.Description.Binding>();
foreach (System.Web.Services.Description.Binding binding in wsdl.Bindings)
{
for (int i = binding.Operations.Count - 1; i >= 0; i--)
{
...
}
if (binding.Operations.Count == 0)
emptyBindings.Add(binding);
}
foreach (var emptyBinding in emptyBindings)
{
wsdl.Bindings.Remove(emptyBinding);
}
}https://stackoverflow.com/questions/34040449
复制相似问题