Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't have multiple implementations! #725

Closed
zhoudi94 opened this issue Dec 19, 2023 · 1 comment
Closed

I can't have multiple implementations! #725

zhoudi94 opened this issue Dec 19, 2023 · 1 comment
Labels
stale stale not updated issue & pr

Comments

@zhoudi94
Copy link

zhoudi94 commented Dec 19, 2023

    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 zhoudi94 changed the title I have multiple implementations I can't have multiple implementations! Dec 21, 2023
Copy link
Contributor

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.

@github-actions github-actions bot added the stale stale not updated issue & pr label Jun 19, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale stale not updated issue & pr
Projects
None yet
Development

No branches or pull requests

1 participant