在基于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 ...
随机推荐
- Glib学习笔记(三)
你将学到什么 如何实现Object的方法 Object的方法 Object的public方法 在头文件声明一个函数,然后在源文件中实现函数即可 /* declaration in the header ...
- iOS工程师 - 简历
基本信息 姓 名:张学友 性 别:男 年 龄:28 学 历:本科 毕业学校:广西师范大学 专 业:通信工程 手 ...
- Go:创建新进程(os.StartProcess源码解读)
关于如何使用go语言实现新进程的创建和进程间通信,我在网上找了不少的资料,但是始终未能发现让自己满意的答案,因此我打算自己来分析这部分源代码,然后善加利用,并且分享给大家,期望大家能从中获得启发. 首 ...
- Unity苹果(iOS)内购接入(Unity内置IAP)
https://www.jianshu.com/p/4045ebf81a1c Unity苹果(iOS)内购接入(Unity内置IAP) Kakarottog ...
- yum源安装Percona
yum源安装更为简单,方便.话不多说,开始吧. 1.下载yum源 yum -y install https://www.percona.com/redir/downloads/percona-rele ...
- MVC(Java , C# ,php)
- sap程序下载
有时需要下载sap 中的程序 一屏一屏的拷贝非常麻烦 这里是在网上找的的一个下载工具 实际就是一个sap程序 建立好sap程序 把代码拷贝进去 在参数处输入程序名字 和下载位置 F8运 ...
- FPGA实战操作(1) -- SDRAM(Verilog实现)
对SDRAM基本概念的介绍以及芯片手册说明,请参考上一篇文章SDRAM操作说明. 1. 说明 如图所示为状态机的简化图示,过程大概可以描述为:SDRAM(IS42S16320D)上电初始化完成后,进入 ...
- ubuntu14 上无法使用vim命令的解决方法
在ubuntu14.10终端输入vim: The program 'vim' can be found in the following packages: * vim * vim-gnome * v ...
- 牛客Professional Manager(并查集)
t’s universally acknowledged that there’re innumerable trees in the campus of HUST. Thus a professi ...