As @KiranChalla says, your MapRoutes have the same template name. If you provide the entire app.UseMvc statement (starting line 75 to the end of the routes declaration), someone is sure to show you exactly which parts need to change. – Matt DeKrey May 26 '15 at 21:06. We Resolve To: Challenge Ourselves and Others We envision a less polarized world with a shared commitment to transforming ambitious ideas into real benefits for people, communities, and ecosystems.
I was wondering if someone knows if it's possible to inject, via Unity, the Attribute class, which is being used on a method?
To be more precise, the project I work on is an MVC2 ASP.NET type, where the controller instances are being injected via Unity. All the dependencies, such as DB Contexts are configured in the Unity configuration file.
![Icallhandler resolve attributes Icallhandler resolve attributes](https://user-images.githubusercontent.com/10516222/40054561-8cc242fa-5845-11e8-9ac2-c994cf52cca4.png)
My question is, how can I inject the custom Attribute class, which also uses a DB Context, i.e. it has a dependency?
Here's an abstract of the controller class:
Thanks in advance for help.
N.
2 Answers
What you will need to do is to make your CustomAuthorize Attribute inherit from Microsoft.Practices.Unity.InterceptionExtension.HandlerAttribute and override the 'ICallHandler CreateHandler(IUnityContainer container)' method.
Now create a derived interface from Microsoft.Practices.Unity.InterceptionExtension.ICallHandler and add your IDBContextas a member.
IAuthorizationAttributeHandler implementation
To your unity configuration add simple interception extension.
Added the attribute to the interface method you wish to have the aspect execute.
Hope this helpsCheersRustin
Ant RadhaIt's not completely impossible, but it's very difficult.
The issue is that the attribute objects themselves are created by the CLR (the reflection API in particular, if I understand it correctly), so there's no way to get a container in there to call the constructor. And you don't have any real control over when the attribute instances are created, so you can't even guarantee if your container will be ready in time.
You could write a custom ActionInvoker that would spin through attributes and call BuildUp on each one, then plug that into the MVC pipeline. It's hard work though.
Jimmy Bogard has a series on getting more DI into an MVC app, the link points to the specific topic on Action Filters.
![Resolve Resolve](https://pieterderycke.files.wordpress.com/2010/10/image.png)
Not the answer you're looking for? Browse other questions tagged unity-container or ask your own question.
Policy injection block has been deprecated in Enterprise Library 5.0, however the ExceptionHandling application block contains ExceptionHandlingCallHandler which was usually found useful when used in conjunction with the Policy Injection App Block.
The purpose of this CallHandler is to handle exceptions using the policies defined in app.config/web.config for the ExceptionHandling app block.
One possible way of using the ExceptionHandlingCallHandler is to use the ExceptionHandlingCallHandler attribute on the methods that need Exception Handling. However the class must be enabled for interception by the Unity interceptors, since the ExceptionHandlingCallHandler attribute enables interception using the Unity Interception framework, hence the containing class should be enabled for interception by the Unity framework.
There are three possible ways of interception:
TransparentProxyInterceptor
InterfaceInterceptor
VirtualMethod interceptor
TransparentProxyInterceptor and InterfaceInterceptors are both interface interceptors and create a proxy around the class that needs interception.
InterfaceInterceptor is much better performing as compared to TransparentProxyInterceptors and hence we should choose InterfaceInterceptors except when we need intercept a class that is MarshalByRef object.
VirtualMethodInterceptor is a Type interceptor hence only works on abstract classes or concrete classes.
The following config shows how to enable a interface for interception:
In the above unity config, we first need to define the sectionExtension for interception:
<sectionExtension type='Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration' />
Then while registering the type with unity we also specify that we need this type to be intercepted. in the above example it uses InterfaceInterception for interception and uses the policyIntection Behavior for determining when the type should be intercepted.
Elsewhere in a common config I have defined the following policy :
In the above config I have defined a policy injection behavior which uses a CustomAttributeMatchingRule to check if a method is decorated with a “OperationContract” attribute and if so then the type is selected for Interception by unity. Thus now if the method in ICountryService is also decorated with the ExceptionHandlingCallHandler attribute, then the Exception Handling for such a method will now be enabled.
However if we don’t want to decorate a method with the ExceptionHandlingCallHandler attribute and we need to enable ExceptionHandling purely using Configuration, then we can create a CallHandler which implements ICallHandler and reads the Exception Handling policy configuration from the app.config/web.config. The following shows such an implementation:
We need this class since we need an instance of ExceptionPolicyImpl and I used the same logic to create such an instance, as is specified in the ExceptionHandlingCallHandlerAttribute implementation.
Once we have this, just call this CallHandler to the unity config and all methods that satisfy the MatchingRule (in our case the CustomAttributeMatchingRule which looks for presence of OperationContract attribute on a method) would then call this ExceptionCallHandler. the example config will look like the following:
In the above config we have explicitly added the following extension:
<extension type='Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common' />
and then added a reference to the created ExceptionCallHandler class do perform exception handling using the “UIPolicyException” policy . A sample Exception Handling policy looks like the following: