博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Autoafc 手动获取接口实例
阅读量:6533 次
发布时间:2019-06-24

本文共 5024 字,大约阅读时间需要 16 分钟。

demo:

using Autofac;using Autofac.Integration.Mvc;using Rongzi.RZR.Huoke.Repository;using Rongzi.RZR.Huoke.Service;using Rongzi.RZR.Huoke.Service.MQ;using System;using System.Collections.Generic;using System.Linq;using System.Reflection;using System.Web;using Rongzi.RZR.Huoke.Repository.Account;using System.IO;using Rongzi.RZR.Huoke.Infrastructure.Dependency;using Rongzi.RZR.Huoke.Service.Services;using Autofac.Core.Lifetime;namespace Rongzi.RZR.Huoke{    public class ContainerConfig    {        public static IContainer BuildUnityContainer()        {            var builder = new ContainerBuilder();            RegisterTypes(builder);            return builder.Build();        }        private static void RegisterTypes(ContainerBuilder builder)        {            builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();            #region register service            builder.RegisterType
().PropertiesAutowired(); builder.RegisterType
().PropertiesAutowired(); builder.RegisterType
().PropertiesAutowired(); builder.RegisterType
().PropertiesAutowired(); builder.RegisterType
().PropertiesAutowired(); builder.RegisterType
().PropertiesAutowired(); builder.RegisterType
().PropertiesAutowired(); builder.RegisterType
().PropertiesAutowired(); #endregion #region register respository builder.RegisterType
(); builder.RegisterType
(); builder.RegisterType
(); builder.RegisterType
(); builder.RegisterType
(); builder.RegisterType
(); builder.RegisterType
(); #endregion } public static T Resolve
(string key = "", ILifetimeScope scope = null) where T : class { if (scope == null) { //no scope specified scope = Scope(); } if (string.IsNullOrEmpty(key)) { return scope.Resolve
(); } return scope.ResolveKeyed
(key); } public static ILifetimeScope Scope() { try { if (HttpContext.Current != null) return AutofacDependencyResolver.Current.RequestLifetimeScope; //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks) return BuildUnityContainer().BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag); } catch (Exception) { //we can get an exception here if RequestLifetimeScope is already disposed //for example, requested in or after "Application_EndRequest" handler //but note that usually it should never happen //when such lifetime scope is returned, you should be sure that it'll be disposed once used (e.g. in schedule tasks) return BuildUnityContainer().BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag); } } }}

调用:

namespace Rongzi.RZR.Huoke.Filters{    public class ApiFormAuthFilterAttribute : System.Web.Mvc.ActionFilterAttribute    {        public AccountService AccountService { get; set; }        public ApiFormAuthFilterAttribute()        {            this.AccountService = ContainerConfig.Resolve
(); } public override void OnActionExecuting(ActionExecutingContext actionContext) { if (actionContext == null || actionContext.HttpContext.Request == null || actionContext.HttpContext.Request.RawUrl == null) { return; } string OrgUserAccountInfo = actionContext.HttpContext.Request.QueryString["OrgUserAccountInfo"]; if (!String.IsNullOrEmpty(OrgUserAccountInfo)) { OrgUserAccountInfo info = new RSAEncryptHelper().DecryptString
(OrgUserAccountInfo); OrganizationUserModel oUser = AccountService.GetOrganizationUserByPhone(info.CellPhone); if (oUser == null || oUser.OrgId!=info.OrgId) { actionContext.Result = GetAuthJsonResult("手机号或机构id错误"); return; } if (DateTime.Now.AddMinutes(-10) > info.timespan) { actionContext.Result = GetAuthJsonResult("该链接已超时"); return; } FormsAuth.SignIn(oUser); base.OnActionExecuting(actionContext); return; } base.OnActionExecuting(actionContext); } public static JsonResult GetAuthJsonResult(string msg = "用户还未登录") { var errResponse = new ResponseContext
(); errResponse.Head = new ResponseHead(-2, ErrCode.AuthError, msg); return new JsonResult { Data = errResponse, ContentEncoding = System.Text.Encoding.UTF8, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } public override void OnActionExecuted(ActionExecutedContext actionExecutedContext) { base.OnActionExecuted(actionExecutedContext); } }}

var obj = container.Resolve<Interface>(); //只有有特殊需求的时候可以通过这样的形式来拿。一般情况下没有必要这样来拿,因为AutoFac会自动工作

(即:会自动去类的带参数的构造函数中找与容器中key一致的参数类型,并将对象注入到类中,其实就是将对象赋值给构造函数的参数)  

 

转载地址:http://aiqbo.baihongyu.com/

你可能感兴趣的文章
MySQL基础
查看>>
学习vue 20天,我写了点东西
查看>>
定制sqlmap tamper脚本
查看>>
django框架的基础知识点《肆》
查看>>
Python - pytesseract 机器视觉
查看>>
Mac从零配置Vim
查看>>
杭电 1159 Common Subsequence
查看>>
夏日里的清新——南锣鼓巷的北京女孩儿们[原创街拍]
查看>>
【转载】showModalDialog returnValue is undefined in Google Chrome
查看>>
详解CSS盒模型(转)
查看>>
用django实现邮件发送
查看>>
mysql 连接 选库 查询
查看>>
Intellij IDEA 构建Spring Web项目 — 用户登录功能
查看>>
大规模均衡分割与层次聚类
查看>>
[AHOI2013]作业
查看>>
[bzoj 4241]历史研究
查看>>
小A的旅行(绿豆蛙的归宿)【期望DP】
查看>>
git push被忽略的文件 处理
查看>>
C#中用ILMerge将所有引用的DLL打成一个DLL文件
查看>>
PHP生成HTML静态页面
查看>>