AJAX调用WCF的配置

前端之家收集整理的这篇文章主要介绍了AJAX调用WCF的配置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
AJAX调用WCF的配置

今天看了.net平台SOA架构的部分书籍,自己也试着做些例子。本以为会比较顺利,结果还是费了些周折,兹记下以备查阅。

先列出SVC的内容

namespace WcfFundamentals
{
[ServiceContract(Namespace = "SimpleXmlService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SimpleXmlService
{
// 添加 [WebGet] 属性以使用 HTTP GET
[WebGet]
[OperationContract]
public XmlElement HelloWorld()
{
var doc = new XmlDocument();
doc.InnerXml = "<message>Hello XML World!</message>";
return doc.DocumentElement;
}
}
}

写法
1
下面这样可以输出WSDL:但是不能被AJAX以HTTP Get访问

<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>
简评:这个是常见的情况,ASP.net调这个服务是能用的;要让JavaScript的HTTP Get方式调用Web服务除了方法上加属性外,Web.Config也要做配置,上面就是因为没有<webHttp/>配置。

写法2:(错误的写法

<system.serviceModel>

<behaviors>

<endpointBehaviors>

</endpointBehaviors>

<serviceBehaviors>

<behavior name="PoxBehavior">

@H_569_301@ <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>
简评:<webHttp/>配置是加上了,但是加的地方不对,因为<webHttp/>是针对endpointBehaviors的,放在<serviceBehaviors>节不对。错误也是反面经验,所以列出来,去伪方存真。

写法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>
</system.serviceModel>

简评<webHttp/>配置的地方是对了,但是不起作用,因为<service>节的behaviorConfiguration属性不会指向<endpointBehaviors>节,只认 <serviceBehaviors>节。既不能输出WSDL,AJAX HTTP Get也不能访问。

写法4AJAX 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>
简评为了让behaviorConfiguration属性指向<endpointBehaviors>节,需要配置endpoint的(而不是service 的behaviorConfiguration,终于AJAX能访问了。但是问题又来了,不能用IIS生成WSDL,这意味着无法使用IDE工具配合C#来测试方法

写法5AJAX 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分别配置,指向不同的行为配置节,这是彻底解决方法,也是两全其美的方法


写法
6
:恶搞的写法(是可以用的)

<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并不冲突。

猜你在找的Ajax相关文章