在基于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 ...
随机推荐
- 分区表分区字段的update操作
默认情况下,oracle的分区表对于分区字段是不允许进行update操作的,如果有对分区字段行进update,就会报错——ORA-14402: 更新分区关键字列将导致分区的更改.但是可以通过打开表的r ...
- Django之表单form
在登录系统以及需要上传填入的信息时候,用的最多就是表单系统,例如像下面的这种格式 <form action="/form1/" method="post" ...
- mongoDB(1) -- 安装及开始
安装完成后在/bin文件夹下打开命令窗口 输入.\mongo启动数据库,若正常启动说明安装成功: 为了启动mongodb方便,将mongod.exe路径加入环境变量,电脑->属性->高级系 ...
- 【Linux】-Ubuntu常用命令吐血整理
前言 刚刚接触Linux操作系统,真的是各种艰难啊,用个什么东西都得从头开始配置,这个时候才明白从头再来是什么滋味了.自己装了数个数十几次的Centos版本的Linux系统,好不容易争气了一次,跑了起 ...
- GuavaCache简介(一)
原文地址 http://blog.csdn.net/guozebo/article/details/51590517 前言 在多线程高并发场景中往往是离不开cache的,需要根据不同的应用场景来需要选 ...
- P2568 GCD
\(\color{#0066ff}{ 题目描述 }\) 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. \(\color{#0066ff}{输入格式}\ ...
- 16.Shortest Unsorted Continuous Subarray(最短未排序子数组)
Level: Easy 题目描述: Given an integer array, you need to find one continuous subarray that if you onl ...
- zabbix监控java内存的脚本
#!/bin/bash # 截取java的pid号 java_pid=`netstat -lnpt |grep |awk -F '/' '{print $1'}` # 截取$jstat命令的位置 js ...
- 2-32 while
do while
- git commit 操作
查看提交历史 然后在此项目中运行 git log,应该会看到下面的输出: 合并commit 信息 我们需要将 2dfbc7e8 和 c4e858b5 合并成一个 commit,那么我们输入如下命令 ...