Пример #1
0
        /// <summary>
        /// 获取用户身份
        /// </summary>
        /// <param name="companyName">公司名</param>
        /// <param name="userName">登录名</param>
        /// <param name="cultureName">区域性名称</param>
        /// <param name="cacheDiscardIntervalHours">缓存丢弃间隔(小时)</param>
        /// <returns>用户资料</returns>
        public static Identity Fetch(string companyName, string userName, string cultureName, int?cacheDiscardIntervalHours = null)
        {
            if (String.IsNullOrEmpty(companyName))
            {
                return(null);
            }
            if (String.IsNullOrEmpty(userName))
            {
                return(null);
            }

            string primaryKey = Standards.FormatCompoundKey(companyName, userName);

            cacheDiscardIntervalHours = cacheDiscardIntervalHours ?? CacheDiscardIntervalHours;
            if (cacheDiscardIntervalHours > 0 && _cache.TryGetValue(primaryKey, out CachedObject <Identity> cachedObject))
            {
                cachedObject.Value._cultureName = cultureName;
                return(cachedObject.Value);
            }

            Identity result = new Identity(companyName, userName, cultureName);

            _cache[primaryKey] = new CachedObject <Identity>(result, DateTime.Now.AddHours(cacheDiscardIntervalHours.Value));
            return(result);
        }
Пример #2
0
 /// <summary>
 /// 获取控制器的授权角色
 /// </summary>
 /// <param name="typeInfo">TypeInfo</param>
 /// <param name="methodInfo">MethodInfo</param>
 /// <returns>控制器的授权角色</returns>
 public static ControllerRole Fetch(TypeInfo typeInfo, MethodInfo methodInfo)
 {
     return(_cache.GetValue(Standards.FormatCompoundKey(typeInfo.FullName, methodInfo.Name, methodInfo.GetHashCode()), () =>
                            FetchRoot(Database.Default, p => p.ControllerName == typeInfo.FullName && p.ActionName == methodInfo.Name,
                                      () => new ControllerRole(Database.Default.DataSourceKey, Database.Default.Sequence.Value, typeInfo, methodInfo))
                            ));
 }
Пример #3
0
        /// <summary>
        /// 获取Orleans服务集群客户端
        /// </summary>
        /// <param name="clusterId">Orleans集群的唯一ID</param>
        /// <param name="serviceId">Orleans服务的唯一ID</param>
        /// <param name="connectionString">Orleans数据库连接串</param>
        /// <returns>Orleans服务集群客户端</returns>
        public static IClusterClient Fetch(string clusterId, string serviceId, string connectionString)
        {
            return(_cache.GetValue(Standards.FormatCompoundKey(clusterId, serviceId), () =>
            {
                IClusterClient value = new ClientBuilder()
                                       .Configure <SerializationProviderOptions>(options =>
                {
                    options.SerializationProviders.Add(typeof(BondSerializer));
                    options.FallbackSerializationProvider = typeof(BondSerializer);
                })
                                       .Configure <ConnectionOptions>(options => { options.ProtocolVersion = NetworkProtocolVersion.Version2; })
                                       .Configure <ClusterOptions>(options =>
                {
                    options.ClusterId = clusterId;
                    options.ServiceId = serviceId;
                })
                                       .UseAdoNetClustering(options =>
                {
                    options.ConnectionString = connectionString;
#if PgSQL
                    options.Invariant = "Npgsql";
#endif
#if MsSQL
                    options.Invariant = "System.Data.SqlClient";
#endif
#if MySQL
                    options.Invariant = "MySql.Data.MySqlClient";
#endif
#if ORA
                    options.Invariant = "Oracle.DataAccess.Client";
#endif
                })
                                       .ConfigureApplicationParts(parts =>
                {
                    /*
                     * 装配Actor协议
                     * 业务程序集都应该统一采用"*.Business.dll"作为文件名的后缀
                     * 契约程序集都应该统一采用"*.Contract.dll"作为文件名的后缀
                     * 以上程序集都应该被部署到主程序的执行目录下
                     */
                    foreach (string fileName in Directory.GetFiles(Phenix.Core.AppRun.BaseDirectory, "*.Business.dll"))
                    {
                        parts.AddApplicationPart(Assembly.LoadFrom(fileName)).WithReferences().WithCodeGeneration();
                    }
                    foreach (string fileName in Directory.GetFiles(Phenix.Core.AppRun.BaseDirectory, "*.Contract.dll"))
                    {
                        parts.AddApplicationPart(Assembly.LoadFrom(fileName)).WithReferences().WithCodeGeneration();
                    }
                })
                                       .AddSimpleMessageStreamProvider(ContextKeys.SimpleMessageStreamProviderName)
                                       .AddOutgoingGrainCallFilter(context =>
                {
                    if (context.Grain is ISecurityContext)
                    {
                        IIdentity currentIdentity = Principal.CurrentIdentity;
                        if (currentIdentity != null)
                        {
                            RequestContext.Set(ContextKeys.CurrentIdentityCompanyName, currentIdentity.CompanyName);
                            RequestContext.Set(ContextKeys.CurrentIdentityUserName, currentIdentity.UserName);
                            RequestContext.Set(ContextKeys.CurrentIdentityCultureName, currentIdentity.CultureName);
                        }
                    }

                    if (context.Grain is ITraceLogContext)
                    {
                        long traceKey;
                        if (RequestContext.Get(ContextKeys.TraceKey) == null)
                        {
                            traceKey = Database.Default.Sequence.Value;
                            RequestContext.Set(ContextKeys.TraceKey, traceKey);
                        }
                        else
                        {
                            traceKey = (long)RequestContext.Get(ContextKeys.TraceKey);
                        }

                        int traceOrder = RequestContext.Get(ContextKeys.TraceOrder) != null ? (int)RequestContext.Get(ContextKeys.TraceOrder) + 1 : 0;
                        RequestContext.Set(ContextKeys.TraceOrder, traceOrder);

                        Task.Run(() => EventLog.Save(context.InterfaceMethod, Phenix.Core.Reflection.Utilities.JsonSerialize(context.Arguments), traceKey, traceOrder));
                        try
                        {
                            return context.Invoke();
                        }
                        catch (Exception ex)
                        {
                            Task.Run(() => EventLog.Save(context.InterfaceMethod, Phenix.Core.Reflection.Utilities.JsonSerialize(context.Arguments), traceKey, traceOrder, ex));
                            throw;
                        }
                    }

                    return context.Invoke();
                })
                                       .Build();
                AsyncHelper.RunSync(() => value.Connect());
                return value;
            }));
        }
Пример #4
0
 string IIdentity.FormatPrimaryKey(string keyExtension)
 {
     return(Standards.FormatCompoundKey(CompanyName, keyExtension));
 }