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

关键代码及步骤:

 

      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>




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值