Ninject error resolution constructor with multiple string parameters

I am encountering an error using Ninject 3 with the MVC WebAPI / Windows Azure project. I have a repository object that looks something like this:

public class Repository : IRepository
{
    /// <summary>
    /// Initialize a new repository object.
    /// </summary>
    public Repository(
        CloudStorageAccount storageAccount,
        ISerializer serializer,
        int timeoutMS = 30000,
        string activityLogQueueName = "activitylog",
        string fileStageName = "filestage",
        string blobTrackingTableName = "blobtracking")
    {
        //snipped boring stuff
    }
 }

And in NinjecWebCommon I have bindings like:

kernel.Bind<Data.IRepository>().To<Data.Repository>();
kernel.Bind<Data.ISerializer>().To<Data.Serializer>();

kernel.Bind<Microsoft.WindowsAzure.Storage.CloudStorageAccount>().ToMethod((context) =>
{
    //code to get storage account from azure config
}

As the repository initializes, it uses strings for activityLogQueueName, etc. to create any missing containers and install Azure client objects. All this works fine - the problem is that Ninject somehow takes the value of the first string parameter ("activityLog") and passes it to all string parameters, so my container container and tracking table are ultimately called "activityLog".

If I specify these parameters in the binding as follows:

kernel.Bind<Data.IRepository>().To<Data.Repository>().WithConstructorArgument("fileStageName", "filestage");

, , .

, :

public class NinjectResolver : NinjectScope, IDependencyResolver
{
    private IKernel _kernel;
    public NinjectResolver(IKernel kernel)
        : base(kernel)
    {
        _kernel = kernel;
    }
    public IDependencyScope BeginScope()
    {
        return new NinjectScope(_kernel.BeginBlock());
    }
}

:

public class NinjectScope : IDependencyScope
{
    protected IResolutionRoot resolutionRoot;

    public NinjectScope(IResolutionRoot kernel)
    {
        resolutionRoot = kernel;
    }

    public object GetService(Type serviceType)
    {
        IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
        return resolutionRoot.Resolve(request).SingleOrDefault();
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        IRequest request = resolutionRoot.CreateRequest(serviceType, null, new Parameter[0], true, true);
        return resolutionRoot.Resolve(request).ToList();
    }

    public void Dispose()
    {
        IDisposable disposable = (IDisposable)resolutionRoot;
        if (disposable != null) disposable.Dispose();
        resolutionRoot = null;
    }
}

nuget.

+4

Source: https://habr.com/ru/post/1523410/


All Articles