Cysharp / MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
MIT License
3.68k stars 417 forks source link

I can't have multiple implementations! #725

Open zhoudi94 opened 6 months ago

zhoudi94 commented 6 months ago
    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

github-actions[bot] commented 1 week ago

This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days.