wcf - 如何更改 WCf 服务的 wsdl 文件中的默认架构位置?

标签 wcf wcf-binding

以下是我的服务的 wsdl 文件:

    <wsdl:types>
      <xsd:schema targetNamespace="http://tempuri.org/Imports">
      <xsd:import schemaLocation="http://localhost:3789/VideoUpload.svc?xsd=xsd0" namespace="http://tempuri.org/" /> 
      <xsd:import schemaLocation="http://localhost:3789/VideoUpload.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/" /> 
      <xsd:import schemaLocation="http://localhost:3789/VideoUpload.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/UploadVideoProtocol" /> 
      </xsd:schema>
    </wsdl:types>
-----
<wsdl:definitions>
<wsdl:service name="VideoUpload">
<wsdl:port name="BasicHttpBinding_IVideoUpload" binding="tns:BasicHttpBinding_IVideoUpload">
  <soap:address location="http://localhost:3789/VideoUpload.svc" /> 
  </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

在上面,我可以通过在服务契约和行为的代码中指定自定义命名空间来更改命名空间。

但我需要更改架构位置中指定的端点地址,

schemaLocation="http://localhost:3789/VideoUpload.svc?xsd=xsd0"

到我自己定义的端点地址为:

schemaLocation="http://myservice.com:8080/VideoUpload.svc?xsd=xsd0"

实现这一目标的程序是什么?代码中必须提到什么才能更改生成的默认端点?任何人都可以帮我解决这个问题吗?

最佳答案

您可以通过添加实现“IWsdlExportExtension”的新行为来动态更新 WSDL 元数据中的 WCF 端点地址

public class HostNameAddressBehavior : Attribute, IWsdlExportExtension, IEndpointBehavior, IServiceBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint,
        BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint,
        ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint,
        EndpointDispatcher endpointDispatcher)
    {

    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }

    public void AddBindingParameters(ServiceDescription serviceDescription,
        ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints,
        BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
        ServiceHostBase serviceHostBase)
    {
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

    public void ExportContract(WsdlExporter exporter,
        WsdlContractConversionContext context)
    {
    }

    /// <summary>
    /// Overwrite service meta data
    /// </summary>
    /// <param name="exporter"></param>
    /// <param name="context"></param>
    public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
    {
        var address = "YOUR_ENDPOINT";

        context.Endpoint.Address = new System.ServiceModel.EndpointAddress(address);

        XmlSchemaSet schemaSet = exporter.GeneratedXmlSchemas;

        foreach (System.Web.Services.Description.ServiceDescription wsdl in exporter.GeneratedWsdlDocuments)
        {
            foreach (XmlSchema schema in wsdl.Types.Schemas)
            {
                ChangeSchemaLocation(schemaSet, schema, address);
            }
        }
    }

    /// <summary>
    /// Update XSD location
    /// </summary>
    /// <param name="xmlSchemaSet"></param>
    /// <param name="xsdDoc"></param>
    /// <param name="address"></param>
    private void ChangeSchemaLocation(XmlSchemaSet xmlSchemaSet, XmlSchema xsdDoc, string address)
    {
        foreach (XmlSchemaExternal external in xsdDoc.Includes)
        {
            if ((external != null) && string.IsNullOrEmpty(external.SchemaLocation))
            {
                string str = (external is XmlSchemaImport) ? ((XmlSchemaImport)external).Namespace : xsdDoc.TargetNamespace;
                foreach (XmlSchema schema in xmlSchemaSet.Schemas(str ?? string.Empty))
                {
                    if (schema != xsdDoc)
                    {
                        external.SchemaLocation = address + "/?xsd=xsd0"; // set the location;
                        break;
                    }
                }
                continue;
            }
        }
    }
}

通过代码或在配置文件中添加您的新行为。

按代码:
var endpoint = listener.ServiceHost.Description.Endpoints.First();
endpoint.Behaviors.Add(new HostNameAddressBehavior());

或者

按配置:

创建扩展:
    public class HostNameAddressBehaviorExtension : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get
        {
            return typeof(HostNameAddressBehavior);
        }
    }

    protected override object CreateBehavior()
    {
        return new HostNameAddressBehavior();
    }
}

然后加:
<extensions>
    <behaviorExtensions>
      <add name="hostNameAddress" type="YourService.HostNameAddressBehaviorExtension, YourService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
    </behaviorExtensions>
  </extensions>

关于wcf - 如何更改 WCf 服务的 wsdl 文件中的默认架构位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3179179/

相关文章:

.net - 从 WCF 服务生成单独的 "model"类

c# - 无法调用服务,WCF 测试客户端?

c# - XML 反序列化忽略不按字母顺序排列的属性

ajax - 使用 gzip 编码时,WCF 服务返回不正确的 Content-Length

wcf - 如何覆盖 WebServiceHostFactory MaxReceivedMessageSize?

wcf - 当 SSL 在 IIS 场 (WCF) 前面的代理上时如何使用 Forms Auth?

wcf - maxItemsInObjectGraph 被忽略

ssl - 为什么在作为 Windows 服务托管的休息服务上添加基于证书的身份验证,无法启动服务?

wix - 如何使用 WIX 3.5 项目/设置将 net.tcp 添加到 "Enabled protocols"?

WCF 服务默认是restful 还是基于soap?