Can't resolve a service in the code behind

Topics: Feature requests
Feb 26, 2014 at 2:37 PM
Version 3.8.1
I have a service that is registered in my app.xmal.cs. When I try and access this services in the code behind of a Catel.Windows.Controls.UserControl, I get the error "the type 'x service' is not registered, not even in an external container". I don't know why I am getting this error.

App.xaml.cs - OnStartup method
            ServiceLocator.Default.RegisterType<IGraphicsService, GraphicsService>();
UserControl behind code
        private static readonly IGraphicsService GraphicsService = 
            ServiceLocator.Default.ResolveType<IGraphicsService>();

        public DockView()
        {
            this.InitializeComponent();

            GraphicsService.SetObject(this.Graph, Enums.GraphObject.Main);
            GraphicsService.SetObject(this.IntermediateGraph, Enums.GraphObject.Intermediate);
        }
Thanks in advance
Bob
Coordinator
Feb 26, 2014 at 3:04 PM
Make sure that you call the RegisterType before the MainWindow is created, which is probably the case here.
Feb 26, 2014 at 3:42 PM
Geert
I made sure that the MainWindow is created last, but still get the same error message.
Bob
Coordinator
Feb 26, 2014 at 3:50 PM
Please create a repro at the official issue tracker:

http://www.catelproject.com/support/issue-tracker/
Feb 26, 2014 at 4:16 PM
Geert,
It appears that even though the error message is showing up in the IDE, the service is being found correctly. Weird, seems to be an issue in the IDE not the in the code. Compiles and runs fine.
Bob
Coordinator
Feb 26, 2014 at 5:41 PM
That's because the designer is actually executing the code (and the services are not registered at design time). What you can do is initialize in the static constructor and before that, check Catel.Environment.IsInDesignMode.
Marked as answer by paco1863 on 2/26/2014 at 11:27 AM
Feb 26, 2014 at 6:28 PM
Thanks