封云亭 发表于 2015-8-17 05:05:04

wcf寄宿在iis上的跨域访问问题【不止是添加跨域文件】

  当客户端为silverlight或者其它不默认支持跨域的技术时,需要为wcf手动开启跨域,详细方法如下:
  首先跨域文件是必须的
  ClientAccessPolicy.xml
  



1 <?xml version="1.0" encoding="utf-8"?>
2<access-policy>
3   <cross-domain-access>
4   <policy>
5       <allow-from http-request-headers="*">
6         <domain uri="*"/>
7       </allow-from>
8       <grant-to>
9         <resource path="/" include-subpaths="true"/>
10       </grant-to>
11   </policy>
12   </cross-domain-access>
13 </access-policy>

  CrossDomain.xml
  



1 <?xml version="1.0"?>
2 <cross-domain-policy>
3   <allow-access-from domain="*" />
4 </cross-domain-policy>

  以上两个跨域文件需放在部署在iis上的根目录下。
  服务端config配置如下:
  



1 <system.serviceModel>
2   <behaviors>
3       <serviceBehaviors>
4         <behavior name="LoggerBehavior">
5         <serviceDebug includeExceptionDetailInFaults="true" />
6         <serviceMetadata httpGetEnabled="true" />      
7         </behavior>
8       </serviceBehaviors>
9   </behaviors>
10   <extensions>
11   </extensions>
12   <bindings>
13       <basicHttpBinding>
14         <binding name="UnSecureConversationBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="1048576" maxBufferPoolSize="2097152" maxReceivedMessageSize="1048576" messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" useDefaultWebProxy="true">
15         <readerQuotas maxDepth="32" maxStringContentLength="16777216" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
16         <security mode="None"></security>      
17         </binding>
18       </basicHttpBinding>
19   </bindings>
20   <services>
21       <service behaviorConfiguration="LoggerBehavior" name="eTank.OIU.Service.OIUService" >
22         <endpointbinding="basicHttpBinding" bindingConfiguration="UnSecureConversationBinding" contract="eTank.OIU.Contract.IOIUUC" />
23         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
24       </service>
25       <service behaviorConfiguration="LoggerBehavior" name="eTank.OIU.Service.SMSConfigService" >
26         <endpointbinding="basicHttpBinding" bindingConfiguration="UnSecureConversationBinding" contract="eTank.OIU.Contract.ISMSConfigUC" />
27         <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
28       </service>
29   </services>
30   </system.serviceModel>

  以上服务器配置需要注意的地方为:
  1.behavior中的<serviceMetadata httpGetEnabled="true" />是必须的;
  2.binding中的安全级别设为none,如:<security mode="None"></security>;
  3.每个服务添加一个元数据的终结点配置,如:<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>;
  

  完成以上配置以后silverlight等客户端即可以实现跨域访问wcf服务了。
页: [1]
查看完整版本: wcf寄宿在iis上的跨域访问问题【不止是添加跨域文件】