Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infolist Dataportal Issue #4411

Open
Greg90277 opened this issue Dec 21, 2024 · 5 comments
Open

Infolist Dataportal Issue #4411

Greg90277 opened this issue Dec 21, 2024 · 5 comments

Comments

@Greg90277
Copy link

I'm using CSLA 8.

The following works great fetching an object:

    public static AppUser GetAppUser(ApplicationContext appContext, Int64 appUserKey  )
    {
        if (!CanGetObject(appContext))
        {
            throw new System.Security.SecurityException("You are not authorized to view a(n) App User");
        }
        DataPortal< AppUser > dataPortal = (DataPortal< AppUser >)appContext.GetRequiredService<IDataPortal< AppUser >>();
        AppUser bo = dataPortal.Fetch(new AppUserKeyCriteria(appUserKey));
        return bo;           
    }

The following fails with the Exception:
Message=Object of type 'Csla.Rules.AddObjectAuthorizationRulesContext' cannot be converted to type 'Csla.ApplicationContext'.

    public static AppUserInfoList GetAppUserInfoList(ApplicationContext appContext)
    {
        if (!CanGetObject(appContext))
        {
            throw new System.Security.SecurityException("You are not authorized to view a(n) App User Info List");
        }
        DataPortal< AppUserInfoList > dataPortal = (DataPortal< AppUserInfoList  >)appContext.GetRequiredService<IDataPortal<AppUserInfoList >>();
        AppUserInfoList bo = dataPortal.Fetch();
        return bo;
    }

Any thoughts?

@StefanOssendorf
Copy link
Contributor

Could you provide the stack trace of the exception?

Side note: why are you casting to dataportal? That would break if we decide to change the implementation. Using the interface is recommended.

@Greg90277
Copy link
Author

Hi Stefan Thanks for the quick reply!

The stack trace isn't showing anything useful:

[External Code]	

[Exception] AppScaffold.Library.SQLite.dll!AppScaffold.Library.SQLite.AppUserInfoList.GetAppUserInfoList(Csla.ApplicationContext appContext) Line 265 C#
[Exception] AppScaffold.WPF.dll!AppScaffold.WPF.ViewModels.UserManagementViewModel.GetAppUserInfoList() Line 404 C#
[Exception] AppScaffold.WPF.dll!AppScaffold.WPF.ViewModels.UserManagementViewModel.UserManagementViewModel() Line 72 C#
[Exception] AppScaffold.WPF.dll!AppScaffold.WPF.Factories.ViewModelFactory.GetUserManagementViewModel(bool getSingleton) Line 66 C#
[Exception] AppScaffold.WPF.dll!AppScaffold.WPF.ViewModels.ApplicationShellViewModel.InitializeNavigation() Line 522 C#
[Exception] AppScaffold.WPF.dll!AppScaffold.WPF.ViewModels.ApplicationShellViewModel.NotifyPropertyChanged(string propertyName) Line 108 C#
[Exception] AppScaffold.WPF.dll!AppScaffold.WPF.ViewModels.ApplicationShellViewModel.IsLoggedIn.set(bool value) Line 321 C#
[Exception] AppScaffold.WPF.dll!AppScaffold.WPF.ViewModels.ApplicationShellViewModel.LoggedIn(AppScaffold.WPF.Prism.Events.CpeLoggedIn.Payload payload) Line 585 C#
[External Code]
[Exception] AppScaffold.WPF.dll!AppScaffold.WPF.ViewModels.LoginControlViewModel.AttemptLoginAsync() Line 385 C#
[External Code]
[Waiting on Async Operation, double-click or press enter to view Async Call Stacks]
AppScaffold.WPF.dll!AppScaffold.WPF.Views.LoginControlView.btnLogin_Click(object sender, System.Windows.RoutedEventArgs e) Line 182 C#
[External Code]

Thanks for your recommendation on using the interface - I don't know how I would do that as at some point I need a concrete instance of the proper dataportal don't I? I am trying to preserve the classic methodology of CSLA encapsulating everything in each business object, hence the approach I am trying here. It's strange that it works fine for a Switchable object but not for the infolist. I am going to rty an erditable list next to see if that works.

@Greg90277
Copy link
Author

Hi Stefan.

I found out what was causing the exception - though it is a mystery as to why.

I had this method in my business object:

    public static void AddObjectAuthorizationRules(ApplicationContext appContext)
    {
        // Per-type business rules - code generated
        // Example: BusinessRules.AddRule(GetType(businessobject), New IsInRole(AuthorizationActions.CreateObject,"Admin"))

        // Call custom Per-type business rules
        AddCustomObjectAuthorizationRules(appContext);
    }

This is the method that causes the exception. What is VERY strange is that I never call it in my code, so it must be called behind-the-scenes by CSLA maybe?

Also very strange is that it does not appear in the call-stack at all for the exception, yet commenting it out cured the exception:
Message=Object of type 'Csla.Rules.AddObjectAuthorizationRulesContext' cannot be converted to type 'Csla.ApplicationContext'.

Any thoughts?

@StefanOssendorf
Copy link
Contributor

Hi Stefan.

I found out what was causing the exception - though it is a mystery as to why.

I had this method in my business object:

    public static void AddObjectAuthorizationRules(ApplicationContext appContext)
    {
        // Per-type business rules - code generated
        // Example: BusinessRules.AddRule(GetType(businessobject), New IsInRole(AuthorizationActions.CreateObject,"Admin"))

        // Call custom Per-type business rules
        AddCustomObjectAuthorizationRules(appContext);
    }

This is the method that causes the exception. What is VERY strange is that I never call it in my code, so it must be called behind-the-scenes by CSLA maybe?

Also very strange is that it does not appear in the call-stack at all for the exception, yet commenting it out cured the exception: Message=Object of type 'Csla.Rules.AddObjectAuthorizationRulesContext' cannot be converted to type 'Csla.ApplicationContext'.

Any thoughts?

You have to get rid of the ApplicationContext parameter. I don't think it's supported at that stage.

@rockfordlhotka
Copy link
Member

That's true - you can't use or access a DI service instance from a static method.

Well, unless you are

  1. Running in a single process/appdomain
  2. Using a remote data portal
  3. Have set up a static app-wide singleton field/property to store the ApplicationContext
  4. Don't pass that ApplicationContext as a parameter, but rather use it from the static singleton (like App.ApplicationContext)

This only works in something like WinForms or WPF, and only if you have a remote data portal app server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants