中国开发网: 论坛: 程序员情感CBD: 贴子 634099
龙之吻
又重新写了一下。
class abc
{
public static Service1Client client1
{
get
{
return ServiceProvider<Service1Client>.GetSerciceClient();
}
}
public static Service2Client client2
{
get
{
return ServiceProvider<Service2Client>.GetSerciceClient();
}
}
}


public class ServiceProvider<T> where T :class,new()
{
public static T GetSerciceClient()
{
T _instance = Singleton<T>.Instance;
Type type = typeof(T);
CommunicationState state = (CommunicationState)type.GetProperty("State").GetValue(_instance, null);
ClientCredentials ClientCredential = (ClientCredentials)type.GetProperty("ClientCredentials").GetValue(_instance, null);
if (state == CommunicationState.Faulted || state == CommunicationState.Closed)
{
LoggingService.Debug("\t通讯错误,重新创建!PickingOrderServiceClient");
type.InvokeMember("Abort", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance
| System.Reflection.BindingFlags.InvokeMethod, null, _instance, null);
_instance = null;
_instance = new T();
//_instance = (T)System.Activator.CreateInstance(type);
ClientCredential.UserName.UserName = null;
}
if (ClientCredential.UserName.UserName == null)
{
ClientCredential.UserName.UserName = WCFService.UserName;
ClientCredential.UserName.Password = WCFService.PassWord;

}
return _instance;
}
}


public class Singleton<T> where T : class, new()
{
private static T _instance;
public Singleton()
{
}
public static T Instance
{
get
{
if (_instance == null)
{
//获得实例,使用这个方法的前提是T要有公有的、无参数的构造函数        
_instance = (T)System.Activator.CreateInstance(typeof(T));

}
return _instance;
}
set
{
_instance = value;
}
}
}


机器人,这首歌学会了没有?

我们的目标是->没有蛀牙!

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录