Kai
Kai

Reputation: 2073

ASP.NET MVC DI with controller action filter

I am using asp.net mvc controller with constructor injected service IMyService. MyService (implements IMyService) uses some contextual information in its constructor. This contextual information supplied from different third party web site. Contextual information is required for creating MyService instance.

Controller's action filter uses some logic in order to determine, if this contextual information is available. If not, then request is redirected to another web site. The problem is, that controller constructor is called by IoC container before controller's filter. As a result MyService throws exception if contextual information is not provided.

How to workaround this situation?

UPD:
It seems that DelegatingHandler is a way to go.

Upvotes: 1

Views: 175

Answers (1)

iandayman
iandayman

Reputation: 4467

If you need a quick workaround you could use remove the IMyService from your Constructor and instead add it in as a property of your controller and use your IoCs Service Locator to resolve it when it is needed by a method in the controller.

Upvotes: 1

Related Questions