在基于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 ...
随机推荐
- 如何在Linux上使用x2go设置远程桌面
Until ACS supports Spice, if ever,you're better off with "on-VM" softare such RDP for Wind ...
- Python中dataframe\ array\ list相互转化
import pandas as pd import numpy as np #创建列表 a1=[1,2,3] #arange函数:指定初始值.终值.步长来创建数组 a2=np.arange(0,1, ...
- B - Pie (二分)
My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a number N ...
- hdu1845(a^b的因子和%p)
题目链接:http://poj.org/problem?id=1845 思路: 1.整数唯一分解定理: 任意正整数都有且只有一种方式写出其素因子的乘积表达式. a=(p1^k1)*(p2^k2)*(p ...
- 缩点+spfa最长路【bzoj】 1179: [Apio2009]Atm
[bzoj] 1179: [Apio2009]Atm Description Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri ...
- [BOI2007]Sequence 序列问题 BZOJ1345
题目描述 对于一个给定的序列a1, …, an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai+1)替代,这样得到一个比原来序列短的新序列.这一 ...
- UVa 11292 勇者斗恶龙(The Dragon of Loowater)
首先先看一下这道题的英文原版... 好吧,没看懂... 大体意思就是: 有一条n个头的恶龙,现在有m个骑士可以雇佣去杀死他,一个能力值为x的勇士可以砍掉直径不超过x的头,而且需要支付x个金币.如何雇佣 ...
- 黑马JavaScript学习一 BOM之Window对象定时器功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- centos7上面关闭防火墙
CentOS 7.0默认使用的是firewall作为防火墙:若没有启用iptables 作为防火墙,则使用以下方式关闭防火墙: systemctl stop firewalld.service 关闭开 ...
- 使用RestTemplate时报错java.lang.IllegalStateException: No instances available for 127.0.0.1
我在RestTemplate的配置类里使用了 @LoadBalanced@Componentpublic class RestTemplateConfig { @Bean @LoadBalanced ...