using Autofac;
using Autofac.Integration.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
[assembly: PreApplicationStartMethod(typeof(XXXNamespace.IocConfig), "RegisterAndSetResolver")]
namespace XXXNamespace
{
public sealed class IocConfig
{
public static void RegisterAndSetResolver()
{
var configuration = GlobalConfiguration.Configuration;
var builder = new ContainerBuilder();
//builder.RegisterType<MyDbContext>().InstancePerRequest();
builder.RegisterAssemblyTypes(typeof(BaseService).Assembly)
.Where(t => t.Name.EndsWith("Service"))
.AsImplementedInterfaces()
.InstancePerRequest();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(configuration);
var container = builder.Build();
// Set the WebApi dependency resolver.
configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
}
}