解决在Silverlight中调用WCF控制台程序宿主时跨域问题
2012-08-01 12:01
459 查看
关键代码及步骤:
1 首先在WCF类库服务中添加System.ServiceModel.Web引用(引用System.ServiceModel.Web时必须把类库设FrameWork 4)
2 定义接口IDomainService(名字可以随便,但后面必须跟配置文件保持一致)
代码如下:
实现接口IDomainService
3 控制台应用程序除了打开之前需要打开的服务外,还需打开刚建立的DomainService服务,代码如下:
4 控制台中app.config中添加DomainServiceBehavior配置节点(红色部分为新增节点)
1 首先在WCF类库服务中添加System.ServiceModel.Web引用(引用System.ServiceModel.Web时必须把类库设FrameWork 4)
2 定义接口IDomainService(名字可以随便,但后面必须跟配置文件保持一致)
代码如下:
[ServiceContract] public interface IDomainService { [OperationContract] [WebGet(UriTemplate = "ClientAccessPolicy.xml")] Message ProvidePolicyFile(); }
实现接口IDomainService
public class DomainService : IDomainService { public System.ServiceModel.Channels.Message ProvidePolicyFile() { FileStream filestream = File.Open(@"ClientAccessPolicy.xml", FileMode.Open); XmlReader reader = XmlReader.Create(filestream); System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader); return result; } }
3 控制台应用程序除了打开之前需要打开的服务外,还需打开刚建立的DomainService服务,代码如下:
namespace ConsoleApplicationHttp { class Program { static ServiceHost host; static void Main(string[] args) { try { host = new ServiceHost(typeof(WCFNetHttpLib.Service1));//Wcf服务 host.Open(); ServiceHost crossDomainserviceHost = new ServiceHost(typeof(WCFNetHttpLib.DomainService));//跨域处理服务 crossDomainserviceHost.Open(); Console.WriteLine("my,service opened!"); Console.ReadKey(); } catch (Exception ex) { Console.Write("service opened failed!" + ex.Message); Console.ReadKey(); } host.Close(); } } }
4 控制台中app.config中添加DomainServiceBehavior配置节点(红色部分为新增节点)
<?xml version="1.0"?> <configuration> <system.serviceModel> <services> <service behaviorConfiguration="ServiceBehavior" name="WCFNetHttpLib.Service1"> <endpoint address="http://localhost:8080/Service1/" binding="basicHttpBinding" bindingConfiguration="" contract="WCFNetHttpLib.IService1"/> <endpoint address="http://localhost:8080/Service1/MEX/" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange"/> </service> <service name="WCFNetHttpLib.DomainService"> <endpoint address="" behaviorConfiguration="DomainServiceBehavior" binding="webHttpBinding" contract="WCFNetHttpLib.IDomainService" /> <host> <baseAddresses> <add baseAddress="http://localhost:8080/" /> </baseAddresses> </host> </service> </services> <behaviors> <endpointBehaviors> <behavior name="DomainServiceBehavior"> <webHttp/> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration>
相关文章推荐
- WCF服务以控制台程序为宿主时的跨域访问问题(附源码)
- WCF服务以控制台程序为宿主时的跨域访问问题(附源码) (转载)
- 解决 Silverlight 调用 WCF 服务 跨域访问 和 Silverlight 引用服务后配置文件不加载的问题
- 解决 Silverlight 调用 WCF 服务 跨域访问 和 Silverlight 引用服务后配置文件不加载的问题
- 解决 Silverlight 调用 WCF 服务 跨域访问 和 Silverlight 引用服务后配置文件不加载的问题
- WCF服务以控制台程序为宿主时的跨域访问问题(附源码)
- Silverlight跨域调用WCF服务安全问题
- 自定义WebSerivce作为代理解决SilverLight跨域调用WebService问题
- silverlight调用webservice跨域导致问题的解决方法
- Silverlight访问Window服务为宿主的WCF跨域问题解决方案.
- Silverlight 2 Beta2调用WebService跨域问题解决方案
- Silverlight跨域,Silverlight在IIS中部署等问题解决之道
- 解决存储过程执行快,但程序调用则执行慢的问题
- Silverlight调用Webservice出现的跨域问题
- 用nginx解决webstorm内置server调用本地api的跨域问题
- 解决matlab中无法调用c程序的问题
- (Java控制台程序版)递归打包整个父文件夹下的文件和子文件夹成压缩文件(*.ZIP)以及打包时常见的文件乱码和文件名乱码以及丢失文件或文件损坏问题解决办法
- Android问题集锦之八:调用其他程序中的activity和Permission Denial: starting Intent 错误解决办法
- 程序中和有js函数的网页交互,线程中调用 get_Script 就会错误的解决方法,由于COM的线程安全问题
- SpringMVC解决跨域问题,以及跨服务器调用时候session丢失的问题总结!!!