示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // services.TryAddScoped<StatisticListener>();


            // AppEvents.ApplicationStarting += (sender, serviceProvider) =>
            // {
            //     var listener = serviceProvider.GetService<StatisticListener>();
            //     AppEvents.Services = serviceProvider;
            //     listener.StartListening();
            // };

            // AppEvents.ApplicationStopping += (sender, serviceProvider) =>
            // {
            //     var listener = serviceProvider.GetService<StatisticListener>();
            //     listener.StopListening();
            // };

            services.TryAddSingleton <IAppSettings>(AppSettings);
            services.TryAddSingleton <AppEvents>(AppEvents);

            services.TryAddSingleton <Configuration>((s) =>
            {
                var configPath = HostingEnvironment.ContentRootFileProvider.GetFileInfo("our.orders.config");

                return(configPath.Exists ?
                       JsonConvert.DeserializeObject <Configuration>(File.ReadAllText(configPath.PhysicalPath)) :
                       new Configuration());
            });

            services.TryAddSingleton <AutoMapperProvider>();

            services.TryAddSingleton <IEmailSender, LoggerSender>();

            var autoMapperProvider = new AutoMapperProvider(services);

            services.AddSingleton <Profile, AutoMapperProfile>();
            services.AddSingleton <IMapper>((s) => autoMapperProvider.GetMapper(s));

            services.AddMemoryCache();

            services.AddAntiforgery(options =>
            {
                options.Cookie.Name   = "XSRF-TOKEN";
                options.HeaderName    = "X-XSRF-TOKEN";
                options.FormFieldName = "requestVerificationToken";
            });


            // Appsettings services custom configuration
            AppEvents.OnConfigure(services);

            // ******  Services depending on AppSettings ******

            // AppSettings validation
            var appSettingsValidator = new AppSettingsValidator();

            appSettingsValidator.EnsureValid(AppSettings);


            var roleStore = new RoleStore();

            services.AddSingleton <RoleStore>(roleStore);
            services.AddSingleton <IRoleStore <Role> >(roleStore);


            services.AddTransient <IOrder, Order>();
            services.AddTransient <IProduct, Product>();
            services.AddTransient <IClient, Client>();
            services.AddTransient <ShippingTemplate>();
            services.AddTransient <DocumentTemplate>();
            services.AddTransient <Shop>();
            services.AddTransient <Movement>();
            services.AddTransient <Voucher>();
            services.AddTransient <StockUnit>();
            services.AddTransient <Warehouse>();
            services.AddTransient <Category>();
            services.AddTransient <User>();



            services.TryAddTransient <OrderService, OrderService>();
            services.TryAddTransient <IService <IOrder>, OrderService>();

            services.TryAddTransient <ProductService, ProductService>();
            services.TryAddTransient <IService <IProduct>, ProductService>();

            services.TryAddTransient <IService <IShippingTemplate>, Service <IShippingTemplate> >();
            services.TryAddTransient <IService <DocumentTemplate>, Service <DocumentTemplate> >();

            services.TryAddTransient <IService <IClient>, Service <IClient> >();

            services.TryAddTransient <IService <Shop>, Service <Shop> >();
            services.TryAddTransient <IService <Movement>, Service <Movement> >();
            services.TryAddTransient <IService <Voucher>, Service <Voucher> >();
            services.TryAddTransient <IService <StockUnit>, Service <StockUnit> >();
            services.TryAddTransient <IService <Warehouse>, Service <Warehouse> >();
            services.TryAddTransient <IService <Category>, Service <Category> >();


            var key = Encoding.ASCII.GetBytes(AppSettings.JwtSecret);

            // ******  End Services depending on AppSettings ******


            // configure jwt authentication

            services
            .AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            // .AddCookie(cfg => cfg.SlidingExpiration = true)
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = false;
                //x.SaveToken = true;

                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };

                x.Events = new JwtBearerEvents
                {
                    // For debugging...
                    OnAuthenticationFailed = async(context) =>
                    {
                        await Task.FromResult(0);
                    },
                    OnChallenge = async(context) =>
                    {
                        await Task.FromResult(0);
                    },
                    OnMessageReceived = async(context) =>
                    {
                        await Task.FromResult(0);
                    },
                    OnTokenValidated = async(context) =>
                    {
                        await Task.FromResult(0);
                    }
                };
            });

            // Add framework services.
            services
            .AddMvc(options =>
            {
                // options.Conventions.Insert(0, new RoutePrefixConvention(new RouteAttribute("our-orders")));
                options.Filters.Add <OperationCancelledExceptionFilter>();
            })

            .AddJsonOptions(options =>
            {
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
                options.SerializerSettings.ContractResolver  = new DefaultContractResolver();
                options.SerializerSettings.Formatting        = Formatting.Indented;
                options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());

                JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
                {
                    ContractResolver  = new DefaultContractResolver(),
                    Formatting        = Newtonsoft.Json.Formatting.Indented,
                    NullValueHandling = NullValueHandling.Ignore,
                    Converters        = new List <JsonConverter> {
                        new StringEnumConverter()
                    }
                };
            })
            .ConfigureApplicationPartManager(manager =>
            {
                manager.FeatureProviders.Add(new InternalControllerFeature());
                manager.FeatureProviders.Add(new ExternalControllerFeatureProvider(AppSettings));
            });
#if NETCORE2_2
            var embeddedProvider = new ManifestEmbeddedFileProvider(Assembly.GetAssembly(this.GetType()));
#else
            var embeddedProvider = new EmbeddedFileProvider(Assembly.GetAssembly(this.GetType()), "our.orders");
#endif
            services.AddSingleton <IFileProvider>(embeddedProvider);

            // configure DI for application services
            // services.AddScoped<IUserService, UserService>();
        }
示例#2
0
 public VolunteerSelfIdentificationController()
 {
     _ctx    = HttpContext.Current.GetOwinContext().Get <CrmServiceContext>();
     _mapper = AutoMapperProvider.GetMapper();
 }
示例#3
0
 public RegionController()
 {
     _ctx    = HttpContext.Current.GetOwinContext().Get <CrmServiceContext>();
     _mapper = AutoMapperProvider.GetMapper();
 }
示例#4
0
 public PortalUserStore(CrmServiceContext crmServiceContext)
 {
     _ctx    = crmServiceContext;
     _mapper = AutoMapperProvider.GetMapper();
 }
示例#5
0
 public VolunteerEmergencyContactController()
 {
     _ctx    = HttpContext.Current.GetOwinContext().Get <CrmServiceContext>();
     _mapper = AutoMapperProvider.GetMapper();
 }