在自hosted的wcf服务中,启用silverlight的跨域操作

今天在学习silverlight的wcf调用操作,从中遇到了跨域操作的问题,在网上也看很多这方面的文章,其中

http://blogs.msdn.com/carlosfigueira/archive/2008/03/07/enabling-cross-domain-calls-for-silverlight-apps-on-self-hosted-web-services.aspx

 

就详细介绍了启用这种操作的方法,下面把它作一下整理。

接口:

 1:  /// <summary>
 2:     /// 策略文件获取接口
 3:     /// 此解决方案在于,可以让非IIS托管
 4:     /// 的wcf服务也可以作到跨域策略文件的设置
 5:     /// </summary>
 6:     [ServiceContract]
 7:     public interface IPolicyRetriever
 8:     {
 9:         [OperationContract, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
10:         Stream GetSilverlightPolicy();
11:         [OperationContract, WebGet(UriTemplate = "/crossdomain.xml")]
12:         Stream GetFlashPolicy();
13:     }

 

实现:

 

 1:  [ServiceBehavior]
 2:      public class PolicyRetriever : IPolicyRetriever
 3:      {
 4:          Stream StringToStream(string result)
 5:          {
 6:              WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
 7:              return new MemoryStream(Encoding.UTF8.GetBytes(result));
 8:          }
 9:   
10:          [OperationBehavior, WebGet(UriTemplate = "/clientaccesspolicy.xml")]
11:          public Stream GetSilverlightPolicy()
12:          {
13:              string result = @"<?xml version=""1.0"" encoding=""utf-8""?>
14:  <access-policy>
15:  <cross-domain-access>
16:  <policy>
17:  <allow-from http-request-headers=""*"">
18:  <domain uri=""*""/>
19:  </allow-from>
20:  <grant-to>
21:  <resource path=""/"" include-subpaths=""true""/>
22:  </grant-to>
23:  </policy>
24:  </cross-domain-access>
25:  </access-policy>";
26:              // return result;
27:              return StringToStream(result);
28:          }
29:          [OperationBehavior, WebGet(UriTemplate = "/crossdomain.xml")]
30:          public Stream GetFlashPolicy()
31:          {
32:              string result = @"<?xml version=""1.0""?>
33:  <!DOCTYPE cross-domain-policy SYSTEM ""http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"">
34:  <cross-domain-policy>
35:  <allow-access-from domain=""*"" />
36:  </cross-domain-policy>";
37:              return StringToStream(result);
38:          }
39:      }

 

测试程序段:

 1:  /// <summary>
 2:          /// 在测试时,浏览器中输入:http://MachineName:8000/clientaccesspolicy.xml
 3:          /// 或http://MachineName:8000/crossdomain.xml
 4:          /// 如果访问成功,说明策略文件设置成功
 5:          /// </summary>
 6:          public static void Test()
 7:          {
 8:              string baseAddress = "http://" + Environment.MachineName + ":8000";
 9:              ServiceHost host = new ServiceHost(typeof(PolicyRetriever), new Uri(baseAddress));
10:              host.AddServiceEndpoint(typeof(IPolicyRetriever), new BasicHttpBinding(), "basic");
11:              host.AddServiceEndpoint(typeof(IPolicyRetriever), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
12:              ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
13:              smb.HttpGetEnabled = true;
14:              host.Description.Behaviors.Add(smb);
15:              host.Open();
16:              Console.WriteLine("Host opened");
17:              Console.Write("Press ENTER to close");
18:              Console.ReadLine();
19:              host.Close();
20:          }

 

配置文件的写法:

 1:  <?xml version="1.0" encoding="utf-8" ?>
 2:  <configuration>
 3:    <system.web>
 4:      <compilation debug="true" />
 5:    </system.web>
 6:    <!-- When deploying the service library project, the content of the config file must be added to the host's 
 7:    app.config file. System.Configuration does not support config files for libraries. -->
 8:    <system.serviceModel>
 9:      <services>      
10:        <service name="WfcSilverLightCrossDomain.PolicyRetriever" behaviorConfiguration="WfcSilverLightCrossDomain.Service1Behavior">
11:          <host>
12:            <baseAddresses>
13:              <add baseAddress = "http://localhost:8000/" />
14:            </baseAddresses>
15:          </host>
16:          <endpoint address ="" binding="webHttpBinding" contract="WfcSilverLightCrossDomain.IPolicyRetriever" behaviorConfiguration="webby">
17:            <identity>
18:              <dns value="localhost"/>
19:            </identity>
20:          </endpoint>
21:          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
22:        </service>
23:      </services>
24:      <behaviors>
25:        <endpointBehaviors>
26:          <behavior name="webby">
27:            <webHttp/>
28:          </behavior>
29:        </endpointBehaviors>
30:        <serviceBehaviors>
31:          <behavior name="WfcSilverLightCrossDomain.Service1Behavior">
32:            <!-- To avoid disclosing metadata information, 
33:            set the value below to false and remove the metadata endpoint above before deployment -->
34:            <serviceMetadata httpGetEnabled="True"/>
35:            <!-- To receive exception details in faults for debugging purposes, 
36:            set the value below to true.  Set to false before deployment 
37:            to avoid disclosing exception information -->
38:            <serviceDebug includeExceptionDetailInFaults="False" />
39:          </behavior>
40:        </serviceBehaviors>
41:      </behaviors>
42:    </system.serviceModel>
43:  </configuration>
44:  

只有在浏览器中,能正常访问到跨域文件内容,才能正常,另外在win7下测试,要使用管理员权限运行vs2008.

Bookmark and Share

转载于:https://www.cnblogs.com/LifelongLearning/archive/2010/02/03/1662976.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值