C# 从Unity获取接口的实例
我正在尝试在MVC应用程序中设置自定义身份验证筛选器,但我需要在身份验证筛选器中引用我的用户服务。我使用Unity设置我的服务,如下所示:C# 从Unity获取接口的实例,c#,asp.net-mvc,unity-container,C#,Asp.net Mvc,Unity Container,我正在尝试在MVC应用程序中设置自定义身份验证筛选器,但我需要在身份验证筛选器中引用我的用户服务。我使用Unity设置我的服务,如下所示: private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() => { var container = new UnityContainer(); RegisterTypes(contai
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 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);
}
}
}
我找到了一个名为service locator的引用,并试图尽我所能使用它,但我认为我缺少了一些东西,因为我得到了ServiceLocationProvider必须设置。
在那一行,所有的google链接都是针对WPF的
这里缺少什么?您已经设置了
依赖解析程序
,它基本上遵循服务定位器模式
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));
}
publicstatict GetService(此IDependencyResolver容器){
return(T)container.GetService(typeof(T));
}
这将允许
var userService = DependencyResolver.Current.GetService<IUserService>();
var userService=dependencysolver.Current.GetService();
public static T GetService<T>(this IDependencyResolver container) {
return (T)container.GetService(typeof(T));
}
var userService = DependencyResolver.Current.GetService<IUserService>();