Note that there are some explanatory texts on larger screens.

plurals
  1. POAOP with Ninject Interception, Castle DynamicProxy and WPF window: Can't find XAML resource in DynamicProxy of window
    primarykey
    data
    text
    <p>In our real world application we defined an attribute that is used to enable logging in methods or classes (the usual AOP use case). When we apply this attribute to a WPF window class, objects of this class can't be created by Ninject. Here is a minimal example to reproduce the issue:</p> <p><strong>dummy interceptor for logging:</strong></p> <pre><code>public class MyInterceptor: IInterceptor { public void Intercept(IInvocation invocation) { Console.WriteLine("Calling {0} at {1}", invocation.Request.Method.Name, DateTime.Now); invocation.Proceed(); } } </code></pre> <p><strong>the corresponding attribute:</strong></p> <pre><code>public class MyAttribute: InterceptAttribute { public override IInterceptor CreateInterceptor(IProxyRequest request) { return new MyInterceptor(); } } </code></pre> <p><strong>the window class (completely empty, only the automatically generated empty grid is inside):</strong></p> <pre><code>[My] public partial class MainWindow: Window { public MainWindow() { InitializeComponent(); } } </code></pre> <p><strong>and finally the app startup code where the object is requested:</strong></p> <pre><code>public partial class App: Application { private void Application_Startup(object sender, StartupEventArgs e) { var kernel = new StandardKernel(new NinjectSettings() { LoadExtensions = false }, new DynamicProxyModule()); var window = kernel.Get&lt;MainWindow&gt;(); window.ShowDialog(); } } </code></pre> <p>When requesting the window via <code>kernel.Get&lt;MainWindow&gt;();</code> an <code>TargetInvocationException</code> is thrown with an inner exception telling me that <code>Castle.Proxies.MainWindowProxy</code> doesn't have a resource specified by URI <code>"/NinjectInterceptionWPF;component/mainwindow.xaml"</code> where <code>NinjectInterceptionWPF</code> is our assembly's short name.</p> <p>When we look at the automatically created <code>InitializeComponent</code> of <code>MainWindow</code> we can see that an URI is created to address the XAML code, which seems to be missing for the proxy:</p> <pre><code>System.Uri resourceLocater = new System.Uri("/NinjectInterceptionWPF;component/mainwindow.xaml", System.UriKind.Relative); #line 1 "..\..\..\MainWindow.xaml" System.Windows.Application.LoadComponent(this, resourceLocater); </code></pre> <p>I already played around a bit and tried to use an absolute URI but <code>LoadComponent</code> only accepts relative ones.</p> <p>Some internet searching shows that a lot of people use Ninject Interception and DynmaicProxy for WPF binding (INotifyPropertyChanged), so I think in general it should be possible to build a proxy of a WPF window.</p> <p>But how?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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