0

I need to set up a template form for my .net core xaf project. As described here

So I am referencing a framework 4.8 project containing my template form. With the following code in my .net6 program.cs

try
{
    winApplication.CreateCustomTemplate += delegate (object sender, CreateCustomTemplateEventArgs e)
    {
        // fails here 
        bool isRibbon = ((IModelOptionsWin)e.Application.Model.Options).FormStyle == RibbonFormStyle.Ribbon;
        if (isRibbon && e.Context == TemplateContext.View)
        {
            e.Template = new DetailRibbonForm2(); // the form is in the framework project
        }
    };

    winApplication.Setup();
    winApplication.Start();
}
catch (Exception e)
{
    MessageBox.Show($"Error. inner: {e.InnerException} e:{e.ToString()}");

    winApplication.StopSplash();
    winApplication.HandleException(e);
}

The error stack is

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=MyApp.Win
  StackTrace:
   at MyApp.Win.Program.<>c.<Main>b__0_0(Object sender, CreateCustomTemplateEventArgs e) in D:\dev\MyApp\MyApp.Win\Program.cs:line 93
   at DevExpress.ExpressApp.XafApplication.RaiseCreateCustomTemplate(CreateCustomTemplateEventArgs args)
   at DevExpress.ExpressApp.Win.WinApplication.PreCreateCustomTemplate(TemplateContext context)
   at DevExpress.ExpressApp.Win.WinApplication.InitializeFrameTemplateCache()
   at DevExpress.ExpressApp.Win.WinApplication.SetupCore(String applicationName, IList`1 objectSpaceProviders, ApplicationModulesManager modulesManager, ISecurityStrategyBase security)
   at DevExpress.ExpressApp.XafApplication.Setup(String applicationName, IList`1 objectSpaceProviders, ApplicationModulesManager modulesManager, ISecurityStrategyBase security)
   at DevExpress.ExpressApp.XafApplication.Setup(String applicationName, IList`1 objectSpaceProviders, String[] moduleAssemblies, ISecurityStrategyBase security)
   at DevExpress.ExpressApp.XafApplication.Setup(String applicationName, String connectionString, String[] moduleAssemblies, ISecurityStrategyBase security)
   at DevExpress.ExpressApp.XafApplication.Setup()
   at MyApp.Win.Program.Main() in D:\dev\MyApp\MyApp.Win\Program.cs:line 101

  This exception was originally thrown at this call stack:
    MyApp.Win.Program.Main.AnonymousMethod__0_0(object, DevExpress.ExpressApp.CreateCustomTemplateEventArgs) in Program.cs
    DevExpress.ExpressApp.XafApplication.RaiseCreateCustomTemplate(DevExpress.ExpressApp.CreateCustomTemplateEventArgs)
    DevExpress.ExpressApp.Win.WinApplication.PreCreateCustomTemplate(DevExpress.ExpressApp.TemplateContext)
    DevExpress.ExpressApp.Win.WinApplication.InitializeFrameTemplateCache()
    DevExpress.ExpressApp.Win.WinApplication.SetupCore(string, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider>, DevExpress.ExpressApp.ApplicationModulesManager, DevExpress.ExpressApp.Security.ISecurityStrategyBase)
    DevExpress.ExpressApp.XafApplication.Setup(string, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider>, DevExpress.ExpressApp.ApplicationModulesManager, DevExpress.ExpressApp.Security.ISecurityStrategyBase)
    DevExpress.ExpressApp.XafApplication.Setup(string, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider>, string[], DevExpress.ExpressApp.Security.ISecurityStrategyBase)
    DevExpress.ExpressApp.XafApplication.Setup(string, string, string[], DevExpress.ExpressApp.Security.ISecurityStrategyBase)
    DevExpress.ExpressApp.XafApplication.Setup()
    MyApp.Win.Program.Main() in Program.cs

[Update]

Application is not null

Application.Model is null

From working code in a Framework example I thought I should be safe copying the framework code for winApplication.CreateCustomTemplate into my xaf .net core application.

However the null reference exception indicates that is different in .net core

[Update] I converted the framework example to .net6 and it works as it should. Now to find out why my project fails

In the working project winApplication.Setup() does not cause the CreateCustomTemplate event to fire. The good call stack causing the event to fire is

CustomizeRibbonTemplateExample.Win.dll!CustomizeRibbonTemplateExample.Win.Program.Main.AnonymousMethod__0_0(object sender, DevExpress.ExpressApp.CreateCustomTemplateEventArgs e) Line 38   C#
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.RaiseCreateCustomTemplate(DevExpress.ExpressApp.CreateCustomTemplateEventArgs args)    Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.OnCreateCustomTemplate(string name)    Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.CreateTemplate(string templateContextName) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.Frame.CreateTemplate()    Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinWindow.WinWindow(DevExpress.ExpressApp.XafApplication application, DevExpress.ExpressApp.TemplateContext context, System.Collections.Generic.ICollection<DevExpress.ExpressApp.Controller> controllers, bool isMain, bool activateControllersImmediately)  Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.CreateWindowCore(DevExpress.ExpressApp.TemplateContext context, System.Collections.Generic.ICollection<DevExpress.ExpressApp.Controller> controllers, bool isMain, bool activateControllersImmediately)    Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.CreateWindow(DevExpress.ExpressApp.TemplateContext context, System.Collections.Generic.ICollection<DevExpress.ExpressApp.Controller> controllers, bool createAllControllers, bool isMain, DevExpress.ExpressApp.View view) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinShowViewStrategyBase.CreateWindow(DevExpress.ExpressApp.ShowViewParameters parameters, DevExpress.ExpressApp.ShowViewSource showViewSource, bool isMain)   Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinShowViewStrategyBase.CreateExplorerWindow(DevExpress.ExpressApp.ShowViewParameters parameters) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinShowViewStrategyBase.ShowStartupWindowCore()   Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.MdiShowViewStrategy.ShowStartupWindowCore()   Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinShowViewStrategyBase.ShowStartupWindow()   Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.ShowStartupWindow()    Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.DoStartupLogic(DevExpress.XtraSplashScreen.IOverlaySplashScreenHandle overlayFormHandle)   Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.Start()    Unknown
CustomizeRibbonTemplateExample.Win.dll!CustomizeRibbonTemplateExample.Win.Program.Main() Line 46    C#

In the bad project winApplication.Setup does cause the event to fire. The bad call stack is

MyApp.Win.dll!MyApp.Win.Program.Main.AnonymousMethod__0_0(object sender, DevExpress.ExpressApp.CreateCustomTemplateEventArgs e) Line 92 C#
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.RaiseCreateCustomTemplate(DevExpress.ExpressApp.CreateCustomTemplateEventArgs args)    Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.PreCreateCustomTemplate(DevExpress.ExpressApp.TemplateContext context) Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.InitializeFrameTemplateCache() Unknown
DevExpress.ExpressApp.Win.v21.2.dll!DevExpress.ExpressApp.Win.WinApplication.SetupCore(string applicationName, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider> objectSpaceProviders, DevExpress.ExpressApp.ApplicationModulesManager modulesManager, DevExpress.ExpressApp.Security.ISecurityStrategyBase security)    Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.Setup(string applicationName, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider> objectSpaceProviders, DevExpress.ExpressApp.ApplicationModulesManager modulesManager, DevExpress.ExpressApp.Security.ISecurityStrategyBase security)    Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.Setup(string applicationName, System.Collections.Generic.IList<DevExpress.ExpressApp.IObjectSpaceProvider> objectSpaceProviders, string[] moduleAssemblies, DevExpress.ExpressApp.Security.ISecurityStrategyBase security) Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.Setup(string applicationName, string connectionString, string[] moduleAssemblies, DevExpress.ExpressApp.Security.ISecurityStrategyBase security)   Unknown
DevExpress.ExpressApp.v21.2.dll!DevExpress.ExpressApp.XafApplication.Setup()    Unknown
MyApp.Win.dll!MyApp.Win.Program.Main() Line 101 C#

So it appears that in the bad program winApplication.Setup is causing the event to fire whereas in the good program winApplication.Start is causing the event to fire.

Now I need to try stepping into winApplication.Setup but I seem to be missing XafApplication.cs in the pdb files. I have asked about it here.

[Update] I think the difference may be here

Initialize Frame Template Cache

It turns out that in the good call stack the following is true

if (RunSetupInNewThread && (SplashScreen == null || SplashScreen is DXSplashScreen))

DXSplashScreen

Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • 1
    So which is line 93? Have you debugged through the code to find out what's null? – Jon Skeet Mar 15 '22 at 07:01
  • Thanks Jon. I updated the question to show that Application.Model is null. – Kirsten Mar 15 '22 at 07:10
  • 1
    Right - I suggest you change your whole question to not refer to NullReferenceException then, as otherwise it's likely to be closed as a duplicate of https://stackoverflow.com/questions/4660142. Instead, explain how you expect the model to have been set up, and what you've done to diagnose that. – Jon Skeet Mar 15 '22 at 07:31
  • Thanks I had a shot at it. Will try again tomorrow, hopefully with a clearer head. – Kirsten Mar 15 '22 at 08:44
  • 1
    I wouldn't assume that it's a different in .NET Core. That sample has all kinds of code that I suspect sets up the model (the `ISupportStoreSettings.SetSettings` implementation) - did you copy *that* code as well? Fundamentally I'd start off by going through the working sample making sure you understand every single line of code - then you're much more likely to be able to understand which parts you need to include in your own app. – Jon Skeet Mar 15 '22 at 08:48
  • Thank you. Yes starting with the working sample made all the difference! – Kirsten Mar 15 '22 at 21:18

2 Answers2

1

It's a duplicate of What is a NullReferenceException, and how do I fix it? and can be debugged easily as NullReferenceException occurs in your custom code (MyApp.Win.Program.<>c.b__0_0(Object sender, CreateCustomTemplateEventArgs e) in D:\dev\MyApp\MyApp.Win\Program.cs:line 93).

The correct solution is to follow the best practices from the DevExpress documentation for this particular scenario at Create a Custom Ribbon Template and add the following check e.Application.Model != null. This is NOT specific to .NET 5, 6 or .NET Framework.

e.Application.Model != null

Dennis Garavsky
  • 538
  • 3
  • 18
  • Thanks Dennis. I had copied the code from https://supportcenter.devexpress.com/ticket/details/e216/how-to-create-a-custom-winforms-ribbon-template which did not have the null check in it. – Kirsten Mar 17 '22 at 09:14
-1

I was able to get winApplication.Setup to follow the happy path by deleting my SplashScreen.cs.

Kirsten
  • 15,730
  • 41
  • 179
  • 318