Note that there are some explanatory texts on larger screens.

plurals
  1. POFull stacktrace not being return when intercepting a method using Microsoft Enterprise Library Unity
    text
    copied!<p>I'm working with an application which is using Unity (v2.0) as part of Microsoft Enterprise Library. It is set up so you can add an attribute to the top of a method, and something will be done (tracing/logging/caching et all), before the method executes. Sample code below:</p> <pre><code>static void Main(string[] args) { IUnityContainer myContainer = new UnityContainer() .AddNewExtension&lt;Interception&gt;() .RegisterType&lt;IFoo, Foo&gt;( new Interceptor&lt;InterfaceInterceptor&gt;(), new InterceptionBehavior&lt;PolicyInjectionBehavior&gt;() ); IFoo myFoo = myContainer.Resolve&lt;IFoo&gt;(); myFoo.fooMethodCall(); } public class TraceAttribute : HandlerAttribute { public override ICallHandler CreateHandler(IUnityContainer container) { return new TraceCallHandler(); } } public class TraceCallHandler : ICallHandler { public int Order { get; set; } public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) { IMethodReturn methodReturn; //Do tracing/chaching/other funky stuff methodReturn = getNext()(input, getNext); return methodReturn; } } public interface IFoo { int fooMethodCall(); } public class Foo : IFoo { [Trace] public int fooMethodCall() { return 0; } } </code></pre> <p>Now, there are two annoyances I have with the way this works, and one pretty serious problem. The first annoyance is the size of the stacktrace which is generated <img src="https://i.stack.imgur.com/apAi3.png" alt="see here">. Trying to debug a function which is 5 levels deep results in some hideous stacktraces which are very difficult to navigate.</p> <p>The second annoyance is that when debugging using VS2010, it seems impossible to step into <code>fooMethodCall</code>. Trying to step in results in the same behaviour as if I had tried to step over the method call.</p> <p>The actual problem I am having is with error handling. Imagine I change <code>fooMethodCall</code> to be the following:</p> <pre><code>public int fooMethodCall() { throw new Exception(); } </code></pre> <p>and call it like so:</p> <pre><code>try { myFoo.fooMethodCall(); } catch Exception(e) { //do something with the exception } </code></pre> <p>If I examine e.StackTrace, it is most unhelpful:</p> <pre><code>at DynamicModule.ns.Wrapped_IFoo_dd88617b4f734f1987dc0099d195ca52.fooMethodCall() at ConsoleApplication1.Program.Main(String[] args) in C:\Projects\ConsoleApplication1\Program.cs:line 152 </code></pre> <p>All this tells me is that fooMethodCall failed. It could have failed right at the start, or in a method nested 5 levels down. </p> <p>Several questions about this then. Does this code look right? If so, are the annoyances/problem I have listed something that is unavoidable when intercepting method calls? Is there a better way to do what I'm trying to achieve?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload