不要忘了遍歷所有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);
}
}
Thanks @nodots fo你的幫助。根據您的答案和所有方法的完整示例[GetCodeSamples](http://www.getcodesamples.com/src/9FE9D387/3892B3EF),我構建了一個執行該任務的IPAuthServiceContract-Attribute。 –