Skip to content
This repository was archived by the owner on May 10, 2018. It is now read-only.
This repository was archived by the owner on May 10, 2018. It is now read-only.

Injecting scoped services as callee #8

Open

Description

@kasravi

Hi @darkl , I'm trying to construct my callees as a service. Transient and Singleton services are working as expected, but when I inject scoped services, since there is no HTTP context available, they are created in the default scope and because of that they behave as singleton (we are using a DI pretty similar to .Net Core Di ). I found out that there is also a raw callee which exposes the invocation lifetime where I can create and dispose scopes before and after. Then I should build the wrapper again with another attribute. Is there any better way to achieve scoped invocation of callees?

Activity

  1. darkl commented on Jan 4, 2017

    @darkl
    Member

    I'm not sure I understand your problem. What are you trying to achieve?

    Elad

  2. kasravi commented on Jan 5, 2017

    @kasravi
    Author

    let me write some code (assume that ConfigureServices is called runtime at startup)

        public class RpcService
        {
            public static void ConfigureServices(IServiceCollection serviceCollection)
            {
                serviceCollection.AddTransient(typeof(RpcService));
            }
    
            public RpcServiceSubA _rpcServiceSubA;
            public UserInfo _userInfo;
    
            public RpcService(RpcServiceSubA rpcServiceSubA, UserInfo userInfo)
            {
                _rpcServiceSubA = rpcServiceSubA;
                _userInfo = userInfo;
            }
    
            [WampProcedure("ping")]
            public RpcService Ping()
            {
               return this;
            }
        }
    
       public class RpcServiceSubA
        {
            public static void ConfigureServices(IServiceCollection serviceCollection)
            {
                serviceCollection.AddTransient(typeof(RpcServiceSubA));
            }
    
            public UserInfo _userInfo;
    
            public RpcServiceSubA(UserInfo userInfo)
            {
                _userInfo = userInfo;
            }
        }
    
       public class UserInfo
        {
            public static void ConfigureServices(IServiceCollection serviceCollection)
            {
                serviceCollection.AddSingleton(typeof(UserInfo));
            }
            public Guid MyKey { get; } = Guid.NewGuid();
        }
    

    We then inject this service like this:

                ....
                using (IWampHost host = new DefaultWampHost(location))
                {
                     IWampHostedRealm realm = host.RealmContainer.GetRealmByName("realm1");
    
                    Task<IAsyncDisposable> registrationTask = realm.Services.RegisterCallee(()=>ServiceManager.GetRequiredService<RpcService>());
                    registrationTask.Wait();
    
                    host.Open();
    
                }
    

    In which the service manager is responsible for providing an instance for every call.
    Now consider if we register UserInfo as Singleton then we would have same MyKey in diffrent calls of ping:
    image

    if we register it as Transient then we would have diffrent MyKeys since the service manager would new up an instance every time:
    image

    These two are working as expected but if we add UserInfo as Scoped we expect that MyKeys would be same in every RPC call, since they are in different WampInvocationContext. something like this
    image
    but the result as I said above is similar to Singleton. I'm trying to make scoped services also work where scopes are created before calls and disposed afterwards.

  3. darkl commented on Jan 5, 2017

    @darkl
    Member
  4. kasravi commented on Jan 5, 2017

    @kasravi
    Author

    I'm not sure about the definition of custom scope but if you mean something like here then no. we just have the Transient, Singleton and something like Request. (our DI is completely similar to .Net Core DI)

  5. darkl commented on Jan 5, 2017

    @darkl
    Member

    That's a bummer, since with Ninject you could simply write:

    kernel.Bind<UserInfo>().ToSelf().InScope(x => WampInvocationContext.Current);
  6. kasravi commented on Jan 5, 2017

    @kasravi
    Author

    aah, well one way for me is to see what is happening in Ninject's custom scopes and if I can add this kind of scopes to our framework. I will continue this thread if there is no easy way towards custom scopes. thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions