终于ok Silverlight和Wcf跨域通信问题

本文介绍如何将WCF服务部署到IIS,并解决跨域访问问题。通过添加必要的clientaccesspolicy.xml和crossdomain.xml文件到IIS根目录,确保服务能够正确响应跨域请求。此外还提供了服务接口定义和服务引用配置示例。

首先发布Wcf到IIS上

image

在发布的时候 有时候IIS无法识别svc文件 就需要添加

image

然后就是添加跨域文件

1.clientaccesspolicy.xml

<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>

2.crossdomain.xml

 

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

 

当发生跨域访问时 IIS会自动在根目录下寻找这两个文件:IIS默认跟目录C:\Inetpub\wwwroot直接添加上述两个文件

image

需要注意是:不是站点根目录 是IIS根目录

[ServiceContract]
   public interface IService1
   {
       [OperationContract]
       string GetData(int value);

       // TODO: Add your service operations here
   }

public string GetData(int value)
       {
           return string.Format("You entered: {0}", value);
       }

ServiceReferences.ClientConfig

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647">
                    <security mode="None" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://pc-200911231149/slwcf/slwcf.Service1.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="slwcf.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/slwcf/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="basicHttpBinding" contract="slwcf.IService1">
          <!--
              Upon deployment, the following identity element should be removed or replaced to reflect the
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity
              automatically.
          -->
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
        <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information,
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes,
          set the value below to true.  Set to false before deployment
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

sl:

public MainPage()
       {
           InitializeComponent();
           ServiceReference1.Service1Client sc = new ServiceReference1.Service1Client();
           sc.GetDataCompleted += new EventHandler<ServiceReference1.GetDataCompletedEventArgs>(sc_GetDataCompleted);
           sc.GetDataAsync(123);
       }

       void sc_GetDataCompleted(object sender, ServiceReference1.GetDataCompletedEventArgs e)
       {
           MessageBox.Show(e.Result);
       }

转载于:https://www.cnblogs.com/3_mu/archive/2009/12/21/1628834.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值