没有登录
中国开发网: 论坛: 程序员情感CBD: 贴子 633549
龙之吻
老M的代码给我指引了方向,终于写出来了。感谢老M和海豹王等牛的热心帮助
using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Security;

namespace test
{

public class c1
{
public static Service1Client Service1
{
get
{
return return Singleton<Service1Client>.Instance;
}
}
}


public class Singleton<T>
{
private static T _instance;

public Singleton()
{
}

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

Type type = typeof(T);
CommunicationState state = (CommunicationState)type.GetProperty("State").GetValue(_instance, null);
ClientCredentials ClientCredential = (ClientCredentials)type.GetProperty("ClientCredentials").GetValue(_instance, null);
UserNamePasswordClientCredential User = (UserNamePasswordClientCredential)ClientCredential.GetType().GetProperty("UserName").GetValue(ClientCredential, 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 = default(T);
_instance = (T)System.Activator.CreateInstance(type);
User.GetType().GetProperty("UserName").SetValue(User, null, null);
}
string username = (string)User.GetType().GetProperty("UserName").GetValue(User, null);
if (username == null)
{
User.GetType().GetProperty("UserName").SetValue(User, WCFService.UserName, null);
User.GetType().GetProperty("Password").SetValue(User, WCFService.PassWord, null);
}
return _instance;
}
set
{
_instance = value;
}
}
}
}


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

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

相关信息:


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