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?
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](https://github.com/Cysharp/MagicOnion/assets/11438661/2dd8c14c-bbb5-40fd-80c9-df4e3ea18d63)