示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(o => o.AddPolicy("CorsPolicy", builder => {
                builder
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials()
                .WithOrigins("http://localhost:4200");
            }));

            services.Configure <ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
            services.AddSingleton <ISubscriber <string>, TopicSubscriber>((serviceProvider) =>
            {
                var logger            = serviceProvider.GetRequiredService <ILogger <TopicSubscriber> >();
                var connectionStrings = serviceProvider.GetRequiredService <IOptions <ConnectionStrings> >();
                return(new TopicSubscriber(logger, connectionStrings.Value.TopicSubscriberConnectionSettings));
            });

            services.AddSingleton((serviceProvider) =>
            {
                var hubContext   = serviceProvider.GetRequiredService <IHubContext <BroadcastHub, IHubClient> >();
                var subscriber   = serviceProvider.GetRequiredService <ISubscriber <string> >();
                var repository   = serviceProvider.GetRequiredService <IRecomendationsRepository>();
                var logger       = serviceProvider.GetRequiredService <ILogger <RecomendationsTopicAdapter> >();
                var topicAdapter = new RecomendationsTopicAdapter(subscriber, hubContext, repository, logger);
                topicAdapter.SetupAsync().GetAwaiter().GetResult();
                return(topicAdapter);
            });

            services.AddSingleton <IPublisher <string>, QueuePublisher>((serviceProvider) =>
            {
                var connectionStrings = serviceProvider.GetRequiredService <IOptions <ConnectionStrings> >();
                return(new QueuePublisher(new MessageOptions("WeatherForecastApi", 1, TimeSpan.FromMinutes(1)), connectionStrings.Value.QueuePublisherConnectionSettings, Microsoft.WindowsAzure.Storage.LogLevel.Informational));
            });

            services.AddSingleton <IRecomendationsRepository, RecomendationsRepository>();
            services.AddSingleton <IForecastsRepository, ForecastsRepository>();
            services.AddSingleton <ILocationsRepository, LocationsRepository>();
            services.AddScoped <IForecastWriteService, ForecastService>((serviceProvider) =>
            {
                serviceProvider.GetRequiredService <ILocationsRepository>();
                var serv = new ForecastService(serviceProvider.GetRequiredService <ILocationsRepository>(), serviceProvider.GetRequiredService <IForecastsRepository>(), serviceProvider.GetRequiredService <IPublisher <string> >());
                serv.Startup().GetAwaiter().GetResult();
                return(serv);
            });
            services.AddScoped <IForecastReadService, ForecastService>();
            services.AddSignalR();

            services.AddControllersWithViews();
            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo {
                    Title = "Certification API", Version = "v1"
                });
                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                if (File.Exists(xmlPath))
                {
                    c.IncludeXmlComments(xmlPath);
                }
            });
        }