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

源:http://www.cnblogs.com/lensso/archive/2011/04/26/2029360.html

当客户端为silverlight或者其它不默认支持跨域的技术时,需要为wcf手动开启跨域,详细方法如下:

首先跨域文件是必须的

ClientAccessPolicy.xml

 

<?xml version="1.0" encoding="utf-8"?>
 <access-policy>
  <cross-domain-access>
    <policy>
      <allow-from http-request-headers="*">
        <domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

 

CrossDomain.xml

 

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

 

以上两个跨域文件需放在部署在iis上的根目录下。

服务端config配置如下:

 

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="LoggerBehavior">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="true" />       
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
    </extensions>
    <bindings>
      <basicHttpBinding>
        <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">
          <readerQuotas maxDepth="32" maxStringContentLength="16777216" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None"></security>       
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="LoggerBehavior" name="eTank.OIU.Service.OIUService" >
        <endpoint  binding="basicHttpBinding" bindingConfiguration="UnSecureConversationBinding" contract="eTank.OIU.Contract.IOIUUC" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
      <service behaviorConfiguration="LoggerBehavior" name="eTank.OIU.Service.SMSConfigService" >
        <endpoint  binding="basicHttpBinding" bindingConfiguration="UnSecureConversationBinding" contract="eTank.OIU.Contract.ISMSConfigUC" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
  </system.serviceModel>

 

以上服务器配置需要注意的地方为:

1.behavior中的<serviceMetadata httpGetEnabled="true" />是必须的;

2.binding中的安全级别设为none,如:<security mode="None"></security>;

3.每个服务添加一个元数据的终结点配置,如:<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>;

 

完成以上配置以后silverlight等客户端即可以实现跨域访问wcf服务了。

 

 

----------------------------------------------

今天用这种办法试了(在版本控制器外厕所),但是怎么都不行,把版本控制器中的两个一样的xml文件和web.config 文件复制过来,直接ok,感觉莫名其妙。

不知道哪里的问题,有朋友了解情况,望留言。

 

posted on 2013-11-13 11:14  中子持心  阅读(416)  评论(0编辑  收藏  举报

导航