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

  • Home
  • SEARCH
  • 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 6581377
In Process

The Archive Base Latest Questions

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

I am trying to use ContextBoundObject and message sinks to inject some aspects into

  • 0

I am trying to use ContextBoundObject and message sinks to inject some aspects into my code.

My problem is that my aspect is being called only once –
when I make the call: myFacadee.GetValue("Tie")
I would expect to see my caching aspect be called twice

  1. Once for the ‘GetValue‘ method
  2. Secondly for the ‘GetValues‘ which is called internally in the
    ‘GetValue‘ method

However, it’s being called only once for the first ‘GetValue‘ method call.

How can change/fix the following code to make sure that all the methods on my ‘MyFacade‘ object cause the caching aspect to be invoked. Even if they are being called by other methods in the same ‘MyFacde‘ object?

Here is a simplified example of what my code looks like:

Test Application:

class Program
{
    static void Main(string[] args)
    {
        var myFacadee = new MyFacade();

        System.Console.WriteLine("Value:\t" + myFacadee.GetValue("Tie"));

        System.Console.ReadLine();
    }
}

Facade:

[Cache]
public class MyFacade : ContextBoundObject
{
    public string GetValue(string name)
    {
        return GetValues().FirstOrDefault(x => x.EndsWith(name));
    }

    public List<string> GetValues()
    {
        return new List<string>
                   {
                       "You asked for a Shirt",
                       "You asked for a Pants",
                       "You asked for a Tie"
                   };
    }
}

CacheAttribute:

[AttributeUsage(AttributeTargets.Class)]
public class CacheAttribute : ContextAttribute
{
    public CacheAttribute() : base("Security") { }

    public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
    {
        ctorMsg.ContextProperties.Add(new CacheProperty());
    }
}

CacheProperty:

public class CacheProperty : IContextProperty, IContributeObjectSink
{
    public IMessageSink GetObjectSink(MarshalByRefObject o, IMessageSink next)
    {
        return new CacheAspect(next);
    }

    public void Freeze(Context newContext)
    {
        // no op
    }

    public bool IsNewContextOK(Context ctx)
    {
        var newContextLogProperty = ctx.GetProperty("CacheProperty") as CacheProperty;
        if (newContextLogProperty == null)
        {
            Debug.Assert(false);
            return false;
        }
        return (true);
    }

    public string Name { get { return "CacheProperty"; } }
}

CacheAspect:

internal class CacheAspect : IMessageSink
{
    internal CacheAspect(IMessageSink next)
    {
        _next = next;
    }

    private readonly IMessageSink _next;

    public IMessageSink NextSink
    {
        get { return _next; }
    }

    public IMessage SyncProcessMessage(IMessage msg)
    {
        Preprocess(msg);

        var returnMethod = _next.SyncProcessMessage(msg);

        return returnMethod;
    }

    public IMessageCtrl AsyncProcessMessage(IMessage msg, IMessageSink replySink)
    {
        throw new InvalidOperationException();
    }

    private void Preprocess(IMessage msg)
    {
        // We only want to process method calls
        if (!(msg is IMethodMessage)) return;

        var call = msg as IMethodMessage;
        var type = Type.GetType(call.TypeName);
        var callStr = type.Name + "." + call.MethodName;

        var argsString = call.Args.Aggregate((current, next) => current + ", " + next);

        Console.WriteLine("Try to get value form cache : {0} for {1}({2})", callStr, call.MethodName, argsString);
    }
}
  • 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:08:42+00:00Added an answer on May 25, 2026 at 4:08 pm

    It is only getting called once because the context boundry is only crossed once. A call to the same instance from within the instance does not require any marshalling into the context, hence your sink is not used to intercept a message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use Thread but i have some problem (I am beginner at
Trying to use GnuPG with Delphi (Win32). I need to sign some file with
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
I am trying use javascript regular expressions to do some matching and I found
trying to use the preferred on method call but the code is not working
I have a regex that I'm trying use to validate against strings. Trying to
I have a 3rd party DLL that I am trying to use in a
I am trying use the jQuery table sorter plugin for a table that is
I am trying use the mysql connector in c++ in ubuntu. It appears that
I was trying use the code shown below to plot in such a way

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.