public void WebRequest()
        {
            var container = new Container();

            container.Register(c => HttpContextLocal <IService> .Set(() => new ServiceImpl()));
            var id  = ((ServiceImpl)container.GetInstance <IService>()).Id;
            var id3 = ((ServiceImpl)container.GetInstance <IService>()).Id;

            Assert.Equal(id, id3);
            var t = new Thread(() => {
                try {
                    var id2 = ((ServiceImpl)container.GetInstance <IService>()).Id;
                    Assert.NotEqual(id, id2);
                } catch (Exception ex) {
                    Assert.True(false, ex.ToString());
                }
            });

            t.Start();
            t.Join();
        }
Example #2
0
        /// <summary>
        /// 构造函数
        /// </summary>
        static AppDbContextBuilder()
        {
            // 判断是否是 Web 环境
            IsWebEnvironment = HttpContextLocal.Current() != null;

            // 扫描程序集,获取数据库实体相关类型
            EntityCorrelationTypes = App.CanBeScanTypes.Where(t => (typeof(IPrivateEntity).IsAssignableFrom(t) || typeof(IPrivateModelBuilder).IsAssignableFrom(t)) &&
                                                              t.IsClass && !t.IsAbstract && !t.IsGenericType && !t.IsInterface && !t.IsDefined(typeof(NonAutomaticAttribute), true))
                                     .ToList();

            if (EntityCorrelationTypes.Count > 0)
            {
                DbContextLocatorCorrelationTypes = new ConcurrentDictionary <Type, DbContextCorrelationType>();

                // 获取模型构建器 Entity<T> 方法
                ModelBuildEntityMethod = typeof(ModelBuilder).GetMethods(BindingFlags.Public | BindingFlags.Instance).FirstOrDefault(u => u.Name == nameof(ModelBuilder.Entity) && u.GetParameters().Length == 0);
            }

            // 查找所有数据库函数,必须是公开静态方法,且所在父类也必须是公开静态方法
            DbFunctionMethods = App.CanBeScanTypes
                                .Where(t => t.IsAbstract && t.IsSealed && t.IsClass && !t.IsDefined(typeof(NonAutomaticAttribute), true))
                                .SelectMany(t => t.GetMethods(BindingFlags.Public | BindingFlags.Static).Where(m => !m.IsDefined(typeof(SkipScanAttribute), false) && m.IsDefined(typeof(QueryableFunctionAttribute), true))).ToList();
        }
Example #3
0
File: App.cs Project: xxzztt/Furion
 /// <summary>
 /// 获取请求生命周期的服务
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public static object GetRequiredService(Type type)
 {
     return(HttpContextLocal.Current()?.RequestServices?.GetRequiredService(type));
 }
Example #4
0
 /// <summary>
 /// 解析服务
 /// </summary>
 /// <typeparam name="TService"></typeparam>
 /// <returns></returns>
 public static TService GetRequiredService <TService>(this object obj)
     where TService : class
 {
     return(HttpContextLocal.Current()?.RequestServices?.GetRequiredService <TService>());
 }
Example #5
0
 /// <summary>
 /// 获取日志操作对象
 /// </summary>
 /// <param name="categoryName"></param>
 /// <returns></returns>
 private static ILogger GetLogger(string categoryName)
 {
     return(HttpContextLocal.Current()?.RequestServices?.GetService <ILoggerFactory>()?.CreateLogger(categoryName));
 }
Example #6
0
 /// <summary>
 /// 获取日志操作对象
 /// </summary>
 /// <typeparam name="TClass">类型</typeparam>
 /// <returns></returns>
 private static ILogger <TClass> GetLogger <TClass>()
 {
     return(HttpContextLocal.Current()?.RequestServices?.GetService <ILogger <TClass> >());
 }