1,首先,修正了一个由于List<TEntity>数据量过大导致的异常:
http://www.cnblogs.com/sinxsoft/archive/2010/11/10/1874249.html
2,修正了此问题之后,发现修改了客户端的app.config文件,这对于发布及其不爽,因为每个客户的app.config不同,这会导致发布后每个客户机上的文件一致;
于是,想用代码动态的设置终结点的行为。
3,在调试过程中,先配置好了客户端,发现了ChannelFactory<T> 的终结点行为列表已经包括了对象: System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior,通过reflector一查,发现该对象的定义为internal:
internal class DataContractSerializerServiceBehavior : IServiceBehavior, IEndpointBehavior
4,于是机会有了:可以通过反射创建对象塞入到终结点的行为列表中了:
private static ChannelFactory<IServiceChannel> GetChannelFactory()
{
if (_factory == null)
{
_factory = new ChannelFactory<IServiceChannel>("BasicHttpBinding_IService");
AttachServiceBehavior(_factory);
}
return _factory;
}
private static void AttachServiceBehavior(ChannelFactory<IServiceChannel> channelFactory)
{
//找到程序集的其中任何一个class,确定程序集
var type = typeof (ClientRuntime);
//实例化internal类,通过反射
var instance =
type.Assembly.CreateInstance("System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior",
true,
BindingFlags.CreateInstance | BindingFlags.Instance |
BindingFlags.NonPublic, null, new object[] {false, Int32.MaxValue}, null,
null);
//转换成终结点的行为接口
var behavior = instance as IEndpointBehavior;
if (behavior != null)
{
//插入到通道工厂的终结点行为集合中
channelFactory.Endpoint.Behaviors.Add(behavior);
}
}
{
if (_factory == null)
{
_factory = new ChannelFactory<IServiceChannel>("BasicHttpBinding_IService");
AttachServiceBehavior(_factory);
}
return _factory;
}
private static void AttachServiceBehavior(ChannelFactory<IServiceChannel> channelFactory)
{
//找到程序集的其中任何一个class,确定程序集
var type = typeof (ClientRuntime);
//实例化internal类,通过反射
var instance =
type.Assembly.CreateInstance("System.ServiceModel.Dispatcher.DataContractSerializerServiceBehavior",
true,
BindingFlags.CreateInstance | BindingFlags.Instance |
BindingFlags.NonPublic, null, new object[] {false, Int32.MaxValue}, null,
null);
//转换成终结点的行为接口
var behavior = instance as IEndpointBehavior;
if (behavior != null)
{
//插入到通道工厂的终结点行为集合中
channelFactory.Endpoint.Behaviors.Add(behavior);
}
}
测试,通过