106 lines
3.5 KiB
C#
106 lines
3.5 KiB
C#
using Microsoft.Owin;
|
|
using Owin;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Web.Mvc;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using AutoMapper;
|
|
using System.Reflection;
|
|
using System.Linq;
|
|
using System.Configuration;
|
|
|
|
[assembly: OwinStartupAttribute(typeof(WebApplication5.Startup))]
|
|
namespace WebApplication5
|
|
{
|
|
public partial class Startup
|
|
{
|
|
public void Configuration(IAppBuilder app)
|
|
{
|
|
|
|
IocConfiguration.Configure();
|
|
ConfigureAuth(app);
|
|
|
|
//configure auto mapper
|
|
}
|
|
public static class IocConfiguration
|
|
{
|
|
public static void Configure()
|
|
{
|
|
var services = new ServiceCollection();
|
|
|
|
#region AutoMapper
|
|
services.AddAutoMapper(new Assembly[] { typeof(AutoMapperProfile).GetTypeInfo().Assembly });
|
|
#endregion
|
|
services.AddScoped(_ =>
|
|
new TestsDbContext(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)
|
|
);
|
|
|
|
services.AddSingleton(_ => new Writer.WriterPDF());
|
|
|
|
services.AddSingleton(typeof(AutoMapperProfile));
|
|
|
|
services.AddControllersAsServices(typeof(Startup).Assembly.GetExportedTypes()
|
|
.Where(t => !t.IsAbstract && !t.IsGenericTypeDefinition)
|
|
.Where(t => typeof(IController).IsAssignableFrom(t)
|
|
|| t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)));
|
|
|
|
var resolver = new DefaultDependencyResolver(services.BuildServiceProvider());
|
|
|
|
// Set the application resolver to our default resolver. This comes from "System.Web.Mvc"
|
|
//Other services may be added elsewhere through time
|
|
DependencyResolver.SetResolver(resolver);
|
|
}
|
|
}
|
|
public class DefaultDependencyResolver : IDependencyResolver
|
|
{
|
|
/// <summary>
|
|
/// Provides the service that holds the services
|
|
/// </summary>
|
|
protected IServiceProvider serviceProvider;
|
|
|
|
/// <summary>
|
|
/// Create the service resolver using the service provided (Direct Injection pattern)
|
|
/// </summary>
|
|
/// <param name="serviceProvider"></param>
|
|
public DefaultDependencyResolver(IServiceProvider serviceProvider)
|
|
{
|
|
this.serviceProvider = serviceProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a service by type - assume you get the first one encountered
|
|
/// </summary>
|
|
/// <param name="serviceType"></param>
|
|
/// <returns></returns>
|
|
public object GetService(Type serviceType)
|
|
{
|
|
return this.serviceProvider.GetService(serviceType);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all services of a type
|
|
/// </summary>
|
|
/// <param name="serviceType"></param>
|
|
/// <returns></returns>
|
|
public IEnumerable<object> GetServices(Type serviceType)
|
|
{
|
|
return this.serviceProvider.GetServices(serviceType);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
public static class ServiceProviderExtensions
|
|
{
|
|
public static IServiceCollection AddControllersAsServices(this IServiceCollection services, IEnumerable<Type> serviceTypes)
|
|
{
|
|
foreach (var type in serviceTypes)
|
|
{
|
|
services.AddTransient(type);
|
|
}
|
|
|
|
return services;
|
|
}
|
|
}
|
|
}
|