WSDL-first 方法:如何为 wsdl:port 和 wsdl:binding 指定不同的名称? [英] WSDL-first approach: How to specify different names for wsdl:port and wsdl:binding?

查看:34
本文介绍了WSDL-first 方法:如何为 wsdl:port 和 wsdl:binding 指定不同的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循 WSDL-first(由我们的客户提供)方法来开发 WCF 服务,但是从我的 wcf 服务生成的 WSDL 与我们客户提供给我的 WSDL 略有不同,并且由于这种不匹配,客户面临着制作的困难打电话给我的服务.

I am following WSDL-first (provided by our client) approach for developing WCF service but WSDLs generated from my wcf service is slightly different from WSDL provided to me by our client and because of this mismatch, client is facing difficulties to make a call to my service.

客户端提供的 wsdl:

Client provided wsdl:

<wsdl:port binding='tns:CheckoutBinding' name='CheckoutServicePort'><soap:address location='placeholder to service uri'/></wsdl:port></wsdl:service>


从 wcf 服务生成的 WSDL:

WSDL generated from wcf service:

<代码><wsdl:service name="CheckoutService"><wsdl:port binding="tns:CheckoutBinding" name="CheckoutBinging"><soap:address location="placeholder to service uri"/></wsdl:port></wsdl:service>

还有,我的服务设置如下:

and, my service settings are as follows:

<endpoint name="CheckoutBinding" address="" binding="basicHttpBinding" bindingName="CheckoutServicePort" bindingConfiguration="basicHttpBinding" bindingNamespace="<<namespace>>"合同="<<合同名称>>"/>

我已经使用 WSCF.Blue 从客户端提供的 wsdl 生成服务器存根代码,并对生成的代码进行了细微的更改,以发出与客户端提供的 WSDL 完全相同的 WSDL,但我不知道要进行哪些更改在配置文件或生成的代码中生成与客户端提供的 wsdl 文件相同的wsdl:port/@name".

I have used WSCF.Blue for generating server-stub code from the client provided wsdl and made minor changes in the generated code to emit WSDL exactly same as the one provided by client but i am not getting any idea about what change to make in the config file or in generated code to get the same "wsdl:port/@name" as in the client provided wsdl file.

根据此url,serviceendpoint name 属性映射到 wsdl:port/@name和 wsdl:binding/@name.基于此,我的配置文件中的端点/@name 属性值映射到 wsdl:port/@name 和 wsdl:binding/@name 但我希望将不同的名称映射到 wsdl:port/@name 和 wsdl:binding/@name 属性.

As per this url, serviceendpoint name property is mapped to wsdl:port/@name and wsdl:binding/@name. Based on this, endpoint/@name attribute value in my config file is mapped to wsdl:port/@name and wsdl:binding/@name but i want different names to be mapped to wsdl:port/@name and wsdl:binding/@name attributes.

请帮助我实现这一目标.

Kindly help me in achieving this.

推荐答案

可以尝试实现 IWsdlExportExtension 并在 ExportEndpoint 中修改 wsdl:port/@name.然后实现 IEndpointBehavior,它将您的扩展添加到端点.要使用您的新行为,您有两种选择:

You can try to implement IWsdlExportExtension and in ExportEndpoint modify wsdl:port/@name. Then implement IEndpointBehavior which will add your extension to an endpoint. To use your new behavior you have two choices:

  • 从代码中添加行为.当服务托管在 IIS 中时,您必须创建自定义 ServiceHost 和 ServiceHostFactory.在自托管中,您只需迭代端点并添加行为即可.
  • 从配置中添加行为.您必须实现自定义 BehaviorExtensionElement,注册此元素并在与您的端点相关的 endpointBehaviors 中使用它.

这是带有扩展元素的简单示例:

Here is simple example with extension element:

using System;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;

namespace CustomWsdlExtension    
{
    public class PortNameWsdlBehavior : IWsdlExportExtension, IEndpointBehavior
    {
        public string Name { get; set; }

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

        public void ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
        {
            if (!string.IsNullOrEmpty(Name))
            {
                context.WsdlPort.Name = Name;
            }
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }

    public class PortNameWsdlBehaviorExtension : BehaviorExtensionElement
    {
        [ConfigurationProperty("name")]
        public string Name
        {
            get 
            { 
                object value = this["name"];
                return value != null ? value.ToString() : string.Empty; 
            }
            set { this["name"] = value; }
        }

        public override Type BehaviorType
        {
            get { return typeof(PortNameWsdlBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new PortNameWsdlBehavior { Name = Name };
        }
    }
}

和配置:

  <system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="portName" type="CustomWsdlExtension.PortNameWsdlBehaviorExtension, CustomWsdlExtension" />
      </behaviorExtensions>
    </extensions>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="customPortName">
          <portName name="myCustomName" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service name="CustomWsdlExtension.Service">
        <endpoint address="" binding="basicHttpBinding" contract="CustomWsdlExtension.IService"
                  behaviorConfiguration="customPortName" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

我的 WSDL 看起来像:

My WSDL then looks like:

<wsdl:service name="Service">
    <wsdl:port name="myCustomName" binding="tns:BasicHttpBinding_IService">
        <soap:address location="http://localhost:2366/Service.svc" /> 
    </wsdl:port>
</wsdl:service>

这篇关于WSDL-first 方法:如何为 wsdl:port 和 wsdl:binding 指定不同的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆