Silverlight摆脱对 ServiceReferences.ClientConfig 的依赖

本文介绍了一种在Silverlight应用中集成WCF服务的方法,通过创建ServiceClientFactory类来动态生成WCF客户端代理,避免了配置文件的硬编码问题,使服务地址更灵活,便于跨环境迁移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在 Silverlight 中,如果用 VS 添加对 WCF Service, 的引用,则会自动生成 ServiceReferences.ClientConfig 配置文件,其中包含该 Service 的 Binding 和 Address 等信息。将配置信息隔离出来本来是好事情,但问题是,由于 Silverlight 只是一个客户端 runtime 的特性决定,配置文件将被在编译时组装到 Siverlight 的 xap 压缩包中去,这样,修改配置就会变得很麻烦,每次要修改后重新编译,重新部署。而由 VS 生成的这个 config 文件中往往包含了对 Service 所在地址的直接引用。比如  http://localhost/SL_SERVICE/Service.svc ,这样,对我们部署到生产环境是非常不方便的。

换一个做法,如果我们能将承载 Silverlight 的页面跟 WCF Service 放到同一个网站中,这样就可以用相对地址来访问到 Service. 在开发环境/测试环境/生产环境之间迁移就会变得很方便。


其中 ClientBin 下是编译生成的 Silverlight 程序的 xap 包。
根据这个结构,我们就可以做一个 ServiceClientFactory 类,可以按需创建出指定类型的 WCF 客户端代理类,而不用去读取配置文件。代码如下:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Windows;
using SL_SERVICEMANAGE.ServiceRef;
namespace SL_SERVICEMANAGE
{
    public class ServiceManger
    {
        //=======================方法一======================================
        public static ServiceClient GetDynamicClient(params string[] svcNames) 
        {
            BasicHttpBinding binding = new BasicHttpBinding(Application.Current.Host.Source.Scheme.Equals("https", StringComparison.InvariantCultureIgnoreCase) ? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.None);
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.MaxBufferSize = int.MaxValue;
            string path = "/SL_SERVICE/Service.svc";
            if (svcNames.Length > 0)
            {
                path = "/SL_SERVICE/" + svcNames[0];
            }
            return new ServiceClient(binding, new EndpointAddress(new Uri(Application.Current.Host.Source, path)));
        }

    }
    //=======================方法二===================
    public static class ServiceClientFactory<TServiceClient, TService>
        where TServiceClient : ClientBase<TService>, TService
        where TService : class 
    {
        public static TServiceClient CreateServiceClient()
        {
            var typeName = typeof(TService).Name;
            var serviceAddress = "/SL_SERVICE/" + typeName + ".svc";
            var endpointAddr = new EndpointAddress(new Uri(Application.Current.Host.Source, serviceAddress));
            var binding = new BasicHttpBinding();
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.MaxBufferSize = int.MaxValue;
            var ctor = typeof(TServiceClient).GetConstructor(new Type[] { typeof(Binding), typeof(EndpointAddress) });
            return (TServiceClient)ctor.Invoke(new object[] { binding, endpointAddr });
        }
    }

}

 这样,就可以利用类似下面的代码来创建客户端代理:

           //ServiceClient client = ServiceManger.GetDynamicClient("Service.svc");
            ServiceClient client = ServiceClientFactory<ServiceClient, Service>.CreateServiceClient();
            client.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(client_DoWorkCompleted);
            client.DoWorkAsync();


比起直接用 new 的方式创建,多传了两个类型参数而已,但是却不需要依赖于配置文件了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值