Singleton and race conditions

In connection with this issue:

Link to null in web api call

But I propagate this separately, because I think this may be a problem with Ninject, and I don't want to confuse the original question if I am wrong.

My problem is that I have a web api project that uses Ninject to manage dependency injection, and it works great most of the time. The problem is that you immediately run a multiple request right after the application starts, sometimes they fail in some really strange way with NullReferenceExceptionsthat simply should not exist for objects that don't seem to be null, and I wonder if this is a problem with Ninject with help InSingletonScopeand does not give me a singleton, but sometimes it gives me something that did not complete the initialization (but perhaps it is initializing by the time I look at it in the debugger). Is it possible? Or am I completely barking the wrong tree?

I have a Ninject configured using Nuget packages and some web resources (sorry I don’t have a link to which ones) and I thought I was doing it right. I have links in my project to Ninjectand Ninject.Web.Common. As NinjectWebCommon.csI have is for the registration of multiple services (I have excluded the majority of them, to isolate only one that gives me the problem):

    private static void RegisterServices(IKernel kernel)
    {
        System.Diagnostics.Debug.WriteLine("Registering Services...");
        kernel.Bind<Services.IUserService>().To<Services.UserService>().InSingletonScope();                                 

        kernel.Bind<Services.IMyFinderService>().To<Services.MyFinderService>().InSingletonScope();
        //kernel.SingletonBind<Services.IMyFinderService>().To<Services.MyFinderService>();
        //kernel.Bind<Services.IMyFinderService>().ToConstant<Services.MyFinderService>(new Services.MyFinderService(kernel.Get<Services.IUserService>()));
    }  

MyFinderService- this is the one that gives me problems and notices that the constructor accepts IUserServicewhich was registered before it (and may also be the cause of the problem):

    public MyFinderService(IUserService service)
    {
        cache = new Dictionary<Guid, MyClassBase>();
        userService = service;
    }

, ( : Ninject InSingletonScope -Api RC), , - , API MyFinderService:

public MyClassBase GetThing(Guid id)
{
    if (cache.ContainsKey(id))
    {
        return cache[id];
    }
    else
    {
        var type = typeof(MyClassBase).Assembly.GetTypes().FirstOrDefault
        (
            t => t.IsClass &&
                t.Namespace == typeof(MyClassBase).Namespace + ".Foo" &&
                t.IsSubclassOf(typeof(MyClassBase)) &&
                (t.GetCustomAttribute<MyIdAttribute>()).GUID == id
        );
        if (type != null)
        {
            System.Diagnostics.Debug.WriteLine(string.Format("Cache null: {0}",cache == null));
            var param = (MyClassBase)Activator.CreateInstance(type, userService);
            cache[id] = param;
            return param;
        }
        return null;
    }
}

NullReferenceException :

cache[id] = param;

, cache, id param .

, , - , Ninject , , , , , , , ?

, web api, , .

+1
1

Ninject.Web.WebApi.WebHost, InSingletonScope .

, , stackoverflow, DependencyResolver ActivationBlock, , .

Ninject , , . , . Dictionary . . ConcurrentDictionary.

+3

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


All Articles