首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WCF ServiceContract中的滤波方法

WCF ServiceContract中的滤波方法
EN

Stack Overflow用户
提问于 2015-12-02 10:31:40
回答 2查看 380关注 0票数 2

我在一个ServiceHost中实现了多个ServiceHost。服务可访问网络的内部和外部,并通过基于IP的方法属性授予访问权限:

代码语言:javascript
运行
复制
[OperationContract]
[IPAuthentication(RequiredPermission = PermissionLevels.ExternalRead)]
bool Ping();

这很好,但是客户机看到所有方法时都很困惑,只有几个方法没有访问限制,其他方法则抛出一个HttpStatusCode.Unauthorized异常。

如何继承、扩展或更改ServiceContractAttribute以实现客户端WSDL中的过滤方法列表?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-02 13:03:31

您可以通过IWsdlExportExtension控制WSDL生成。

这里有一个很好的例子:

http://blogs.msdn.com/b/carlosfigueira/archive/2011/10/06/wcf-extensibility-wsdl-export-extension.aspx

票数 2
EN

Stack Overflow用户

发布于 2015-12-04 09:43:43

不要忘记遍历所有wsdl文件来触摸任何对象:

代码语言:javascript
运行
复制
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和绑定中删除空合同:

代码语言:javascript
运行
复制
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);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34040449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档