public MethodInvocationContext(MethodInfo method, ServiceHandlerConfiguration serviceConfiguration, ServiceHandlerConfigurationInstance instanceConfiguration) { InstanceConfiguration = instanceConfiguration; ServiceConfiguration = serviceConfiguration; MethodInfo = method; var attrib = (AsyncStateMachineAttribute)method.GetCustomAttribute(typeof(AsyncStateMachineAttribute)); if (attrib != null) { IsAsync = true; } RestAttribute = method.GetCustomAttribute(typeof(RestAttribute), true) as RestAttribute; if (RestAttribute == null) { return; } // set allowable authorization roles if (RestAttribute.AuthorizationRoles != null) { AuthorizationRoles = RestAttribute.AuthorizationRoles .Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries) .ToList(); if (AuthorizationRoles.Count == 0) { AuthorizationRoles.Add(string.Empty); // Any authorized user } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ServiceHandlerConfiguration config) { //if (env.IsDevelopment()) //{ // app.UseDeveloperExceptionPage(); //} //if (config.Cors.UseCorsPolicy) //app.UseCors(config.Cors.CorsPolicyName); app.UseServiceHandler(); //app.UseMvc(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ServiceHandlerConfiguration config) { app.UseServiceHandler(); app.UseOpenApiHandler(info: new OpenApiInfo { Title = "CODE Framework Service/API Example", Description = "This service/api example is used to test, demonstrate, and document some of the CODE Framework service/api features.", TermsOfService = "http://codeframework.io", License = "MIT", Contact = "*****@*****.**" }); // TODO: For now, we are using Swashbuckle, but it would be nice to just have this without an outside dependency app.UseSwaggerUI(options => { options.SwaggerEndpoint("/openapi.json", "Service Description"); }); }
/// <summary> /// Configure the service and make it so you can inject /// IOptions<ServiceHandlerConfiguration> /// You can also /// </summary> /// <param name="services"></param> /// <param name="optionsAction"></param> /// <returns></returns> public static IServiceCollection AddServiceHandler(this IServiceCollection services, Action <ServiceHandlerConfiguration> optionsAction) { // add strongly typed configuration services.AddOptions(); services.AddRouting(); var provider = services.BuildServiceProvider(); var serviceConfiguration = provider.GetService <IConfiguration>(); var config = new ServiceHandlerConfiguration(); serviceConfiguration.Bind("ServiceHandler", config); ServiceHandlerConfiguration.Current = config; optionsAction?.Invoke(config); foreach (var svc in config.Services) { if (svc.ServiceType == null) { Type type = ReflectionUtils.GetTypeFromName(svc.ServiceTypeName); if (type == null) { if (ReflectionUtils.LoadAssembly(svc.AssemblyName) == null) { throw new ArgumentException( string.Format(Resources.InvalidServiceType, svc.ServiceTypeName)); } type = ReflectionUtils.GetTypeFromName(svc.ServiceTypeName); if (type == null) { throw new ArgumentException( string.Format(Resources.InvalidServiceType, svc.ServiceTypeName)); } } svc.ServiceType = type; } // Add to DI so we can compose the constructor services.AddTransient(svc.ServiceType); } // Add configured instance to DI services.AddSingleton(config); // Add service and create Policy with options services.AddCors(options => { options.AddPolicy(config.Cors.CorsPolicyName, builder => { if (config.Cors.AllowedOrigins == "*") { builder = builder.AllowAnyOrigin(); } else if (!string.IsNullOrEmpty(config.Cors.AllowedOrigins)) { var origins = config.Cors.AllowedOrigins.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); builder.WithOrigins(origins); } if (!string.IsNullOrEmpty(config.Cors.AllowedMethods)) { var methods = config.Cors.AllowedMethods.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); builder.WithMethods(methods); } if (!string.IsNullOrEmpty(config.Cors.AllowedHeaders)) { var headers = config.Cors.AllowedHeaders.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); builder.WithHeaders(headers); } if (config.Cors.AllowCredentials) { builder.AllowCredentials(); } }); }); // Allow injection of the IPrincipal services.AddScoped <IPrincipal>(serviceProvider => { try { // get User from Http Context var context = serviceProvider.GetRequiredService(typeof(IHttpContextAccessor)) as IHttpContextAccessor; return(context.HttpContext.User); } catch { // return an empty identity return(new ClaimsPrincipal(new ClaimsIdentity())); } }); return(services); }
/// <summary> /// Configure the service and make it so you can inject IOptions /// </summary> /// <param name="services"></param> /// <param name="optionsAction"></param> /// <returns></returns> public static IServiceCollection AddServiceHandler(this IServiceCollection services, Action <ServiceHandlerConfiguration> optionsAction) { // add strongly typed configuration services.AddOptions(); services.AddRouting(); var provider = services.BuildServiceProvider(); var serviceConfiguration = provider.GetService <IConfiguration>(); var config = new ServiceHandlerConfiguration(); serviceConfiguration.Bind("ServiceHandler", config); ServiceHandlerConfiguration.Current = config; optionsAction?.Invoke(config); foreach (var svc in config.Services) { if (svc.ServiceType == null) { var type = ObjectHelper.GetTypeFromName(svc.ServiceTypeName); if (type == null) { var assemblyNameWithPath = svc.AssemblyName; if (assemblyNameWithPath.IndexOf("\\", StringComparison.Ordinal) < 0 && assemblyNameWithPath.IndexOf("/", StringComparison.Ordinal) < 0) { var entryAssembly = Assembly.GetEntryAssembly(); if (entryAssembly != null) { var directoryName = Path.GetDirectoryName(entryAssembly.Location); if (directoryName != null) { assemblyNameWithPath = Path.Combine(directoryName, assemblyNameWithPath); } } } var assemblyNameWithFullPath = Path.GetFullPath(assemblyNameWithPath); if (ObjectHelper.LoadAssembly(assemblyNameWithFullPath) == null) { throw new ArgumentException(string.Format(Resources.InvalidServiceType, svc.ServiceTypeName)); } type = ObjectHelper.GetTypeFromName(svc.ServiceTypeName); if (type == null) { throw new ArgumentException(string.Format(Resources.InvalidServiceType, svc.ServiceTypeName)); } } svc.ServiceType = type; } // Add to DI so we can compose the constructor services.AddTransient(svc.ServiceType); } // Add configured instance to DI services.AddSingleton(config); // Add service and create Policy with options services.AddCors(options => { options.AddPolicy(config.Cors.CorsPolicyName, builder => { if (config.Cors.AllowedOrigins == "*") { builder = builder.SetIsOriginAllowed(s => true); } else if (!string.IsNullOrEmpty(config.Cors.AllowedOrigins)) { builder.WithOrigins(config.Cors.AllowedOrigins.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)); } if (!string.IsNullOrEmpty(config.Cors.AllowedMethods)) { builder.WithMethods(config.Cors.AllowedMethods.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)); } if (!string.IsNullOrEmpty(config.Cors.AllowedHeaders)) { builder.WithHeaders(config.Cors.AllowedHeaders.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)); } if (config.Cors.AllowCredentials) { builder.AllowCredentials(); } }); }); // Allow injection of the IPrincipal services.AddScoped <IPrincipal>(serviceProvider => { try { // get User from Http Context return(serviceProvider.GetRequiredService(typeof(IHttpContextAccessor)) is IHttpContextAccessor context ? context.HttpContext.User : null); } catch { // return an empty identity return(new ClaimsPrincipal(new ClaimsIdentity())); } }); return(services); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ServiceHandlerConfiguration config) { app.UseServiceHandler(); }