您的位置:首页 > 其它

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

2012-08-01 12:01 459 查看
关键代码及步骤:



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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐