Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6586649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:49:05+00:00 2026-05-25T16:49:05+00:00

I’ve got a situation where policy injection no longer works when I’m using a

  • 0

I’ve got a situation where policy injection no longer works when I’m using a derived class.

The classes involved look like this (basically an interface, an abstract base class, and an implementation class):

public interface IRepository<T>
{
    void Create(T iItem);
}

public abstract class ElmtRepository<T> : IRepository<T>
{
    protected List<T> Items { get; set; }

    public ElmtRepository()
    {
        Items = new List<T>();
    }

    public void Create(T iItem)
    {
        Items.Add(iItem);
    }
}

public class AcctPgmRepository : ElmtRepository<AcctPgm>
{
}

The configuration looks like this:

  <container>
    <extension type="Interception"/>
    <register type="IRepository[AcctPgm]" mapTo="AcctPgmRepository">
      <interceptor type="InterfaceInterceptor"/>
      <interceptionBehavior type="PolicyInjectionBehavior"/>
    </register>
    <interception>
      <policy name="policy-create">
        <matchingRule name="create-rule1" type="TypeMatchingRule">
          <constructor>
            <param name="typeName">
              <value value="AcctPgmRepository"/>
            </param>
          </constructor>
        </matchingRule>
        <matchingRule name="create-rule2" type="MemberNameMatchingRule">
          <constructor>
            <param name="namesToMatch">
              <array type="string[]">
                <value value="Create"/>
              </array>
            </param>
          </constructor>
        </matchingRule>
        <callHandler name="create-handler1" type="AcctPgmAuthorizationHandler">
          <lifetime type="singleton"/>
          <constructor>
            <param name="allowedRoles">
              <array type="string[]">
                <value value="GroupController"/>
              </array>
            </param>
          </constructor>
        </callHandler>
      </policy>
    </interception>
  </container>

If I remove the ElmtRepository base class, it works as expected. With the base class, the injection doesn’t happen. No error messages, but no policies either. This happens even if I implement the Create() method in the derived class.

Is there a way to make this sort of class hierarchy work with Unity policy injection?

Thanks,
Jim

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:49:06+00:00Added an answer on May 25, 2026 at 4:49 pm

    This kind of class inheritance Unity usually does without a problem. However configuring generics is endless headaches with poor error messages. I bet that’s your real problem. However since you didn’t post your error message (or the AcctPgm class, or the AcctPgmAuthorizationHandler) I can’t be sure.

    I changed the contained type to int and got this version of your code working:

    using System;
    using System.Collections.Generic;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Microsoft.Practices.Unity.InterceptionExtension;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
    
    namespace UnityTest
    {
        public interface IRepository<T>
        {
            void Create(T iItem);
        }
    
        public abstract class ElmtRepository<T> : IRepository<T>
        {
            protected List<T> Items { get; set; }
    
            public ElmtRepository()
            {
                Items = new List<T>();
            }
    
            public void Create(T iItem)
            {
                System.Diagnostics.Debug.WriteLine("Creating...");
                Items.Add(iItem);
            }
        }
    
        public class AcctPgmRepository : ElmtRepository<int> { }
    
        public class LogHandler : ICallHandler
        {
            public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
            {
                System.Diagnostics.Debug.WriteLine("Begin");
                IMethodReturn result = getNext().Invoke(input, getNext);
                System.Diagnostics.Debug.WriteLine("End");
                return result;
            }
            public int Order { get; set; }
        }
    
        [TestClass]
        public class InheritenceGenericsTests
        {
            [TestMethod]
            public void CreateTest()
            {
                IUnityContainer container = new UnityContainer().LoadConfiguration("Inheritence");
                IRepository<int> r2 = container.Resolve<IRepository<int>>();
                Assert.IsNotNull(r2);
                r2.Create(2);
            }
        }
    }
    

    with config:

    <alias alias="IRepository" type="UnityTest.IRepository`1, UnityTest"/>
    <alias alias="IRepositoryClosed" type="UnityTest.IRepository`1[System.Int32], UnityTest"/>
    <alias alias="AcctPgmRepository" type="UnityTest.AcctPgmRepository, UnityTest"/>
    
    <container name="Inheritence">
      <extension type="Interception"/>
      <!-- register either type="IRepositoryClosed" or type="IRepository" -->
      <register type="IRepositoryClosed" mapTo="AcctPgmRepository">
        <interceptor type="InterfaceInterceptor"/>
        <policyInjection/>
      </register>
      <interception>
        <policy name="policy-create">
          <matchingRule name="create-rule2" type="MemberNameMatchingRule">
            <constructor>
              <param name="namesToMatch">
                <array type="string[]">
                  <value value="Create"/>
                </array>
              </param>
            </constructor>
          </matchingRule>
          <callHandler name="create-handler1" type="UnityTest.LogHandler, UnityTest"></callHandler>
        </policy>
      </interception>
    </container>
    

    Gives output:

    Begin
    Creating...
    End
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.