2015-12-02 21 views
2

我在一個ServiceHost中實現了多個ServiceContracts。這項服務是可訪問的內部和外部網絡,並訪問是通過基於IP的方法授予屬性:WCF ServiceContract中的過濾方法

[OperationContract] 
[IPAuthentication(RequiredPermission = PermissionLevels.ExternalRead)] 
bool Ping(); 

這個偉大的工程,但它是混淆客戶端看到的所有方法,但只有幾種方法沒有訪問限制,其他人拋出一個HttpStatusCode.Unauthorized異常。

如何繼承,擴展或更改ServiceContractAttribute以實現客戶端WSDL中的過濾方法列表?

回答

0

不要忘了遍歷所有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); 
    } 
}