110,566
社区成员
发帖
与我相关
我的任务
分享
internal static class ChannelFactoryCreator
{
private static Hashtable channelFactories = new Hashtable();
private static string ServerAddress;
public static ChannelFactory<T> Create<T>(string ServerName)
{
if (string.IsNullOrEmpty(ServerName))
{
throw new ArgumentNullException("ServerName");
}
ChannelFactory<T> channelFactory = null;
if (channelFactories.ContainsKey(ServerName))
{
channelFactory = channelFactories[ServerName] as ChannelFactory<T>;
}
if (channelFactory == null)
{
NetTcpBinding ntb = new NetTcpBinding();
ntb.Security.Mode = SecurityMode.None;
ntb.TransferMode = TransferMode.Streamed;
ntb.MaxBufferSize = 10240000;
ntb.MaxReceivedMessageSize = 10240000;
ntb.ReaderQuotas.MaxArrayLength = 10240000;
ntb.ReaderQuotas.MaxBytesPerRead = 10240000;
ntb.ReaderQuotas.MaxDepth = 20;
ntb.ReaderQuotas.MaxNameTableCharCount = 10240000;
ntb.ReaderQuotas.MaxStringContentLength = 10240000;
ntb.CloseTimeout = TimeSpan.Parse("00:10:00");
ntb.ReceiveTimeout = TimeSpan.Parse("00:10:00");
ntb.SendTimeout = TimeSpan.Parse("00:10:00");
ntb.OpenTimeout = TimeSpan.Parse("00:10:00");
if (string.IsNullOrEmpty(ServerAddress))
{
string filepath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\config.xml";
XmlDocument XmlDoc = new XmlDocument();
XmlDoc.Load(filepath);
XmlNode xn = XmlDoc.SelectSingleNode("/ClientConfig/ServerInfoList/ServerInfo[@isSelected='1']");
ServerAddress = xn.Attributes["AddressAndPort"].Value.ToString();
}
EndpointAddress ea = new EndpointAddress("net.tcp://" + ServerAddress + ServerName);
channelFactory = new ChannelFactory<T>(ntb, ea);
lock (channelFactories.SyncRoot)
{
channelFactories[ServerName] = channelFactory;
}
}
return channelFactory;
}
}
public class ServiceRealProxy<T> : RealProxy
{
private string _ServerName;
public ServiceRealProxy(string ServerName)
: base(typeof(T))
{
if (string.IsNullOrEmpty(ServerName))
{
throw new ArgumentNullException("ServerName");
}
this._ServerName = ServerName;
}
public override IMessage Invoke(IMessage msg)
{
T channel = ChannelFactoryCreator.Create<T>(this._ServerName).CreateChannel();
IMethodCallMessage methodCall = (IMethodCallMessage)msg;
IMethodReturnMessage methodReturn = null;
object[] copiedArgs = Array.CreateInstance(typeof(object), methodCall.Args.Length) as object[];
methodCall.Args.CopyTo(copiedArgs, 0);
try
{
object returnValue = methodCall.MethodBase.Invoke(channel, copiedArgs);
methodReturn = new ReturnMessage(returnValue, copiedArgs, copiedArgs.Length, methodCall.LogicalCallContext, methodCall);
(channel as ICommunicationObject).Close();
}
catch (Exception ex)
{
if (ex.InnerException is CommunicationException || ex.InnerException is TimeoutException)
{
(channel as ICommunicationObject).Abort();
}
if (ex.InnerException != null)
{
methodReturn = new ReturnMessage(ex.InnerException, methodCall);
}
else
{
methodReturn = new ReturnMessage(ex, methodCall);
}
}
return methodReturn;
}
}
public static class ServiceProxyFactory
{
public static T Create<T>(string ServerName)
{
if (string.IsNullOrEmpty(ServerName))
{
throw new ArgumentNullException("ServerName");
}
return (T)(new ServiceRealProxy<T>(ServerName).GetTransparentProxy());
}
}
GZipMessageEncodingBindingElement compBindingElement = new GZipMessageEncodingBindingElement(new BinaryMessageEncodingBindingElement());