mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-11-08 10:24:44 +08:00
统一注入代码
This commit is contained in:
@@ -1,73 +0,0 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.Mvc
|
||||
// Author : yubaolee
|
||||
// Created : 10-26-2015
|
||||
//
|
||||
// Last Modified By : yubaolee
|
||||
// Last Modified On : 10-26-2015
|
||||
// ***********************************************************************
|
||||
// <copyright file="AutofacExt.cs" company="www.cnblogs.com/yubaolee">
|
||||
// Copyright (c) www.cnblogs.com/yubaolee. All rights reserved.
|
||||
// </copyright>
|
||||
// <summary>IOC扩展</summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using System.Reflection;
|
||||
using System.Web.Http;
|
||||
using System.Web.Mvc;
|
||||
using Autofac;
|
||||
using Autofac.Integration.Mvc;
|
||||
using Autofac.Integration.WebApi;
|
||||
using OpenAuth.App;
|
||||
using OpenAuth.Repository;
|
||||
using OpenAuth.Repository.Interface;
|
||||
|
||||
namespace OpenAuth.WebApi
|
||||
{
|
||||
internal static class AutofacExt
|
||||
{
|
||||
public static void InitAutofac()
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
|
||||
//注册数据库基础操作和工作单元
|
||||
builder.RegisterGeneric(typeof(BaseRepository<>)).As(typeof(IRepository<>)).PropertiesAutowired();
|
||||
builder.RegisterType(typeof (UnitWork)).As(typeof (IUnitWork)).PropertiesAutowired();
|
||||
|
||||
//注册app层
|
||||
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof (AuthorizeApp))).PropertiesAutowired();
|
||||
|
||||
|
||||
// OPTIONAL: Register model binders that require DI.
|
||||
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
|
||||
builder.RegisterModelBinderProvider();
|
||||
|
||||
// OPTIONAL: Register web abstractions like HttpContextBase.
|
||||
builder.RegisterModule<AutofacWebTypesModule>();
|
||||
|
||||
// OPTIONAL: Enable property injection in view pages.
|
||||
builder.RegisterSource(new ViewRegistrationSource());
|
||||
|
||||
// OPTIONAL: Enable property injection into action filters.
|
||||
builder.RegisterFilterProvider();
|
||||
|
||||
//注册所有的ApiControllers
|
||||
builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
|
||||
|
||||
var container = builder.Build();
|
||||
HttpConfiguration config = GlobalConfiguration.Configuration;
|
||||
//注册api容器需要使用HttpConfiguration对象
|
||||
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
|
||||
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从容器中获取对象
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public static T GetFromFac<T>()
|
||||
{
|
||||
return (T)DependencyResolver.Current.GetService(typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Optimization;
|
||||
using System.Web.Routing;
|
||||
using OpenAuth.App;
|
||||
|
||||
namespace OpenAuth.WebApi
|
||||
{
|
||||
|
||||
@@ -178,7 +178,6 @@
|
||||
<Compile Include="Areas\SSO\Controllers\LoginController.cs" />
|
||||
<Compile Include="Areas\SSO\Controllers\CheckController.cs" />
|
||||
<Compile Include="Areas\SSO\SSOAreaRegistration.cs" />
|
||||
<Compile Include="AutofacExt.cs" />
|
||||
<Compile Include="Global.asax.cs">
|
||||
<DependentUpon>Global.asax</DependentUpon>
|
||||
</Compile>
|
||||
|
||||
Reference in New Issue
Block a user