1

So I have a Unity.WebApi installed on my MVC Application. There is also a web api section in a folder called "Areas".

I'm in the process of wiring things up to implement caching via policy injection and attributes on the Web Api.

Here's my Bootstrapper.cs class:

public static void Initialise(IUnityContainer theContainer)
{
    var container = BuildUnityContainer(theContainer);

    GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
}

private static IUnityContainer BuildUnityContainer(IUnityContainer theContainer)
{
    theContainer.RegisterType<IPatientSearchController, PatientSearchController>(
            new InterceptionBehavior<PolicyInjectionBehavior>(),
            new Interceptor<InterfaceInterceptor>());

    // register all your components with the container here
    // e.g. container.RegisterType<ITestService, TestService>();            

    return theContainer;
}

And in the global.asax I have this:

IUnityContainer container = Application.GetContainer();
container.AddNewExtension<Interception>();

ContainerBootstrapper.RegisterTypes(container);
Bootstrapper.Initialise(container);

I already have the cachingAttribute and cachingCallHandler implemented. And I want to be able to attribute my webapi methods so they have caching ability.

[caching]
public int GetNumber() 
{
    return 5;
}

I believe I have most things correctly implemented here. But I keep getting this error when I access the api method from the URL

Type passed must be an interface. System.ArgumentException

at System.RuntimeTypeHandle.VerifyInterfaceIsImplemented(RuntimeTypeHandle handle, RuntimeTypeHandle interfaceHandle) at System.RuntimeType.GetInterfaceMap(Type ifaceType) at Microsoft.Practices.Unity.InterceptionExtension.InterfaceInterceptor.d__0.MoveNext() at Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior..ctor(CurrentInterceptionRequest interceptionRequest, InjectionPolicy[] policies, IUnityContainer container) at lambda_method(Closure , IBuilderContext ) at Microsoft.Practices.ObjectBuilder2.DynamicBuildPlanGenerationContext.<>c_DisplayClass1.b_0(IBuilderContext context) at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context) at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context) at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) at Microsoft.Practices.ObjectBuilder2.BuilderContext.NewBuildUp(NamedTypeBuildKey newBuildKey, Action1 childCustomizationBlock) at Microsoft.Practices.Unity.InterceptionExtension.InterceptionBehaviorsPolicy.<GetEffectiveBehaviors>d__4.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at Microsoft.Practices.Unity.InterceptionExtension.InstanceInterceptionStrategy.PostBuildUp(IBuilderContext context) at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context) at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name, IEnumerable`1 resolverOverrides)

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
coolcat21
  • 11
  • 1
  • Is your PatientSearchController has the public int GetNumber() Api method? – Spock Nov 04 '13 at 08:25
  • Yes it's in the interface IpatientSearchController and the class also PatientSearchController – coolcat21 Nov 04 '13 at 20:27
  • You might be missing some configuration. Have you tried...... theContainer.RegisterType(new ContainerControlledLifetimeManager()); theContainer.AddNewExtension() .Configure() .SetDefaultInterceptorFor(new TransparentProxyInterceptor()); – Spock Nov 04 '13 at 22:18
  • I tried your code but it gave me this error when i try accessing the web api method: Cannot reuse an 'ApiController' instance. 'ApiController' has to be constructed per incoming message. Check your custom 'IHttpControllerActivator' and make sure that it will not manufacture the same instance. – coolcat21 Nov 05 '13 at 20:59
  • It might be the issue with 'new ContainerControlledLifetime()' manager. Try removing that and you whether you can progress. – Spock Nov 05 '13 at 21:07
  • It works if i remove the containercontrolledlifetime thing. But..it just calls the api method all the time...it doesn't go through the cacheAttribute nor the cacheHandler – coolcat21 Nov 05 '13 at 21:33
  • I think my code posted in my first post is really close...i just couldn't for the life of me figure out what's causing the "type passed must be an interface" error. But IT IS ! grr :( – coolcat21 Nov 05 '13 at 21:42

0 Answers0