/// <summary> /// 创建一个EtpException实例,用以封装Etp返回的错误消息。 /// </summary> /// <param name="etp">返回错误消息的平台的名称。</param> /// <param name="code">主错误码。</param> /// <param name="description">主错误描述。</param> /// <param name="subCode">子错误码。</param> /// <param name="subDescription">子错误描述。</param> public static EtpException Create(EtpName etp, string code, string description, string subCode, string subDescription) { //通过容器返回指定平台的异常构造器 IEtpExceptionBuilder builder = EtpExceptionBuilderContainer.Current.GetBuilder(etp); //创建异常 return builder.Create(code, description, subCode, subDescription); }
/// <summary> /// 获取对应于指定平台的授权提供程序 /// </summary> /// <param name="etpName">平台名称。</param> public IAuthorizationProvider GetProvider(EtpName etpName) { IAuthorizationProvider provider = null; if (dicCatheProvider.ContainsKey(etpName.Name.ToLower().Trim())) { //从缓存中取 provider = dicCatheProvider[etpName.Name.ToLower().Trim()]; } else { lock (lockO) { if (!dicCatheProvider.ContainsKey(etpName.Name.ToLower().Trim())) { //从配置文件创建 OAuthLoginConfig freepEtp = (OAuthLoginConfig)ConfigurationManager.GetSection("OAuthLogin"); var authorizationProvider = freepEtp.AuthorizationProvider[etpName.Name.ToLower().Trim()]; Assembly assembly = Assembly.Load(authorizationProvider.Assembly); //反射创建提供程序实例 object obj = assembly.CreateInstance(authorizationProvider.Type); //添加缓存 authorizationProviderContainer.dicCatheProvider.Add(authorizationProvider.Etp, (IAuthorizationProvider)obj); provider = (IAuthorizationProvider)obj; } } } return provider; }
/// <summary> /// 比较当前实例与指定的Etp实例是否等效,是则返回true,否则返回false。 /// 注:名称字符串相等即为等效,忽略大小写。 /// </summary> /// <param name="etpName">EtpName实例。</param> public bool Equals(EtpName etpName) { if (etpName.FullName.ToLower().Trim() == FullName.ToLower().Trim() && etpName.Name.ToLower().Trim() == Name.ToLower().Trim()) { return true; } return false; }
/// <summary> /// 根据名称查找Etp。 /// </summary> /// <param name="name">平台的名称。</param> public static Platform Find(EtpName name) { if (string.IsNullOrWhiteSpace(name.Name)) { throw new Exception("未指定平台名"); } Platform platform = new Platform(); //配置文件中查找平台属性填充(注意填充平台下包含的应用) OAuthLoginConfig oAuthLoginConfig; try { oAuthLoginConfig = (OAuthLoginConfig)ConfigurationManager.GetSection("OAuthLogin"); } catch { throw new Exception("配置错误"); } var etpSection = oAuthLoginConfig.Etps[name.Name.ToLower().Trim()]; if (etpSection == null) { throw new Exception("平台未配置"); } //填充平台数据 platform.Name = name; platform.ApiUrl = etpSection.ApiUrl; platform.AuthorizationUrl = etpSection.AuthorizationUrl; platform.TokenUrl = etpSection.TokenUrl; platform.Applications = new Application[etpSection.Apps.Count]; //授权提供程序 IAuthorizationProvider provider = AuthorizationProviderContainer.Current.GetProvider(name); for (int i = 0; i < etpSection.Apps.Count; i++) { //填充平台下应用数据 Application appliction = new Application(platform, provider); appliction.AppKey = etpSection.Apps[i].Appkey; appliction.RedirectUrl = etpSection.Apps[i].RedirectUrl; appliction.Secret = etpSection.Apps[i].Secret; platform.Applications[i] = appliction; } //返回平台 return platform; }
/// <summary> /// 获取适用于指定平台的EtpException构造器。 /// </summary> /// <param name="etpName"></param> public IEtpExceptionBuilder GetBuilder(EtpName etpName) { //有则从缓存中取 if (dicCatheBuilder.ContainsKey(etpName.Name.ToLower().Trim())) { return dicCatheBuilder[etpName.Name.ToLower().Trim()]; } else { //反射创建 OAuthLoginConfig authLoginConfig = (OAuthLoginConfig)ConfigurationManager.GetSection("OAuthLogin"); var builder = authLoginConfig.EtpExceptionBuilder[etpName.Name.ToLower().Trim()]; Assembly assembly = Assembly.Load(builder.Assembly); //反射创建构造器实例 object obj = assembly.CreateInstance(builder.Type); //添加缓存 etpExceptionBuilderContainer.dicCatheBuilder.Add(builder.Etp, (IEtpExceptionBuilder)obj); return (IEtpExceptionBuilder)obj; } }