- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有 WCF 背景,我成功地将 IoC 与方面/拦截器结合使用来抽象功能,例如身份验证和日志记录。我会简单地将所需的接口(interface)添加到方面构造函数,就像您使用任何典型的 IoC 设置一样。
我现在正尝试将相同类型的过程应用于 webapi,但由于 Controller 继承自 ApiController 并且不实现接口(interface)。我假设可能有不同的应用方面的方式?
public class MyController: ApiController
{
private readonly IUnitOfWork _unitOfWork;
private readonly ILoginService _loginService;
private readonly ILog _log;
public LoginController(ILoginService loginService, IUnitOfWork unitOfWork, ILog log)
{
this._loginService = loginService;
this._unitOfWork = unitOfWork;
this._log = log;
}
// I WANT TO INTERCEPT THIS METHOD USING UserTokenAuthenticationInterceptor
public HttpResponseMessage Get(Guid id)
{
_log.Log(log something);
// some code thats gets some data using this._loginService
_log.Log(log the save);
_unitOfWork.Save();
}
}
看点
public class UserTokenAuthenticationInterceptor : IInterceptionBehavior
{
private readonly ILoginService _loginService;
private readonly ILog _log;
public UserTokenAuthenticationInterceptor(ILog log, ILoginService loginService)
{
this._log = log;
this._loginService = loginService;
}
public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
{
_log.Log(log entering authentication aspect);
// do some authentication here using this._loginService
_log.Log(log exiting authentication aspect);
}
public IEnumerable<Type> GetRequiredInterfaces()
{
return Type.EmptyTypes;
}
public bool WillExecute { get { return true; }}
}
容器注册:
container.RegisterType<IUnitOfWork, UnitOfWork.UnitOfWork>(new HierarchicalLifetimeManager());
container.RegisterType<ILoginService , LoginService>();
container.RegisterType<ILog, LogService>();
我在这个例子中使用了 unity。谁能指出我正确的方向?
最佳答案
感谢大家的帮助,我终于弄明白了。
我从这篇文章中得到了大部分答案 https://unity.codeplex.com/discussions/446780
我使用了以下 nuget 包。
首先,我需要一个新的 IFilterProvider 实现。它的工作是向容器注册所有 Action 过滤器。
public class UnityActionFilterProvider : ActionDescriptorFilterProvider, IFilterProvider
{
private readonly IUnityContainer container;
public UnityActionFilterProvider(IUnityContainer container)
{
this.container = container;
}
public new IEnumerable<FilterInfo> GetFilters(HttpConfiguration configuration, HttpActionDescriptor actionDescriptor)
{
var filters = base.GetFilters(configuration, actionDescriptor);
foreach (var filter in filters)
{
container.BuildUp(filter.Instance.GetType(), filter.Instance);
}
return filters;
}
}
然后需要一个注册方法,注册新的actionfilterprovider,去掉原来的webapi实现。这需要在 Unity.WebApi nuget 包创建的 UnityConfig.cs 文件中的 RegisterComponents() 方法中执行。
public static void RegisterFilterProviders(IUnityContainer container)
{
var providers = GlobalConfiguration.Configuration.Services.GetFilterProviders().ToList();
GlobalConfiguration.Configuration.Services.Add(typeof(System.Web.Http.Filters.IFilterProvider),
new UnityActionFilterProvider(container));
var defaultprovider = providers.First(p => p is ActionDescriptorFilterProvider);
GlobalConfiguration.Configuration.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), defaultprovider);
}
在同一个 RegisterComponents() 方法中我注册了我的类型
container.RegisterType<IUnitOfWork, UnitOfWork.UnitOfWork>(new HierarchicalLifetimeManager());
container.RegisterType<ILoginService , LoginService>();
container.RegisterType<ILog, LogService>();
接下来,我需要创建一个基于 AuthorizeAttribute 的类。
public class UserTokenAuthenticationAttribute : AuthorizeAttribute
{
private ILoginService _loginService;
// This is the magic part - Unity reads this attribute and sets injects the related property. This means no parameters are required in the constructor.
[Microsoft.Practices.Unity.Dependency]
public ILoginService LoginService
{
get
{
return this._loginService;
}
set
{
this._loginService = value;
}
}
protected override bool IsAuthorized(HttpActionContext actionContext)
{
// Authorise code goes here using injected this._loginService
}
}
还需要一个日志 Action 过滤器ActionFilterAttribute
public sealed class LogAttribute : ActionFilterAttribute
{
private ILog _log;
// This is the magic part - Unity reads this attribute and sets injects the related property. This means no parameters are required in the constructor.
[Microsoft.Practices.Unity.Dependency]
public ILog Log
{
get
{
return this._log;
}
set
{
this._log = value;
}
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
this._log.Info("Exited " + actionContext.Request.Method);
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
this._log.Info("Entering" + actionContext.Request.Method);
}
}
现在让我们配置 webapi Controller 。我们需要用我们的新属性来装饰这个类
[UserTokenAuthentication] // magic attribute in use
[Log] // magic attribute in use
public class MyController: ApiController
{
private readonly IUnitOfWork _unitOfWork;
private readonly ILoginService _loginService;
private readonly ILog _log;
public MyController(ILoginService loginService, IUnitOfWork unitOfWork, ILog log)
{
this._loginService = loginService;
this._unitOfWork = unitOfWork;
this._log = log;
}
[System.Web.Http.AllowAnonymous] // doesnt require authentication as were not logged in yet
public HttpResponseMessage Get(Guid id)
{
_log.Log(log something);
// some code thats gets some data using this._loginService
_log.Log(log the save);
_unitOfWork.Save();
}
public HttpResponseMessage GetMyDetails(Guid id)
{
_log.Log(log something);
// some code thats gets some data using this._loginService
_log.Log(log the save);
_unitOfWork.Save();
}
}
关于c# - 我可以让 WebApi 与 IoC Aspects/Interceptor 一起工作吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26907512/
我正在执行命令 ng g interceptor error 其中错误是拦截器的名称 但面临如下问题: An unhandled exception occurred: Schematic "inte
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是《quarkus依赖注入》系列的第五篇
我在每个 Controller 代码(例如@UseInterceptors(ClassSerializerInterceptor))中都使用了,所以我决定将其设置为全局,并且尝试设置它时不走运。 我在
应用程序开发语言:ionic + Restangular 我在 Restangular 中有一个用于 BaseUrl 和 Auth Interceptor 的全局配置。[就像 http://app 一
在我的服务类中,我需要可用的 hibernate session 。我目前在 beans.xml 中这样做: com.app.dao.UserDao
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码): https://github.com/zq2599/blog_demos 本篇概览 本
mybatis采用责任链模式,通过动态代理组织多个拦截器(插件),通过这些拦截器可以改变mybatis的默认行为(诸如sql重写之类的),由于插件会深入到mybatis的核心,因此在编写自己的插件前
下面给大家介绍mybatis interceptor 处理查询参数及查询结果,具体代码如下所示: ?
我一直在尝试实现应用程序范围的拦截器,但是,我一直看到以下错误: TypeError: this.interceptor.intercept is not a function 经过多次调试,我意识到
我有一个 HTTP 拦截器。后端 API 需要自定义 header ,其值在应用初始化和其他操作期间存储在 indexdb 中。因此,我需要获取值并在每个请求中传递它。但到目前为止,未能这样做,因为拦
我的 HibernateInterceptor 有问题。我延长了 EmptyInterceptor并覆盖 onFlushDirty方法。 我使用这种方法手动检查脏值(将以前的值与当前值进行比较)来为更
我在 Angular 5 中有一个有效的拦截器。它在 App.module 的提供者中注册,并正确拦截了从应用程序发出的所有请求。 问题是它不会拦截从应用程序使用的库发出的请求。 我正在使用开源库 (
我们已经实现了以下内容 function Inteceptors($httpProvider) { 'ng-inject'; $httpProvider.interceptors.pu
我需要拦截请求/响应,类似于 java 过滤器中的处理方式。在 Django 中执行此操作的最佳方法是什么?我只是要创建自己的中间件还是已经存在其他东西? 最佳答案 如果您需要拦截客户端请求/响应,请
目前,我们在 Jboss EAP 6.2 上使用 CXF 2.7.3 时遇到问题,并出现自定义 SoapFault 异常。 当我们发送自定义 SoapFault 时,不会显示子代码及其值: 这是我们想
CDI 中拦截器的范围是什么? aka,这合法吗?我会在每个调用拦截器的地方获得相同的拦截器实例吗? @RequestScoped public class SalesForceControllerI
我正在构建一个有 2 个拦截器的 angular 5 应用程序: 重试失败的 504 请求 另一个向用户显示有关失败请求的错误消息 我希望仅当错误不是 504 或错误为 504 并且已被第一个拦截器重
利用Spring MVC 的 Interceptor 实现个简易的性能监控,计算一下每个url的执行时间。 在 preHandle 方法中记录当前的时间戳到线程局部变量里,然后在afterComp
我想知道是否有办法在 Angular 的 HttpInterceptor 中检索当前路由。我正在使用来自不同路由的相同拦截器,但我需要检查是否正在从特定路由使用拦截器。如果是这样,我想在执行之前向后端
我创建了一个简单的拦截器来将用户重定向到登录页面,服务器返回 401 错误。但是它不起作用,因为状态被设置为 404,所以 $location 永远不会被调用。 .config( function m
我是一名优秀的程序员,十分优秀!