Sileverlight很多时候需要通过wcf和后台,程序进行交互。如果 iis was托管还好,极端的遇到自托管的程序,console,windowsservice,winform,wpf等,就会出现跨域问题。网上很多解决方式。俺在以下博文基础上又总结了点。

  以下博文可以先学习下:

    http://blog.csdn.net/boyhxy/article/details/5224112

    http://blog.sina.com.cn/s/blog_74066ace0100vhs5.html

    http://www.cnblogs.com/lxblog/archive/2012/08/02/2620393.html


以下是个人总结

     1.此解决方案与Silverlight版本无关。

     2.可以跨进程托管Domain

     3.可以不写配置文件托管Domain和Service

     4.可以用 Stream和Message当Domain返回值

     5.不通过配置实现的自托管代码如下:


跨域解决方案

     方案1:

  1.在项目中加入clientaccesspolicy.xml,编译生成内容,复制=true

  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>

      2. 加入域提供类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Web;
  9. using System.ServiceModel.Activation;
  10. using System.ServiceModel.Channels;
  11. using System.Xml;
  12. namespace TEST
  13. {
  14. public class DomainService : IDomainService
  15. {
  16. public System.ServiceModel.Channels.Message ProvidePolicyFile()
  17. {
  18. XmlReader reader = XmlReader.Create("clientaccesspolicy.xml");
  19. System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);
  20. return result;
  21. }
  22.  
  23. }
  24.  
  25. [ServiceContract]
  26.  
  27. public interface IDomainService
  28. {
  29. [OperationContract]
  30. [WebGet(UriTemplate = "/clientaccesspolicy.xml")]
  31. System.ServiceModel.Channels.Message ProvidePolicyFile();
  32. }
  33. }

     3.自托管 启动跨域访问

  1. //启动跨域访问
  2. ServiceHost crossDomainserviceHost = new ServiceHost(typeof(DomainService));
  3. crossDomainserviceHost.AddServiceEndpoint(typeof(IDomainService), new WebHttpBinding()
  4. , "http://127.0.0.1:4514/");
  5.  
  6. crossDomainserviceHost.Description.Endpoints[].Behaviors.Add(new WebHttpBehavior());
  7. crossDomainserviceHost.Open();

注:

            优点:跨域访问的返回文件类型是:Content-Type: application/xml 。IE,Chrome都测试过,都可以使用。

            缺点:客户端过多可能对造成过多的IO请求,需要做多客户端压力测试。


  方案2:

   1.在项目中加入clientaccesspolicy.xml,编译生成嵌入资源

    2.加入域提供类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Web;
  9. using System.ServiceModel.Activation;
  10. using System.ServiceModel.Channels;
  11. using System.Xml;
  12. namespace Test
  13. {
  14. public class DomainService : IDomainService
  15. {
  16. public Stream ProvidePolicyFile()
  17. {
  18.  
  19. var policyfile = "Test.clientaccesspolicy.xml";
  20. Stream result= Assembly.GetExecutingAssembly().GetManifestResourceStream(policyfile);

  21. return result;
  22. }
  23. }
  24.  
  25. [ServiceContract]
  26.  
  27. public interface IDomainService
  28. {
  29. [OperationContract]
  30. [WebGet(UriTemplate = "/clientaccesspolicy.xml")]
  31. Stream ProvidePolicyFile();
  32.  
  33. }
  34. }

     3.自托管 启动跨域访问

 注:

            优点:没有过多的IO请求。

            缺点:跨域访问的返回文件类型是:Content-Type: application/octet-stream 。IE可用,Chrome不可用。


     方案3:

         1.加入域提供类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.ServiceModel;
  8. using System.ServiceModel.Web;
  9. using System.ServiceModel.Activation;
  10. using System.ServiceModel.Channels;
  11. using System.Xml;
  12. namespace TEST
  13. {
  14. public class DomainService : IDomainService
  15. {
  16. public Stream ProvidePolicyFile()
  17. {
  18. string clientAccessPolicy = "<?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>";
  19. return StringToStream(clientAccessPolicy);
  20. }
  21.  
  22. private Stream StringToStream(string result)
  23. {
  24. WebOperationContext.Current.OutgoingResponse.ContentType = "application/xml";
  25.  
  26. return new MemoryStream(Encoding.UTF8.GetBytes(result));
  27. }
  28.  
  29. }
  30.  
  31. [ServiceContract]
  32.  
  33. public interface IDomainService
  34. {
  35. [OperationContract]
  36. [WebGet(UriTemplate = "/clientaccesspolicy.xml")]
  37. Stream ProvidePolicyFile();
  38.  
  39. }
  40. }

     3.自托管 启动跨域访问

 注:

            优点:没有过多的IO请求。跨域访问的返回文件类型是Content-Type: application/xml。IE,Chrome可用

            缺点:指定了编码方式,可能带来局限性。

   


总结:

              方案3,1一般情况均较好。个人喜欢第3种。


      --Domain--
  ServiceHost crossDomainserviceHost = new ServiceHost(typeof(DomainService));
  crossDomainserviceHost.AddServiceEndpoint(typeof(IDomainService), new WebHttpBinding()
     , "http://localhost:9090/");

  crossDomainserviceHost.Description.Endpoints[0].Behaviors.Add(new WebHttpBehavior());
  crossDomainserviceHost.Open();

  --Service--

  ServiceHost testHost = new ServiceHost(typeof(WCFService), new Uri("http://localhost:9090/WCFService/"));
  testHost.AddServiceEndpoint(typeof(IWCFService), new BasicHttpBinding()
    , "");

  ServiceMetadataBehavior bb = new ServiceMetadataBehavior();
  bb.HttpGetEnabled = true;
  bb.HttpGetUrl = new Uri("http://localhost:9090/WCFService/mex");
  testHost.Description.Behaviors.Add(bb);

  ServiceCredentials credential = new ServiceCredentials();
  testHost.Description.Behaviors.Add(credential);

  testHost.Open();

    注意:请用“http://localhost:9090/WCFService/mex”来寻找服务,目前水平有限,还不能直接通过代码实现和配置一样通过http://localhost:9090/WCFService/"来寻找服务。望大家知道的能回复答案。

 

Silverlight 调用自托管的wcf 报跨域异常的处理的更多相关文章

  1. chrome,opera..通过file协议浏览html代码时,发送的ajax请求本地文件,会报跨域错误

    XMLHttpRequest cannot loadfile:///E:/webs/extJS/ext-3.3.0/examples/csdn/combobox.txt?_dc=14147389739 ...

  2. Silverlight+WCF实现跨域调用

    在这篇文章中.WCF扮演server,向外提供LoginVaild服务.Silverlight扮演client.调用WCF提供的LoginVaild服务.思路有了.以下进行代码实现. 数据库脚本实现 ...

  3. 关于C#调用非托管DLL,报“内存已损坏的”坑,坑,坑

    因客户需求,与第三方对接,调用非托管DLL,之前正常对接的程序,却总是报“内存已损坏的异常”,程序进程直接死掉,折腾到这个点(2018-05-11 00:26),终于尘埃落定,直接上程序. 之前的程序 ...

  4. springboot 服务工程,前端服务调用接口报跨域错误

    前后端分离,VUE.JS调用服务接口时,跨域错误.需要服务接口工程设置,如下: @SpringBootApplicationpublic class SpringCloudOpenapiApplica ...

  5. 前端页面调用Spring boot接口发生的跨域问题

    最近要重构一个基于spring boot的后端API服务,需要再本地测试.在本地测试时,运行在本地的前端页面发送一个ajax请求访问后端API,然后浏览器报错blocked CORS policy. ...

  6. fetch各种报跨域错误,数据无法获取的解决方案

    1.介绍 fetch 提供了一个获取资源的接口 (包括跨域). fetch 的核心主要包括:Request , Response , Header , Body 利用了请求的异步特性 --- 它是基于 ...

  7. 跨域解决方案之JSONP,通过借助调用百度搜索的API了解跨域案例

    跨域解决方案之JSONP 同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web ...

  8. wcf项目跨域问题处理

    最近做了一个wcf项目,请求发起的项目是一个webform项目,所以这是分开的两个项目端口必然不一样,理所当然存在跨域问题. 有的人当下就反应过来jsonp,jsonp只能用于get请求,对于参数比较 ...

  9. WCF ajax跨域配置

    webconfig必须配置 binding="webHttpBinding" <service name="Hezi.MsgService.Send"&g ...

随机推荐

  1. 调整 FMX Android 文字显示「锯齿」效果

    说明:调整 Firemonkey Android 显示文字有「锯齿」效果 适用:Firemonkey Android 平台 修改方法: 请将源码 FMX.FontGlyphs.Android.pas  ...

  2. 修改thinkphp路由模式,去掉Home

    第一步:入口文件增加 define('BIND_MODULE', 'Home'); 第二步:修改config文件,我这里路由模式设置为2 效果展示:

  3. ubuntu 下安装memcache 以及php扩展

    1,下载软件   下载   memcached   http://memcached.org/downloads   下载libevent http://libevent.org/     2,安装  ...

  4. 基本I/O模型与Epoll简介

    5种基本的I/O模型:1)阻塞I/O ;2)非阻塞I/O; 3)I/O复用(select和poll);4)信号驱动I/O(SIGIO);5)异步I/O(POSIX.1的aio_系列函数). 操作系统中 ...

  5. Dewplayer 音乐播放器

    Dewplayer 是一款用于 Web 的轻量级 Flash 音乐播放器.提供有多种样式选择,支持播放列表,并可以通过 JavaScript 接口来控制播放器. 注意事项: 该播放器只支持 mp3 格 ...

  6. 苹果浏览器和uc浏览器在移动端的坑(日常积累,随时更新)

    先mark 1 .  移动端uc浏览器不兼容css3 calc() 2 .  ie8下a标签没有内容给宽高也不能触发点击跳转 3 . safari输入框加上readOnly="ture&qu ...

  7. .NET web开发之WebApi初试水

    前几天看了.NET的EF(Entity Framework),发现居然有这么先进的东西,只要操作几个类就可以完成数据库的增删查改,而且可以用数据库直接导出类(DB First).也可以用类来生成数据库 ...

  8. [android]亲自破解Flappy Bird(去广告+永生)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3544785.html  听说最近Flappy Bird很火,但 ...

  9. git stash提交PR的正确步骤&git squash技术

    1.git stash梳理 1.1git stash的克隆与同步 首先整理下git stash的逻辑是这样 在本地做出了新的修改,提交时显示当前的版本不是最新版本,这时就需要先pull一下自己代码仓库 ...

  10. UIColor与PatternImage

    UIColor有一个方法叫做+ (UIColor *)colorWithPatternImage:(UIImage *)image;. 返回的是一个UIColor,但没有明确的RGB值,所以叫做pat ...