在 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 });
}
}
}
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();
ServiceClient client = ServiceClientFactory<ServiceClient, Service>.CreateServiceClient();
client.DoWorkCompleted += new EventHandler<DoWorkCompletedEventArgs>(client_DoWorkCompleted);
client.DoWorkAsync();
比起直接用 new 的方式创建,多传了两个类型参数而已,但是却不需要依赖于配置文件了。