1

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...

DanielS
  • 11
  • 3
  • According to the line "Injection of dependency ITEnvironment into parameter userService of constructor of type TEnvironment", it appears that something is trying to inject a TEnvironment as a IUserService. Try removing that constructor from TEnvironment and see what happens. – Grax32 Jan 28 '15 at 18:23
  • Can you show how your UserService Class is structured? Are you injecting an ITEnvironment into the UserService class and injecting a IUserService in the TEnvironment class? http://stackoverflow.com/questions/3511547/cyclic-dependency-with-ninject – broguyman Jan 28 '15 at 18:26
  • Is your `UserService` getting constructed with `ITEnvironment`? Then you have 2 services that are dependent on each other. Either remove one of the dependencies if that's an option, or use property injection instead. – boosts Jan 28 '15 at 18:26
  • You never told ninject how to resolve a `IUserService` in your bindings. If you add the line `Bind().To – Tommy Jan 28 '15 at 18:54
  • Thanks for your help Grax, broguyman, boosts and Tommy. I edited my question, trying to make one clear question. Hope it could be useful – DanielS Jan 28 '15 at 19:49
  • @DanielS - did you add the binding that I showed in my previous comment to your `DependencyModule`?. Also, it appears you need to show ninject how to resolve a `IRepository` as well. Anytime you pass an interface into a constructor, ninject needs to be told what class implements that interface in the bindings module, else it won't know what to do with it. – Tommy Jan 28 '15 at 21:39
  • @Tommy - Yes, I did. Now I'm looking for my code, and thinking how can I resolve it. I think I'll try something here to resolve, eliminating some blocks. Maybe I'll resolve this. If I can solve this, I'll post here the solution – DanielS Jan 29 '15 at 11:07

0 Answers0