解决在Silverlight中调用WCF控制台程序宿主时跨域问题

解决在Silverlight中调用WCF控制台程序宿主时跨域问题,第1张

概述关键代码及步骤:         1  首先在WCF类库服务中添加System.ServiceModel.Web引用(引用System.ServiceModel.Web时必须把类库设FrameWork 4)     2  定义接口IDomainService(名字可以随便,但后面必须跟配置文件保持一致)    代码如下:       [ServiceContract] public int

关键代码及步骤:

 

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


控制台应用程序除了打开之前需要打开的服务外,还需打开刚建立的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>
总结

以上是内存溢出为你收集整理的解决在Silverlight中调用WCF控制台程序宿主时跨域问题全部内容,希望文章能够帮你解决解决在Silverlight中调用WCF控制台程序宿主时跨域问题所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1065682.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存