I need a help with an method present in a WCF service. Let me show what I'm trying to do:
First, I've created one public class, just like this:
public class TEnvironment : ITEnvironment
{
private readonly IUserService _userService;
public TEnvironment(IUserService userService)
{
_userService = userService;
}
public string GetUserName(string user, string password)
{
string userName;
var username = _userService.GetByLoginAndPassword(user, password);
userName = username.UserName;
return userName;
}
}
After this, I've created one interface:
public interface ITEnvironment
{
string GetUserName(string user, string password);
}
Then finally, I've created one controller in my application (MVC), adding this:
public class UserController : BootstrapBaseController
{
private readonly ITEnvironment _tEnvironment;
public UserController (ITEnvironment tEnvironment)
{
_tEnvironment = tEnvironment;
}
}
If I comment the block:
public UserController (ITEnvironment tEnvironment)
{
_tEnvironment = tEnvironment;
}
My application runs. But I need the method "GetUserName(user, password)". If I don't comment that block, one exception is generated:
Error activating ITEnvironment using binding from ITEnvironment to TEnvironment
A cyclical dependency was detected between the constructors of two services.
Activation path:
3) Injection of dependency ITEnvironment into parameter userService of constructor of type TEnvironment
2) Injection of dependency ITEnvironment into parameter tEnvironment of constructor of type UserController
1) Request for UserController
Suggestions:
1) Ensure that you have not declared a dependency for ITEnvironment on any implementations of the service.
2) Consider combining the services into a single one to remove the cycle.
3) Use property injection instead of constructor injection, and implement IInitializable
If you need initialization logic to be run after property values have been injected.
Well, there's one present bind into my class "DependencyModule.cs", that you can see below:
public class DependencyModule : NinjectModule
{
public override void Load()
{
Bind<ITEnvironment>().To<TEnvironment>();
}
}
EDITED:
Here's my UserService:
public class UserService : IUserService
{
private readonly IRepository<User> _userData;
public UserService(IRepository<User> userRepository)
{
_userData = userRepository;
}
public User GetByLoginAndPassword(string username, string password)
{
var userInfo = _userData.Table.SingleOrDefault(x => x.Login == login);
if (userInfo == null)
throw new EntityNotFoundException("Invalid user name or password");
return userInfo;
}
}
IUserService:
public interface IUserService
{
User GetByLoginAndPassword(string username, string password);
}
Now... What am I doing wrong is the biggest question...