我有麻烦注入当前loggedonuser到我的服务层,我想尝试类似于代码营服务器,但努力弄清楚为什么我的代码不工作...使用Ninject注入当前用户到我的域名
我的应用程序:UI层 - > conneced到域名服务 - >连接到回购层...
回购未连接到用户界面,一切都进行检查核实,并通过从的DomainService层回来......
我的代码:
//这是宣告我的域名服务
public interface IUserSession
{
UserDTO GetCurrentUser();
}
Inside无我的web应用程序我想实现这个服务,然后将其注入我的服务层,(这是我在哪里卡住):
public class UserSession : IUserSession
{
//private IAuthorizationService _auth;
public UserSession()//IAuthorizationService _auth)
{
//this._auth = _auth;
}
public UserDTO GetCurrentUser()
{
var identity = HttpContext.Current.User.Identity;
if (!identity.IsAuthenticated)
{
return null;
}
return null;
//return _auth.GetLoggedOnUser(identity.Name);
}
}
我想要做的是:得到认证服务的loggedonuser,但没有工作,所以我掐灭代码...
我绑定内Global.asax的一切都像:
protected override void OnApplicationStarted()
{
AreaRegistration.RegisterAllAreas();
// hand over control to NInject to register all controllers
RegisterRoutes(RouteTable.Routes);
Container.Get<ILoggingService>().Info("Application started");
//here is the binding...
Container.Bind<IUserSession>().To<UserSession>();
}
首先:当我尝试使用使用IUserSession的服务时发生异常,它表示请为控制器x提供一个默认的无参数构造函数,但是如果我从域服务中删除引用,则一切正常......
服务构造函数:
private IReadOnlyRepository _repo;
private IUserSession _session;
public ActivityService(IReadOnlyRepository repo, IUserSession _session)
{
this._repo = repo;
this._session = _session;
}
有没有实现这一种更好的方式/更简单的方法?
UPATE从下面我的回复帮助成功地完成这件事,我已经上传到gituhub如果有人想知道我是怎么做的?
https://gist.github.com/1042173
感谢您的回复,我正在使用应用程序替代来连接ninject,无论如何...我的问题的主要部分 - 我将如何将IMembershipProvider或IAuthenticationProvider连接到我的域图层,这是一个单独的类库,它不参考我的Web应用程序? – Haroon 2011-06-19 22:36:55
我在我的Core库中有'IMembershipProvider',它没有引用Web。我的控制器和服务也在Core中,并且在构造函数中注入了'IMembershipProvider'。当'NinjectControllerFactory'创建控制器时,它会向其中注入'IMembershipProvider'。然后,我的Web库中只有一个Ninject模块,它将接口绑定到具体类型“MembershipProvider”。所以你至少需要将接口放在与使用它的类相同的程序集中。 – 2011-06-20 13:55:45
那就是我所做的,我有我的领域层中定义的接口,然后我在我的网络应用程序内实现它,我用ninject手动绑定它在应用程序启动,我想我会尝试和绑定它内核Kernal,看看是否那工作... – Haroon 2011-06-22 10:53:45