gpt4 book ai didi

c# - 从 Unity 获取接口(interface)实例

转载 作者:行者123 更新时间:2023-11-30 16:44:09 25 4
gpt4 key购买 nike

我正在尝试在我的 MVC 应用程序中设置自定义身份验证过滤器,但我需要在身份验证过滤器中引用我的用户服务。我像这样使用 Unity 设置我的服务:

    private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});

public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}

public static void RegisterTypes(IUnityContainer container)
{

// container.LoadConfiguration();

// TODO: Register your types here
container.RegisterType<IUserService, UserService>();

}

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(GERPWeb.App_Start.UnityWebActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(GERPWeb.App_Start.UnityWebActivator), "Shutdown")]

namespace APP.App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();

FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}

/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}

只要我有一个构造函数,它就可以正常工作,就像这样:

public class UserController : BaseController
{

private readonly IUserService _userService;

public CustomerController(IUserService userService)
{
_userService = userService;

}

但是我的身份验证过滤器中不能有:

public class BasicAuthorization : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
//see if we can skip the authorization
bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), true);

if (!skipAuthorization)
{
//var userService = ServiceLocator.Current.GetInstance<IUserService>();
//get instance of user service here


base.OnAuthorization(filterContext);
}
}
}

然后在我的 FilterConfig.cs 中:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new BasicAuthorization());
filters.Add(new HandleErrorAttribute());
}

我找到了对称为服务定位器的东西的引用,并尝试尽我所能使用它,但我认为我遗漏了一些东西,因为我得到 ServiceLocationProvider must be set. 在该行,所有的谷歌链接都是针对 WPF 的。

我在这里错过了什么?

最佳答案

您已经设置了基本上遵循服务定位器模式的 DependencyResolver

public class BasicAuthorization : AuthorizeAttribute {
public override void OnAuthorization(AuthorizationContext filterContext) {
//see if we can skip the authorization
bool skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), true);

if (!skipAuthorization) {
//Using dependency resolver here
var userService = (IUserService) DependencyResolver.Current.GetService(typeof(IUserService));


base.OnAuthorization(filterContext);
}
}
}

您可以创建一个扩展方法以允许更清晰地解析实例

public static T GetService<T>(this IDependencyResolver container) {
return (T)container.GetService(typeof(T));
}

这将允许

var userService = DependencyResolver.Current.GetService<IUserService>();

关于c# - 从 Unity 获取接口(interface)实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43881730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com