Inject service with different configuration into controller - asp.net-web-api

In a Web API application, I have two controllers, MyAController and MyBController, each depending on IMyService but with a different configuration:
public class MyAController : ApiController
{
private readonly IMyService service;
public MyAController(IMyService service)
{
this.service = service;
}
}
public class MyBController : ApiController
{
private readonly IMyService service;
public MyBController(IMyService service)
{
this.service = service;
}
}
public interface IMyService
{
}
public class MyService : IMyService
{
private readonly string configuration;
public MyService(string configuration)
{
this.configuration = configuration;
}
}
I've tried configuring DryIoc the following way:
private enum ServiceKeyEnum
{
ServiceA,
ServiceB
}
container.RegisterInstance("configurationA", serviceKey: "CONFIGURATIONA");
container.RegisterInstance("configurationB", serviceKey: "CONFIGURATIONB");
container.Register<IMyService, MyService>(Reuse.Singleton, Made.Of(() => new MyService(Arg.Of<string>("CONFIGURATIONA"))), serviceKey: ServiceKeyEnum.ServiceA);
container.Register<IMyService, MyService>(Reuse.Singleton, Made.Of(() => new MyService(Arg.Of<string>("CONFIGURATIONB"))), serviceKey: ServiceKeyEnum.ServiceB);
container.Register<MyAController>(Reuse.InResolutionScope, made: Parameters.Of.Details((r, p) => ServiceDetails.IfUnresolvedReturnDefault).Type<IMyService>(serviceKey: ServiceKeyEnum.ServiceA));
container.Register<MyBController>(Reuse.InResolutionScope, made: Parameters.Of.Details((r, p) => ServiceDetails.IfUnresolvedReturnDefault).Type<IMyService>(serviceKey: ServiceKeyEnum.ServiceB));
and if I try to call resolve using:
var controllerA = container.Resolve<MyAController>();
var controllerB = container.Resolve<MyBController>();
I get two controllers configured with configurationA and configurationB respectively.
However, when I try to call the api using a REST call, I get the following error:
An error occurred when trying to create a controller of type 'MyAController'. Make sure that the controller has a parameterless public constructor.
so I guess, that I need to register the controller in a different way... but how?
Any help would be greatly appreciated....

The error is caused by improper setup for controllers. The DryIoc.WebApi extension have already discovered and registered your controllers, so normally you don't need to do it yourself. I will provide the working code (from the question comments) for you specific setup later. But now the reason behind the "parameterless constructor..": when DryIoc fails, WebAPI falls back to using Activator.CreateInstance for controller, which expects parameterless constructor. The fallback masks the original DryIoc error. To find it, you can setup DryIoc.WebApi extension as:
container = container.WithWebApi(throwIfUnresolved: type => type.IsController());
The working setup for your case, which registers dependencies with condition to select controller for injection:
container.Register<IMyService, MyService>(Made.Of(
() => new MyService(Arg.Index<string>(0)), _ => "configurationA"),
Reuse.Singleton,
setup: Setup.With(condition: r => r.Parent.ImplementationType == typeof(MyAController)));
container.Register<IMyService, MyService>(Made.Of(
() => new MyService(Arg.Index<string>(0)), _ => "configurationB"),
Reuse.Singleton,
setup: Setup.With(condition: r => r.Parent.ImplementationType == typeof(MyBController)));
The main thing that this setup does not require special controller registration.
Plus you can avoid using service keys, and no need to register config strings separately.

Related

Call two Action Methods and Combine the responses to produce new response in .NET Web API

I have two versions of an API.
The second version of API will be having only one action method instead of two action methods in first version of API.
Second version of API action method will basically combine responses of first version of API's both action methods and return combined response to client.
Example code as follows:
[ApiController]
[Route("[controller]")]
public class NumbersV1Controller : ControllerBase
{
private readonly ILogger<NumbersV1Controller> _logger;
public NumbersV1Controller(ILogger<NumbersV1Controller> logger)
{
_logger = logger;
}
[HttpGet]
public int Get()
{
return 1;
}
[HttpPost]
public int Post()
{
return 2;
}
}
[ApiController]
[Route("[controller]")]
public class NumbersV2Controller : ControllerBase
{
private readonly ILogger<NumbersV2Controller> _logger;
public NumbersV2Controller(ILogger<NumbersV2Controller> logger)
{
_logger = logger;
}
[HttpPost]
public IEnumerable<int> Get()
{
// Method 1: Make a direct HTTP request.
// int response1 = HTTPClientHelper.GetRequest("Get", "NumbersV1");
// int response2 = HTTPClientHelper.PostRequest("Post", "NumbersV1");
// Method 2: Use instances and set controller context.
NumbersV1Controller numbersV1Controller = new NumbersV1Controller(null);
numbersV1Controller.ControllerContext = this.ControllerContext;
int response1 = numbersV1Controller.Get();
int response2 = numbersV1Controller.Post();
// Method 3: Use RedirectToAction method.
// RedirectToActionResult response1 = RedirectToAction("Get", "NumbersV1");
// RedirectToActionResult response2 = RedirectToAction("Post", "NumbersV1");
return new List<int>() { response1, response2 };
}
}
Method 1: Make a direct HTTP request.
It works perfectly but it is having additional boilerplate code and also it like making a new network call.
Method 2: Use instances and set controller context.
Not sure if this will work perfectly like can I access the Request object in version 1 controller and not sure how to initialize the version 2 controller will multiple injected objects
Method 3: Use RedirectToAction method.
I was assuming RedirectToAction will work but I don't see the result of the Action method in response object RedirectToActionResult.
What are the best options available for doing this in .NET Web API or is there any other way of doing this elegently?
Avoid using method 2 / method 3. Why? It violates so many patterns and performance will be an issue.
Method 1 is average if you really want to do it that way but will cost a network call though.
Method 4:
You can call directly inline business logic code from your V2 controller. If you already separated your business logic code to an individual service then you need to call it from your controller.
I have introduced a new class to do all the logical operations. You might have a similar one / many service classes for handling business requirements.
Let me give you an example:
public class Number1Controller : BaseController {
// You can use DI container to resolve this. I am using this as an example.
private readonly Service _service = new();
[HttpGet("{id}")]
public int GetById(int id) => _service.GetById(id);
[HttpGet("{name}")]
public string GetByName(string name) => _service.GetByName(name);
}
public class Number2Controller : BaseController {
// You can use DI container to resolve this. I am using this as an example.
private readonly Service _service = new();
[HttpGet("{id}")]
public int GetById(int id) => _service.GetById(id);
[HttpGet("{name}")]
public string GetByName(string name) => _service.GetByName(name);
}
// Business Logic Service
public class Service {
public int GetById(int id) => 1;
public string GetByName(string name) => "Stack Over Flow";
}

Service Fabric Remoting to WebApi

We are running a few Stateless Reliable Services and are having performance issues with service-to-service communication using the reverse proxy (http://localhost:19081/{app}/{svc}/bleh). Without getting into the details there, we are looking into using remoting as described here: https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-communication-remoting
However, I am having a hard time figuring out how I would expose the API methods in the service type class, as they currently exist in our controllers. The controllers, via dependency injection, get the repository instances needed, etc..., so I'm spinning my wheels on how to get this accomplished without some sort of redundant instances or circular dependency.
I'm sitting here staring at this on "PersonService.cs":
internal sealed class PersonService: StatelessService, IPersonService
{
public PersonService(StatelessServiceContext context)
: base(context)
{ }
...
public PersonResponse GetPersonFromDb()
{
//lost here :(
}
Where my controller, which works fine, has:
public PersonController(IPersonRepository personRepository)
{
_personRepository = personRepository;
}
...
public IActionResult GetPerson()
{
var personResponse = _dbRepository.GetPerson();
return new ObjectResult(personResponse);
}
D:
Can't you pass the repository to your service, similar to this?
public PersonService(StatelessServiceContext context, IPersonRepository personRepository)
: base(context)
{
_personRepository = personRepository;
}
public PersonResponse GetPersonFromDb()
{
var personResponse = _personRepository.GetPerson();
return personResponse;
}

Ninject Method Injection Redis

I am trying to use Ninject to manage my Redis dependencies on a ASP.NET Web Api project.
I do my binding like this:
var clientManager = new PooledRedisClientManager("localhost");
kernel.Bind<IRedisClientsManager>()
.ToMethod(ctx => clientManager)
.InSingletonScope();
kernel.Bind<IRedisClient>()
.ToMethod(k => k.Kernel.Get<IRedisClientsManager>()
.GetClient());
How can I subsequently get access to my redis client in other classes in the project?
I'm not familiar with Redis, so beware...
Now that you've got a binding, you can inject it into a constructor
public class Foo {
public Foo(IRedisClient redisClient) {...}
}
Or you can use a func to access/create it at a specific time:
public class Foo {
private readonly Func<IRedisClient> redisClientFunc;
public Foo(Func<IRedisClient> redisClientFunc)
{
this.redisClientFunc = redisClientFunc;
}
public void DoSomething()
{
IRedisClient client = this.redisClientFunc();
client.SayHello();
}
}
or, equivalently, you can use the ninject factory extension to achieve the same as the func, but with an interface, see https://github.com/ninject/ninject.extensions.factory/wiki. Both Func<> and interface factory need the factory extension.

load repository with constructor parameters in Web API (1)

I'm trying to get the following scenario using autofac but I'm not sure how my code will be built to get this up & running.
I have a repository class, this repository class needs to get a project key (string) on initialization (constructor). I want to instantiate this repository in initialization of my "Initialize" method provided to my by Web Api, because the project key will be available in my route.
so instead of calling "new ProductRepository(projectKey)", I want to use Autofac. Can someone point me in the right direction? I didn't find any way to send in specific data to the container in web api, since the container/builder is only available in the appStart.
Should I make the container available as a singleton so that I can approach it, or is this bad practice?
in your initialization code:
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
var container = builder.Build();
var resolver = new AutofacWebApiDependencyResolver(container);
config.DependencyResolver = resolver;
in your controller:
public class MyController : ApiController
{
public MyController(IComponentContext container)
{
var key = new NamedParameter("projectKey", "keyFromRoute");
var repository = container.Resolve<ProductRepository>(key);
}
}
That should do it.
There is a nuget package that provides a DependencyResolver for WebApi that integrates with AutoFac. Create the DependencyResolver, assign it to the config, register your controllers in the autofac container.
I'm making some assumptions because you didn't provide your code, but I think you have something like this:
public class ProductRepository
{
public ProductRepository(DbContext dbContext, int projectKey)
{
}
}
public class SomeController : Controller
{
private readonly Func<int, ProductRepository> _repoFactory;
public SomeController(Func<int, ProductRepository> repoFactory)
{
_repoFactory = repoFactory;
}
public void DoStuff(int projectKey)
{
var repo = _repoFactory(projectKey);
repo.DoStuff();
}
}
public class RepositoryModule : Module
{
public override Load(ContainerBuilder builder)
{
builder.RegisterType<ProductRepository>();
}
}

Autofac delegate factory using func<>

I am trying to understand the delegate factory pattern with Autofac. I know how to implement factory using IIndex<> with Keyed() registration, which is explained nicely in here: Configuring an Autofac delegate factory that's defined on an abstract class
I would like to know if I can create a factory using Func<>, and how would I do the registrations for the following sample:
public enum Service
{
Foo,
Bar
}
public interface FooService : IService
{
ServiceMethod();
}
public interface BarService : IService
{
ServiceMethod();
}
public class FooBarClient
{
private readonly IService service;
public FooBarClient(Func<Service, IService> service)
{
this.service = service(Service.Foo);
}
public void Process()
{
service.ServiceMethod(); // call the foo service.
}
}
Autofac cannot construct this Func<Service, IService> for you which lets you return different types based on a parameter. This is what IIndex<> is for.
However if you don't want/cannot use IIndex<> you can create this factory function with the help of the Keyed or Named and register your factory in the container:
var builder = new ContainerBuilder();
builder.RegisterType<FooBarClient>().AsSelf();
builder.RegisterType<FooService>().Keyed<IService>(Service.Foo);
builder.RegisterType<BarService>().Keyed<IService>(Service.Bar);
builder.Register<Func<Service, IService>>(c =>
{
var context = c.Resolve<IComponentContext>();
return s => context.ResolveKeyed<IService>(s);
});

Resources