MagicOnion icon indicating copy to clipboard operation
MagicOnion copied to clipboard

I can't have multiple implementations!

Open zhoudi94 opened this issue 1 year ago • 1 comments

    1.get service
    private readonly IUserService _userService;
    public TestController(IServiceFactory serviceFactory)
    {
        _userService= serviceFactory.CreateService<IUserService>();
    }

    2.IServiceFactory.cs
    public interface IServiceFactory
    {
        T CreateService<T>();
    }

    public class ServiceFactory : IServiceFactory
    {
        private readonly IComponentContext _context;

        public ServiceFactory(IComponentContext context)
        {
            _context = context;
        }

        public T CreateService<T>()
        {
            var serviceName = typeof(T).Name;// UserServiceForS1 or UserServiceForS2
            return _context.ResolveKeyed<T>(serviceName);
        }
    }

   3.service implement
    public class UserServiceForS1 : ServiceBase<IUserService>, IUserService
    {
        public async UnaryResult<GetUserInfoResponseDto> GetUserInfo(GetUserInfoRequestDto request)
        {
            await Task.Delay(1);
            throw new NotImplementedException();
        }
 
    }

    public class UserServiceForS2 : ServiceBase<IUserService>, IUserService
    {
        public async UnaryResult<GetUserInfoResponseDto> GetUserInfo(GetUserInfoRequestDto request)
        {
            await Task.Delay(1);
            throw new NotImplementedException();
        } 
    }
    
   4.autofac register service
       container.RegisterType<UserServiceForS1>().Keyed<IUserService>("UserServiceForS1");
       container.RegisterType<UserServiceForS2>().Keyed<IUserService>("UserServiceForS2");

I want to get my service through the factory, but it reports an error, the error message is "Method does not allow overload, UserServiceForS2.GetUserInfo" ,can I inject my service via autofac? image

zhoudi94 avatar Dec 19 '23 15:12 zhoudi94