You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
zhoudi94
changed the title
I have multiple implementations
I can't have multiple implementations!
Dec 21, 2023
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?
The text was updated successfully, but these errors were encountered: