Exemple #1
0
 public static IMvcBuilder AddDatabaseJsonOptions(this IMvcBuilder builder, Action <JsonSerializationContractResolver> setupAction = null)
 {
     return(builder.AddNewtonsoftJson(opt =>
     {
         var resolver = new JsonSerializationContractResolver();
         opt.SerializerSettings.ContractResolver = resolver;
         opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
         setupAction?.Invoke(resolver);
     }));
 }
Exemple #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
#if !NETCOREAPP
#endif

            //#region xaf authentication

            JsonSerializationContractResolver resolver    = new JsonSerializationContractResolver();
            Action <MvcNewtonsoftJsonOptions> JsonOptions =
                options =>
            {
                options.SerializerSettings.ContractResolver      = resolver;
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            };
            //services.AddMvc(options =>
            //{
            //    options.EnableEndpointRouting = false;
            //})
            //    .SetCompatibilityVersion(CompatibilityVersion.Latest);

            services.AddControllers()
            .AddNewtonsoftJson(JsonOptions);
            //services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            //     .AddCookie(options =>
            //     {
            //         options.LoginPath = loginPath;
            //     });
            //services.AddSingleton<XpoDataStoreProviderService>();
            //services.AddSingleton(Configuration);
            //services.AddHttpContextAccessor();
            //services.AddScoped<SecurityProvider>();
            //#endregion

            //services.AddControllers();


            //services.AddControllers()
            //    .AddNewtonsoftJson(options =>
            //    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
            //);
            //services.AddControllers();
            //services.AddMvcCore();
            //services.AddApiExplorer();

            services.AddMvc(options =>
            {
                options.EnableEndpointRouting = false;
            })
            .SetCompatibilityVersion(CompatibilityVersion.Latest);

            #region xaf uow
            services.AddControllers()
            .AddNewtonsoftJson(JsonOptions)
            .AddDatabaseJsonOptions();
            services.AddXpoDefaultUnitOfWork(true, (DataLayerOptionsBuilder options) =>
                                             options.UseConnectionString(Configuration.GetConnectionString("ConnectionString"))
                                             // .UseAutoCreationOption(AutoCreateOption.DatabaseAndSchema) // debug only
                                             .UseEntityTypes(PRWebApi.Helpers.ConnectionHelper.GetPersistentTypes())
                                             );

            #endregion
            services.AddSingleton <XpoDataStoreProviderService>();
            services.AddSingleton(Configuration);
            services.AddHttpContextAccessor();
            services.AddScoped <SecurityProvider>();

            #region cors
            services.AddCors();
            //services.AddCors(options =>
            //{
            //    options.AddPolicy("CorsPolicy", builder =>
            //     builder.AllowAnyOrigin()
            //     .AllowAnyMethod()
            //     .AllowAnyHeader()
            //     .AllowCredentials()
            //     .Build());

            //});
            #endregion
            #region jwt bearer
            //services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            //    .AddJwtBearer(options =>
            //    {
            //        options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
            //        {
            //            ValidateIssuer = true,
            //            ValidateAudience = true,
            //            ValidateLifetime = true,
            //            ValidateIssuerSigningKey = true,
            //            ValidIssuer = Configuration["Jwt:Issuer"],
            //            ValidAudience = Configuration["Jwt:Issuer"],
            //            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
            //        };
            //    });
            #endregion

            #region swagger
            // Inject an implementation of ISwaggerProvider with defaulted settings applied.
            services.AddSwaggerGen();
            // Add the detail information for the API.
            services.ConfigureSwaggerGen((options) =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo {
                    Title   = "Swagger API",
                    Version = "v1"
                });

                //Determine base path for the application.
                var basePath = PlatformServices.Default.Application.ApplicationBasePath;
                //Set the comments path for the swagger json and ui.
                options.IncludeXmlComments(basePath + "\\PRWebApi.xml");
            });
            #endregion
        }