AJAX调用WCF的配置
AJAX调用WCF的配置
今天看了.net平台SOA架构的部分书籍,自己也试着做些例子。本以为会比较顺利,结果还是费了些周折,兹记下以备查阅。 <system.serviceModel> <behaviors> <endpointBehaviors> </endpointBehaviors>
<serviceBehaviors> <behavior name="PoxBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <services> <service name="WcfFundamentals.SimpleXmlService" behaviorConfiguration="PoxBehavior"> <endpoint address="" binding="webHttpBinding"contract="WcfFundamentals.SimpleXmlService" /> </service> </services> </system.serviceModel>
写法2:(错误的写法) <system.serviceModel> <behaviors> <endpointBehaviors> </endpointBehaviors>
<serviceBehaviors> <behavior name="PoxBehavior"> <webHttp/> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <services> <service name="WcfFundamentals.SimpleXmlService"behaviorConfiguration="PoxBehavior"> <endpoint address="" binding="webHttpBinding"contract="WcfFundamentals.SimpleXmlService" /> </service> </services> </system.serviceModel>
写法3:(错误的写法) <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="PoxBehavior"> <webHttp /> </behavior> </endpointBehaviors>
<serviceBehaviors> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <services> <service name="WcfFundamentals.SimpleXmlService"behaviorConfiguration="PoxBehavior"> <endpoint address=""binding="webHttpBinding"contract="WcfFundamentals.SimpleXmlService" /> </service> </services> 简评:<webHttp/>配置的地方是对了,但是不起作用,因为<service>节的behaviorConfiguration属性不会指向<endpointBehaviors>节,只认 <serviceBehaviors>节。既不能输出WSDL,AJAX HTTP Get也不能访问。 写法4:AJAX HTTP Get能够访问的写法:(但是不能用IIS生成WSDL,提示“当前已禁用此服务的元数据发布”) <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="PoxBehavior"> <webHttp /> </behavior> </endpointBehaviors>
<serviceBehaviors> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <services> <service name="WcfFundamentals.SimpleXmlService" > <endpoint address="" behaviorConfiguration="PoxBehavior"binding="webHttpBinding"contract="WcfFundamentals.SimpleXmlService" /> </service> </services> </system.serviceModel> 写法5:AJAX HTTP Get能够访问的写法,也能用IIS生成WSDL <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="PoxBehavior"> <webHttp /> </behavior> </endpointBehaviors>
<serviceBehaviors> <behavior name="PoxBehavior2"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <services> <service name="WcfFundamentals.SimpleXmlService" behaviorConfiguration="PoxBehavior2"> <endpoint address=""behaviorConfiguration="PoxBehavior"binding="webHttpBinding" contract="WcfFundamentals.SimpleXmlService" /> </service> </services> </system.serviceModel> 简评:为了能AJAX HTTP Get能够访问,也能用IIS生成WSDL,需要对service和endpoint的behaviorConfiguration分别配置,指向不同的行为配置节,这是彻底解决方法,也是两全其美的方法。
<system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="PoxBehavior"> <webHttp /> </behavior> </endpointBehaviors>
<serviceBehaviors> <behavior name="PoxBehavior"> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> <services> <service name="WcfFundamentals.SimpleXmlService" behaviorConfiguration="PoxBehavior"> <endpoint address="" behaviorConfiguration="PoxBehavior"binding="webHttpBinding" contract="WcfFundamentals.SimpleXmlService" /> </service> </services> </system.serviceModel>简评:之所以称恶搞,是因为endpointBehaviors和serviceBehaviors节各有一个同名的PoxBehavior。并且这样的配置是可以被AJAX HTTP Get访问,也能用IIS生成WSDL的。于是在不知缘由的情况下,容易被这个搞蒙。这是因为service的behaviorConfiguration属性指向serviceBehaviors下的PoxBehavior,而endpoint的behaviorConfiguration属性指向endpointBehaviors下的PoxBehavior,endpointBehaviors和serviceBehaviors节各自的PoxBehavior并不冲突。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |