在基于WCF开发的Web Service导出WSDL定义问题及自定义wsdl:port 名称
在契约优先的Web服务开发过程中,往往是先拿到WSDL服务定义,各家开发各自的服务实现或客户端,然后互相调用。
尽管Web Service的标准已经发布很多年,但各个语言,框架对其实现仍然有差异,实际使用中仍有许多坑需要填。
.NET平台下,ASP.NET Web Service的兼容性最好,对比较复杂一点的wsdl文件更较好的生成Proxy Stub代码,而WCF兼容性较差,遇到复杂一点的生产就会存在问题,而且从Java等平台的客户端调用也存在问题。但毕竟WCF很灵活,可以嵌入到任何应用中,不必必须是Web应用,这一点很诱人。
在Web Service服务实现好以后,一般不会直接使用原来的wsdl定义文件,平台会根据导出契约自动生成wsdl,然后客户端会据此生成相应的Proxy访问代码。
WCF导出WSDL的坑
悲催的是,在我根据WSDL生成的类,并实现相应接口后,在导出的WSDL中找不到任何方法,基本就是空的,我还以为哪里的配置出来问题,百思不得其解。改来改去,最后,用攒机时的最小系统法来排除,原来问题出在OperationContract里的Action属性,这个属性是由WSDL文件生成代码时自动产生的,因此不敢去改,但正是因此,导致无法正确生成WSDL,删掉以后,一切正常。
OperationContract的Action属性本是用于控制消息的派发,基本对应WSDL里定义的operation的名字,在一个服务里可以有一个方法Action=”*”来接收所有未处理的消息。
[ServiceContract(Namespace="http://Microsoft.WCF.Documentation")]
public interface ISampleService{ [OperationContract(
Action="http://Microsoft.WCF.Documentation/OperationContractMethod",
Name="OCAMethod",
ReplyAction="http://Microsoft.WCF.Documentation/ResponseToOCAMethod"
)]
string SampleMethod(string msg); [OperationContractAttribute(Action = "*")]
void UnrecognizedMessageHandler(Message msg);
}
至于为何有他会影响我WSDL的导出,还希望高手指点。
wsdl:port的名称的自定义
本人先使用的使用Java平台的JAX-WS实现Linux下Web Service,然后在Windows上用客户端调用,客户端同时自己也实现Web Service,用于回调消息。这里问题就来了,同一个WSDL定义,在Java下面名称是XXXServerSoap,而在WCF里就是BasicHttpBinding_XXXServerSoap,这样在Java里回调的时候就失败了,本人Java不熟,不知道Java里为啥不能自动取可用的port,非得指定命名。这里最好能把BasicHttpBinding_这个前缀去掉,查了好久好像这是写死的,BasicHttpBinding这个名字可以改,但必须是BindingName_ServiceName这种形式。
唯一有的办法就是写一个扩展,自定义WSDL导出过程,呵呵还真有人这么干
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
{
Console.WriteLine("PortNameWsdlBehaviorExtension");
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="<NameSpace>.PortNameWsdlBehaviorExtension, <NameSpace>, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
搞定,这时候再看wsdl:port的name属性已经变成你的binding name了,你在配置文件里爱怎么配置怎么配置
在基于WCF开发的Web Service导出WSDL定义问题及自定义wsdl:port 名称的更多相关文章
- 用C#基于WCF创建TCP的Service供Client端调用
本文将详细讲解用C#基于WCF创建TCP的Service供Client端调用的详细过程 1):首先创建一个Windows Service的工程 2):生成的代码工程结构如下所示 3):我们将Servi ...
- 构建一个基于 Spring 的 RESTful Web Service
本文详细介绍了基于Spring创建一个“hello world” RESTful web service工程的步骤. 目标 构建一个service,接收如下HTTP GET请求: http://loc ...
- WCF 、Web API 、 WCF REST 和 Web Service 的区别
WCF .Web API . WCF REST 和 Web Service 的区别 The .Net framework has a number of technologies that allow ...
- C# 开发XML Web Service与Java开发WebService
一.web service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量 ...
- 转 Difference between WCF and Web API and WCF REST and Web Service
http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and-WCF-R ...
- WCF、Web API、WCF REST、Web Service
WCF.Web API.WCF REST.Web Service 区别 Web Service It is based on SOAP and return data in XML form. It ...
- WCF、Web API、WCF REST、Web Service的区别
Difference between WCF and Web API and WCF REST and Web Service The .Net framework has a number of ...
- WCF、Web API、WCF REST、Web Service比较
原文地址:http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and- ...
- 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...
随机推荐
- C 语言 clock() 函数,例:计算多项式值
C 语言 clock() 函数,例:计算多项式值 /** * clock(): 捕捉从程序开始运行到 clock() 被调用时所耗费的时间. * 这个时间单位是 clock tick, 即" ...
- day03-Linux操作系统目录结构
一. Linux系统目录表示 ‘/’表示根目录: ‘.’表示当前目录 ...
- 读《JavaScript权威指南》笔记(五)
1.getComputedStyle()方法的返回值是一个CSSStyleDeclaration对象,它代表了应用在指定元素(或伪对象)上的所有样式. 2.clip style="clip: ...
- go语言实战教程之管理员查询功能、退出功能
前面第10节课内容中已经学习开发完成了管理员登陆功能.本节课我们将继续学习开发完成管理员信息查询功能.管理员退出功能 管理员信息查询功能 请求及路由映射 管理员信息查询接口 接口名称:获取管理员信息. ...
- SP18637 LAWRENCE - Lawrence of Arabia
\(\color{#0066ff}{ 题目描述 }\) 给定一个长度为n的序列,至多将序列分成m+1段,每段序列都有权值,权值为序列内两个数两两相乘之和.求序列权值和最小为多少? \(\color{# ...
- jdk 1.6.0_39 下载
Java SE Development Kit 6u39 Product / File Description File Size Download password Linux x86 65.42 ...
- Gym - 101845F 最大流
The UN finals are here!, the coaches/ex-coaches team is creating a new exciting contest to select wh ...
- 04->python字典
字典dict 1.数据类型分类(按可变和不可变) 不可变数据类型:数字.bool.str.tuple 可变数据类型:list.dict.set 2.dict的组成 key:va ...
- Qt 学习之路 2(8):添加动作
Home / Qt 学习之路 2 / Qt 学习之路 2(8):添加动作 [在WINDOWS10 QTCREATOR MENU添加无效] Qt 学习之路 2(8):添加动作 豆子 ...
- LUNA16数据集(二)肺结节可视化
在检测到肺结节后,还需要可视化,这样才能为诊断服务. 我使用的项目地址为:https://github.com/wentaozhu/DeepLung 项目基于论文:DeepLung: Deep 3D ...