예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <SurveyContext>(opt =>
                                                  opt.UseSqlServer(Configuration.GetConnectionString("SurveyContext"), ssopt => ssopt.CommandTimeout(300))
                                                  //opt.UseInMemoryDatabase("StudentSurveySystemDb")
                                                  );
            services.AddControllers().AddControllersAsServices().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.Converters.Add(new StringEnumConverter());
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.DateTimeZoneHandling  = DateTimeZoneHandling.Utc;
            });
            //Cross origin...
            services.AddCors(options =>
            {
                //may need to switch on prod to defined cors
                options.AddDefaultPolicy(builder =>
                                         builder.AllowAnyOrigin()
                                         .AllowAnyHeader()
                                         .AllowAnyMethod());
            });

            var key = Encoding.ASCII.GetBytes(Configuration["Secret"]);

            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata      = false;
                x.SaveToken                 = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false
                };
            });


            //swagger config
            SwaggerConfig.Setup(services);

            // configure DI for application services
            ServicesConfig.Setup(services);

            services.AddSwaggerGenNewtonsoftSupport();
        }
예제 #2
0
        public override void OnLoad(ContainerBuilder builder)
        {
            HttpConfiguration config = GlobalConfiguration.Configuration;

            GlobalConfiguration.Configure(WebApiConfig.Register);

            SwaggerConfig.Register();

            builder.RegisterApiControllers(GetWebEntryAssembly()).InstancePerDependency();
            builder.RegisterWebApiFilterProvider(config);

            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
예제 #3
0
        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();

            // Web API configuration and services
            config.Services.Replace(typeof(IExceptionHandler), new MyExceptionHandler());

            SwaggerConfig.Register(config);

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                "DefaultApi",
                "api/{controller}/{id}",
                new { id = RouteParameter.Optional }
                );

            app.UseErrorPage()
            .UseWelcomePage("/")
            .Use((ctx, next) =>
            {
                var msg = "故意引發例外";
                Console.WriteLine(msg);
                throw new Exception(msg);
            })
            .UseWebApi(config)
            ;

            //app.Use(async (ctx, next) =>
            //        {
            //            try
            //            {
            //                await next();
            //            }
            //            catch (Exception ex)
            //            {
            //                try
            //                {
            //                    this.ErrorHandle(ex, ctx);
            //                }
            //                catch (Exception exception)
            //                {
            //                    Console.WriteLine(exception.Message);
            //                    throw;
            //                }
            //            }
            //        })
            //   .Use((ctx, next) =>
            //        {
            //            var msg = "故意引發例外";
            //            Console.WriteLine(msg);
            //            throw new Exception(msg);
            //        })
            //   .UseWebApi(config);

            //app.Use<ErrorHandler>()
            //   .Use((ctx, next) =>
            //        {
            //            var msg = "故意引發例外";
            //            Console.WriteLine(msg);
            //            throw new Exception(msg);
            //        })
            //   .UseWebApi(config);

            //app.Use<ErrorHandlerOwinMiddleware>();
            //app.Use((ctx, next) =>
            //        {
            //            var msg = "故意引發例外";
            //            Console.WriteLine(msg);
            //            throw new Exception(msg);
            //        });
            //app.UseWebApi(config);
        }