eXpand icon indicating copy to clipboard operation
eXpand copied to clipboard

After installation Reactive.rest i am start getting such error: Unable to cast object of type 'Castle.Proxies.ApplicationUserProxy' to type 'Xpand.XAF.Modules.Reactive.Rest.ICredentialBearer'.

Open beneton2003 opened this issue 2 years ago • 12 comments

𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 Hi! Maybe somebody can help me with some strange issue. This is the full log

Log:

info: DevExpress.AspNetCore.Reporting.Logger[0]
      AspNetCoreThreadingTimerStoragesCleaner.StartCore

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:57602
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:57603
info: Hangfire.BackgroundJobServer[0]
      Starting Hangfire Server using job storage: 'SQL Server: 88.99.226.170@HRStation'
info: Hangfire.BackgroundJobServer[0]
      Using the following options for SQL Server job storage: Queue poll interval: 00:00:00.
info: Hangfire.BackgroundJobServer[0]
      Using the following options for Hangfire Server:
          Worker count: 20
          Listening queues: 'default'
          Shutdown timeout: 00:00:15
          Schedule polling interval: 00:00:15
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: D:\apps_sharp\HRStationXaf\HRStation\HRStation.Blazor.Server
info: Hangfire.Server.BackgroundServerProcess[0]
      Server nurubkov:16404:a29c975d successfully announced in 1117,2346 ms
info: Hangfire.Server.BackgroundServerProcess[0]
      Server nurubkov:16404:a29c975d is starting the registered dispatchers: ServerWatchdog, ServerJobCancellationWatcher, ExpirationManager, CountersAggregator, SqlServerHeartbeatProcess, Worker, DelayedJobScheduler, RecurringJobScheduler...
info: Hangfire.Server.BackgroundServerProcess[0]
      Server nurubkov:16404:a29c975d all the dispatchers started
warn: Microsoft.EntityFrameworkCore.Query[20504]
      Compiling a query which loads related collections for more than one collection navigation, either via 'Include' or through projection, but no 'QuerySplittingBehavior' has been configured. By default, Entity Framework will use 'QuerySplittingBehavior.SingleQuery', which can potentially result in slow query performance. See https://go.microsoft.com/fwlink/?linkid=2134277 for more information. To identify the query that's triggering this warning call 'ConfigureWarnings(w => w.Throw(RelationalEventId.MultipleCollectionIncludeWarning))'.
fail: DevExpress.ExpressApp.Blazor.Services.ExceptionService[0]
      Handled exception occurs
      System.InvalidCastException: Unable to cast object of type 'Castle.Proxies.ApplicationUserProxy' to type 'Xpand.XAF.Modules.Reactive.Rest.ICredentialBearer'.
         at Xpand.XAF.Modules.Reactive.Rest.RestService.<>c__DisplayClass12_0.<WhenNonPersistentObjectSpace>b__1(ValueTuple`2 t)
         at System.Reactive.Linq.ObservableImpl.SelectMany`2.ObservableSelector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/SelectMany.cs:line 869

================================================================================ The error occurred:


	Type:       InvalidCastException
	Message:    Unable to cast object of type 'Castle.Proxies.ApplicationUserProxy' to type 'Xpand.XAF.Modules.Reactive.Rest.ICredentialBearer'.
	Data:       0 entries
	Stack trace:

   at Xpand.XAF.Modules.Reactive.Rest.RestService.<>c__DisplayClass12_0.<WhenNonPersistentObjectSpace>b__1(ValueTuple`2 t)
   at System.Reactive.Linq.ObservableImpl.SelectMany`2.ObservableSelector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/SelectMany.cs:line 869
	InnerException is null

beneton2003 avatar Nov 15 '23 06:11 beneton2003

I have custom implemented ApplicationUser class, maybe this is the problem?:


namespace HRStation.Module.BusinessObjects.Security;

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net.Mail;
using System.Reactive.Linq;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.Security;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;


[DefaultProperty(nameof(UserName))]
public class ApplicationUser : PermissionPolicyUser, ISecurityUserWithLoginInfo
{
    public ApplicationUser() : base()
    {
        UserLogins = new ObservableCollection<ApplicationUserLoginInfo>();
    }

    [Browsable(false)]
    [Aggregated]
    public virtual IList<ApplicationUserLoginInfo> UserLogins { get; set; }

    IEnumerable<ISecurityUserLoginInfo> IOAuthSecurityUser.UserLogins => UserLogins.OfType<ISecurityUserLoginInfo>();

    ISecurityUserLoginInfo ISecurityUserWithLoginInfo.CreateUserLoginInfo(string loginProviderName, string providerUserKey)
    {
        ApplicationUserLoginInfo result = ((IObjectSpaceLink)this).ObjectSpace.CreateObject<ApplicationUserLoginInfo>();
        result.LoginProviderName = loginProviderName;
        result.ProviderUserKey = providerUserKey;
        result.User = this;
        return result;
    }



}

beneton2003 avatar Nov 15 '23 06:11 beneton2003

I have also nonpersistent object in my project:

namespace HRStation.Module.BusinessObjects.Worklogs;

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.DC;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using HRStation.Module.BusinessObjects.Issues;
using HRStation.Module.BusinessObjects.Persons;
using HRStation.Module.BusinessObjects.Security;
using HRStation.Module.BusinessObjects.Sprints;

[DomainComponent]
[RuleCriteria("WorklogNPO_EndOn", DefaultContexts.Save, "EndOn > StartOn", "End On shoulde greater than Start On")]
public class WorklogActionNPO : NonPersistentObjectImpl, IObjectSpaceLink, INotifyPropertyChanged
{
    #region Private fields
    private Person person;
    private Project project;
    private ProjectAction projectAction;
    private Issue issue;
    private Sprint sprint;
    private DateTime startOn;
    private int durationHours;
    private int durationMinutes;
    private string description;
    //private IObjectSpace objectSpace;
    #endregion

    public override void OnCreated()
    {
        ApplicationUser currentUser = ObjectSpace.GetObjectByKey<ApplicationUser>(SecuritySystem.CurrentUserId);
        Person = ObjectSpace.FirstOrDefault<Person>(p => p.ApplicationUser == currentUser);
        Sprint = ObjectSpace.FirstOrDefault<SprintIssue>(s => s.Person == Person)?.Sprint;
        base.OnCreated();
    }

    [RuleRequiredField]
    public Person Person
    {
        get { return person; }
        set { SetPropertyValue(ref person, value); }

    }

    [RuleRequiredField]
    public Project Project
    {
        get { return project; }
        set 
        {
            ProjectAction = null;
            Issue = null;
            SetPropertyValue(ref project, value); 
        }

    }

    [RuleRequiredField]
    [DataSourceProperty("Project.ProjectActions")]
    public ProjectAction ProjectAction {
        get
        {
            return projectAction;
        }
        set
        {
            Issue = value?.Issue;
            SetPropertyValue(ref projectAction, value);
        }

    }

    public Issue Issue
    {
        get { return issue; }
        set { SetPropertyValue(ref issue, value); }

    }

    [RuleRequiredField]
    public Sprint Sprint
    {
        get { return sprint; }
        set { SetPropertyValue(ref sprint, value); }

    }
    public DateTime StartOn
    {
        get { return startOn; }
        set { SetPropertyValue(ref startOn, value); }

    }
    public DateTime EndOn { get { return StartOn.AddHours(DurationHours).AddMinutes(DurationMinutes); } }

    [RuleValueComparison("WorklogNPO_DurationHoursMax", DefaultContexts.Save, ValueComparisonType.LessThanOrEqual, 24)]
    [RuleValueComparison("WorklogNPO_DurationHoursMin", DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 0)]
    public int DurationHours
    {
        get { return durationHours; }
        set { SetPropertyValue(ref durationHours, value); }

    }

    [RuleValueComparison("WorklogNPO_DurationMinutesMax", DefaultContexts.Save, ValueComparisonType.LessThanOrEqual, 59)]
    [RuleValueComparison("WorklogNPO_DurationMinutesMin", DefaultContexts.Save, ValueComparisonType.GreaterThanOrEqual, 0)]
    public int DurationMinutes
    {
        get { return durationMinutes; }
        set { SetPropertyValue(ref durationMinutes, value); }

    }

    public string Description
    {
        get { return description; }
        set { SetPropertyValue(ref description, value); }

    }

}


beneton2003 avatar Nov 15 '23 06:11 beneton2003

Can you please post a sample.

apobekiaris avatar Nov 15 '23 09:11 apobekiaris

Reference i

https://github.com/beneton2003/HRStationXaf - i added you to my project, so you can see the whole solution.

beneton2003 avatar Nov 15 '23 11:11 beneton2003

thnks I will test the case, I also interested to know if you plan to work with the REST module or you face problems cause you install it together with the all the packages

apobekiaris avatar Nov 15 '23 11:11 apobekiaris

I want to use REST feature, maybe i installed something wrong. Sorry i am not very experienced. I will be very happy if you give me some advice how to fix it, i also have a question about authentification, how to use bearer token for the requests? Maybe you have some example of the working with some public api?

beneton2003 avatar Nov 15 '23 11:11 beneton2003

I want to get recipient accounts from this api: https://docs.wise.com/api-docs/api-reference/recipient

beneton2003 avatar Nov 15 '23 12:11 beneton2003

thnks for the clarification, now I see the problem without to explore your solution

Castle.Proxies.ApplicationUserProxy

meaning you are in EF and the package uses XPO. Note the package is tested for various cases in https://github.com/eXpandFramework/Reactive.XAF/tree/master/src/Tests/Reactive.Rest. Consider exploring them to get how the package works if you plan to use XPO instead

apobekiaris avatar Nov 15 '23 14:11 apobekiaris

thnks for the clarification, now I see the problem without to explore your solution

Castle.Proxies.ApplicationUserProxy

meaning you are in EF and the package uses XPO. Note the package is tested for various cases in https://github.com/eXpandFramework/Reactive.XAF/tree/master/src/Tests/Reactive.Rest. Consider exploring them to get how the package works if you plan to use XPO instead

I am using both xpo and EF. I have Hangfire module installed and used XPO, maybe i just can add this module to that part which using XPO?

But when i tried to include this module in the part which uses xpo, its still getting this error... i will explore more

beneton2003 avatar Nov 15 '23 15:11 beneton2003

I realise that Reactive.rest somehow uses the Security system (like PermissionPolicyUser), but i am using my security system in EF and maybe this is the reason....

beneton2003 avatar Nov 15 '23 16:11 beneton2003

Not exactly

Unable to cast object of type 'Castle.Proxies.ApplicationUserProxy' to type 'Xpand.XAF.Modules.Reactive.Rest.ICredentialBearer'.

it speaks out, meaning u imlpement ICredentialBearer in your ApplicationUser which is an EF Entity

apobekiaris avatar Nov 15 '23 16:11 apobekiaris

the package may be able to work even with EF (I never try it) given that ICrendentialBearer is not an EF entity, or they maybe other problems in regards to EF, I really not sure as this was designed with XPO

apobekiaris avatar Nov 15 '23 16:11 apobekiaris

Closing issue for age. Feel free to reopen it at any time.

.Thank you for your contribution.

expand avatar Mar 09 '24 21:03 expand