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);
}
}
測試,通過