protected EndpointServiceBase()
 {
     Mapper = MapperConfig.CreateMaps();
 }
        public TestBase()
        {
            var efServiceProvider = new ServiceCollection().AddEntityFrameworkInMemoryDatabase().BuildServiceProvider();

            var services = new ServiceCollection();

            var hostingEnvMock      = new Mock <IHostingEnvironment>(MockBehavior.Strict);
            var currentAssemblyName = this.GetType().GetTypeInfo().Assembly.GetName().Name;

            hostingEnvMock.Setup(he => he.ApplicationName).Returns(currentAssemblyName);

            services.AddOptions();
            services
            .AddDbContext <DeviserDbContext>(b => b.UseInMemoryDatabase("DeviserWI").UseInternalServiceProvider(efServiceProvider));

            services.AddIdentity <User, Role>()
            .AddEntityFrameworkStores <DeviserDbContext>();

            services.AddSingleton <DiagnosticSource>(new DiagnosticListener("Microsoft.AspNetCore"));
            services.AddSingleton <IHostingEnvironment>(hostingEnvMock.Object);
            services.Add(ServiceDescriptor.Transient <IConfigureOptions <MvcOptions>, SerializerSettingsSetup>());
            services.AddSingleton <ObjectPoolProvider, DefaultObjectPoolProvider>();
            services.TryAddSingleton <ObjectMethodExecutorCache>();
            services.TryAddSingleton <ITempDataDictionaryFactory, TempDataDictionaryFactory>();
            //services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance)

            services.AddLogging();
            services.AddOptions();



            services.AddMvc(option =>
            {
                //var jsonOutputFormatter = new JsonOutputFormatter();
                //jsonOutputFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                ////jsonOutputFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
                //jsonOutputFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
                //jsonOutputFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

                //var jsonOutputFormatterOld = option.OutputFormatters.FirstOrDefault(formatter => formatter is JsonOutputFormatter);
                //if (jsonOutputFormatterOld != null)
                //{
                //    option.OutputFormatters.Remove(jsonOutputFormatterOld);
                //}
                ////options.OutputFormatters.RemoveAll(formatter => formatter.Instance.GetType() == typeof(JsonOutputFormatter));
                //option.OutputFormatters.Insert(0, jsonOutputFormatter);
            })
            .AddRazorOptions(options =>
            {
                options.ViewLocationExpanders.Add(new ModuleLocationRemapper());
            });

            // IHttpContextAccessor is required for SignInManager, and UserManager
            //var context = new DefaultHttpContext();
            //context.Features.Set<IHttpAuthenticationFeature>(new HttpAuthenticationFeature() { Handler = new TestAuthHandler() });
            //services.AddSingleton<IHttpContextAccessor>(
            //    new HttpContextAccessor()
            //    {
            //        HttpContext = context,
            //    });


            //Mapper.Reset();
            MapperConfig.CreateMaps();

            // Add Autofac
            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterModule <DefaultModule>();
            containerBuilder.Populate(services);
            _container       = containerBuilder.Build();
            _serviceProvider = new AutofacServiceProvider(_container);
        }
Beispiel #3
0
 protected ApiControllerBase()
 {
     Mapper = MapperConfig.CreateMaps();
 }
        public static IServiceCollection AddDeviserPlatform(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton <IInstallationProvider, InstallationProvider>();
            services.AddSingleton <ISiteSettingRepository, SiteSettingRepository>();

            var sp = services.BuildServiceProvider();
            IInstallationProvider installationProvider = sp.GetRequiredService <IInstallationProvider>();

            SharedObjects.ServiceProvider = sp;


            //if (installationProvider.IsPlatformInstalled)
            //{
            services.AddDbContext <DeviserDbContext>(
                (internalServiceProvider, dbContextOptionBuilder) =>
            {
                //dbContextOptionBuilder.UseInternalServiceProvider(sp);
                installationProvider.GetDbContextOptionsBuilder(dbContextOptionBuilder);
            });
            //}

            services.AddIdentity <User, Role>()
            .AddEntityFrameworkStores <DeviserDbContext>()
            .AddDefaultTokenProviders();

            MapperConfig.CreateMaps();
            sp = services.BuildServiceProvider();

            //Add framework services.
            //services.AddDbContext<DeviserDbContext>(options =>
            //    options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"), b => b.MigrationsAssembly("Deviser.WI")));


            if (installationProvider.IsPlatformInstalled)
            {
                var siteSettingRepository = sp.GetService <ISiteSettingRepository>(); //sp.GetService<ISiteSettingRepository>();
                var siteSettings          = siteSettingRepository.GetSettings();

                var enableFacebookAuth    = siteSettings.FirstOrDefault(s => s.SettingName == "EnableFacebookAuth").SettingValue;
                var facebookAppId         = siteSettings.FirstOrDefault(s => s.SettingName == "FacebookAppId").SettingValue;
                var facebookAppAppSecret  = siteSettings.FirstOrDefault(s => s.SettingName == "FacebookAppSecret").SettingValue;
                var enableGoogleAuth      = siteSettings.FirstOrDefault(s => s.SettingName == "EnableGoogleAuth").SettingValue;
                var googleClientId        = siteSettings.FirstOrDefault(s => s.SettingName == "GoogleClientId").SettingValue;
                var googleClientSecret    = siteSettings.FirstOrDefault(s => s.SettingName == "GoogleClientSecret").SettingValue;
                var enableTwitterAuth     = siteSettings.FirstOrDefault(s => s.SettingName == "EnableTwitterAuth").SettingValue;
                var twitterConsumerKey    = siteSettings.FirstOrDefault(s => s.SettingName == "TwitterConsumerKey").SettingValue;
                var twitterConsumerSecret = siteSettings.FirstOrDefault(s => s.SettingName == "TwitterConsumerSecret").SettingValue;

                if (!string.IsNullOrEmpty(enableFacebookAuth) && bool.Parse(enableFacebookAuth.ToLower()))
                {
                    services.AddAuthentication().AddFacebook(facebookOptions =>
                    {
                        facebookOptions.AppId     = facebookAppId;
                        facebookOptions.AppSecret = facebookAppAppSecret;
                    });
                }

                if (!string.IsNullOrEmpty(enableTwitterAuth) && bool.Parse(enableTwitterAuth.ToLower()))
                {
                    services.AddAuthentication().AddTwitter(twitterOptions =>
                    {
                        twitterOptions.ConsumerKey    = twitterConsumerKey;
                        twitterOptions.ConsumerSecret = twitterConsumerSecret;
                    });
                }

                if (!string.IsNullOrEmpty(enableGoogleAuth) && bool.Parse(enableGoogleAuth.ToLower()))
                {
                    services.AddAuthentication().AddGoogle(googleOptions =>
                    {
                        googleOptions.ClientId     = googleClientId;
                        googleOptions.ClientSecret = googleClientSecret;
                    });
                }
            }

            services.Add(ServiceDescriptor.Transient <IConfigureOptions <MvcOptions>, SerializerSettingsSetup>());

            RegisterModuleDependencies(services);

            services
            .AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddRazorOptions(options =>
            {
                options.ViewLocationExpanders.Add(new ModuleLocationRemapper());
            })
            .AddControllersAsServices();

            services.AddSignalR();

            services.AddSession();

            // Add application services.
            services.AddTransient <IEmailSender, MessageSender>();
            services.AddTransient <ISmsSender, MessageSender>();
            services.TryAddSingleton <ObjectMethodExecutorCache>();
            services.TryAddSingleton <ITypeActivatorCache, TypeActivatorCache>();

            return(services);
        }