Mapping an interface as CallHandler

Topics: Policy Injection Application Block
Jul 29, 2011 at 8:10 AM
Edited Jul 29, 2011 at 8:12 AM

Hi

I am trying to set up an interception, which works great until I try to set an interface as type for the callhandler.

I get the following error:

"The current type, XXXX, is an interface and cannot be constructed. Are you missing a type mapping?"

The thing is that the interface is mapped to a type in the unity configuration file, so I am thinking there should not be a problem.

I can't see anything I am doing wrong... Does anyone know if the scenario above is unsupported? It would be very beneficial in those cases where several policies use the same callhandler so they can all be switched at one place.

Edit: Maybe I should mention I configure by file.

/K

Aug 1, 2011 at 4:02 AM

Hi,

Can you share us your configuration file?

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 1, 2011 at 9:16 AM

Hi... this is configuration file (unrelated stuff removed).

 

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity>
    <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />
    <typeAliases>
      <typeAlias alias="string" type="System.String, mscorlib" />
      <typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager, Microsoft.Practices.Unity" />
      <typeAlias alias="ITextRepository" type="WI4Lab.Repository.Text.ITextRepository, WI4Lab" />
      <typeAlias alias="TextRepository" type="WI4Lab.Repository.Text.TextRepository, WI4Lab" />
      <typeAlias alias="ITextLogger" type="WI4Lab.Proxy.Text.ITextLogger, WI4Lab" />
      <typeAlias alias="SaveTextLogger" type="WI4Lab.Proxy.Text.SaveTextLogger, WI4Lab" />
      <typeAlias alias="ITextService" type="WI4Lab.Service.Text.ITextService, WI4Lab" />
      <typeAlias alias="TextService" type="WI4Lab.Service.Text.TextService, WI4Lab" />
    </typeAliases>
    <containers>
      <container name="container">
        <extension type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Unity.EnterpriseLibraryCoreExtension, Microsoft.Practices.EnterpriseLibrary.Common" />
        <extension type="Interception" />
	<register type="ITextService" mapTo="TextService" >
          <lifetime type="singleton" />
        </register>        
	<register type="ITextLogger" mapTo="SaveTextLogger" >
          <lifetime type="singleton" />
        </register>
        <register type="ITextRepository" mapTo="TextRepository" >
          <lifetime type="singleton" />
          <interceptor type="InterfaceInterceptor"/>
          <interceptionBehavior type="PolicyInjectionBehavior" />
        </register>
       
        <interception>
          <policy name="SaveTextShouldLog">
            <matchingRule name="ShouldBeRepository" type="TypeMatchingRule">
              <constructor>
                <param name="typeName" value="IRepository`1" />
              </constructor>
            </matchingRule>
            <matchingRule name="ShouldHaveSaveChangesMethod" type="MemberNameMatchingRule">
              <constructor>
                <param name="nameToMatch" >
                  <value value="SaveChanges"/>
                </param>
              </constructor>
            </matchingRule>
            <callHandler name="ShouldBeLogged" type="ITextLogger">
              <lifetime type="singleton"/>
            </callHandler>
          </policy>
        </interception>

      </container>
    </containers>
  </unity>
</configuration>


If I run it like this i get the error:

Resolution of the dependency failed, type = "WI4Lab.Service.Text.TextService", name = "(none)".
Exception occurred while: Calling constructor Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior(Microsoft.Practices.Unity.InterceptionExtension.CurrentInterceptionRequest interceptionRequest, Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[] policies, Microsoft.Practices.Unity.IUnityContainer container).
Exception is: ResolutionFailedException - Resolution of the dependency failed, type = "Microsoft.Practices.Unity.InterceptionExtension.ICallHandler", name = "bb619854-36b7-4bba-9ace-709444c8a39d".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, WI4Lab.Proxy.Text.ITextLogger, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:

  Resolving WI4Lab.Proxy.Text.ITextLogger,bb619854-36b7-4bba-9ace-709444c8a39d (mapped from Microsoft.Practices.Unity.InterceptionExtension.ICallHandler, bb619854-36b7-4bba-9ace-709444c8a39d)

-----------------------------------------------
At the time of the exception, the container was:

  Resolving WI4Lab.Service.Text.TextService,(none)
  Resolving parameter "repository" of constructor WI4Lab.Service.Text.TextService(WI4Lab.Repository.Text.ITextRepository repository, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager exeptionManager, Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter logWriter, Microsoft.Practices.EnterpriseLibrary.Logging.TraceManager traceManager, Microsoft.Practices.EnterpriseLibrary.Validation.ValidatorFactory validatorFactory, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.CryptographyManager cryptographyManager, Microsoft.Practices.EnterpriseLibrary.Security.IAuthorizationProvider authProvider, WI4Lab.Repository.Text.INHObjectRepository objectRepository)
    Resolving WI4Lab.Repository.Text.TextRepository,(none) (mapped from WI4Lab.Repository.Text.ITextRepository, (none))
      Resolving Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior,(none)
      Calling constructor Microsoft.Practices.Unity.InterceptionExtension.PolicyInjectionBehavior(Microsoft.Practices.Unity.InterceptionExtension.CurrentInterceptionRequest interceptionRequest, Microsoft.Practices.Unity.InterceptionExtension.InjectionPolicy[] policies, Microsoft.Practices.Unity.IUnityContainer container)

If I change the callhandler from ITextLogger to SaveTextLogger, everything works as intended. If I look in the unity container, it looks as though ITextLogger has been mapped to SaveTextLogger correctly.

The textservice being constructed in the error message above is a WCF REST service that takes a repository as input argument. The repository is in turn connected to the interception behaviour.

 

 


Aug 2, 2011 at 6:08 AM

Hi,

I tried your code and I got the same. Though I can't find a documentation for this behavior, I strongly believe that the scenario you wanted is unsupported. Anyway, you can log it here as a feature request.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Aug 2, 2011 at 7:33 AM

Okies... thank you for your help.