示例#1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen(s =>
            {
                s.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Sms Messages API", Version = "v1"
                });
                s.DescribeAllParametersInCamelCase();
            });

            Cqrs.RegisterDependencies(services);
            Database.RegisterDependencies(services);
            Services.RegisterDependencies(services);

            var connection = Configuration["DefaultDatabaseConnection"];

            services.AddDbContext <MessagesDbContext>(options => options.UseSqlServer(connection));

            services.Configure <RabbitMqConnectionData>(Configuration.GetSection("RabbitMq"));

            services.AddCors(options =>
            {
                options.AddPolicy("CorsApiPolicy",
                                  builder => builder.AllowAnyOrigin()
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .AllowCredentials());
            });

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }
        public AppModule(Cqrs _cqrs)
        {
            Post["/api/cqrsbus"] = _ =>
            {
                Console.WriteLine("*** POST REQUEST RECEIVED ***");

                try
                {
                    ICqrsInput cqrsInput = new CqrsInput()
                    {
                        Body = Context.Request.Body.AsString(),
                        Auth = Context.Request.Headers.Authorization
                    };

                    string jsonResult = _cqrs.Exe(cqrsInput);

                    return(new TextResponse(jsonResult)); // Respond with code 200
                }
                catch (Exception e)
                {
                    Console.WriteLine("EXCEPTION: " + e.Message);

                    if (e is IHttpRespondStatusCode)
                    {
                        return(new TextResponse((HttpStatusCode)((IHttpRespondStatusCode)e).StatusCode,
                                                contents: "EXCEPTION: " + e.GetType().ToString()));
                    }

                    return(new TextResponse(HttpStatusCode.InternalServerError, e.Message));
                }
            };


            Get["/test"] = _ =>
            {
                return(new TextResponse("test ok"));
            };
        }