-1

When I'm trying into insert the data from swagger it's displaying below error.

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=Csla StackTrace: at Csla.Core.BusinessBase.VerifyAuthorizationCache() at Csla.Core.BusinessBase.CanWriteProperty(IPropertyInfo property) at Csla.Core.BusinessBase.CanWriteProperty(IPropertyInfo property, Boolean throwOnFalse) at Csla.Core.BusinessBase.SetProperty[P](PropertyInfo1 propertyInfo, P newValue, NoAccessBehavior noAccess) at Csla.Core.BusinessBase.SetProperty[P](PropertyInfo1 propertyInfo, P newValue) at LearningNet7.CSLA.BusinessLibrary.PersonEdit.set_PersonId(Int32 value) in C:\CSLA\CSLA Example\LearningNet7.CSLA.BusinessLibrary\PersonEdit.cs:line 20

I've the following code in API Controller

using Csla;
using LearningNet7.CSLA.BusinessLibrary;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace LearningNet7.CSLA.BusinessLibraryAPI.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PersonController : Csla.Server.Hosts.HttpPortalController
    {
        private readonly IDataPortal<PersonList> _personListDataPortal;
        private readonly IDataPortal<PersonEdit> _personEditDataPortal;

      public PersonController(ApplicationContext applicationContext
            , IDataPortal<PersonList> personListDataportal
            , IDataPortal<PersonEdit> personEditDataportal) : base(applicationContext)
        {
            _personListDataPortal = personListDataportal;
            _personEditDataPortal = personEditDataportal;
        }

        [HttpGet]
        [Route("GetPersons")]
        public PersonList GetPersons()
        {
            return _personListDataPortal.Fetch();
        }

        [HttpGet]
        [Route("GetPerson")]
        public PersonEdit GetPerson(int id)
        {
            return _personEditDataPortal.Fetch(id);
        }

        [HttpGet]
        [Route("Person")]
        public async Task<ActionResult> Create()
        {
            var obj = await _personEditDataPortal.CreateAsync();
            return Ok(obj);
        }



        [HttpPost]
        //[ValidateAntiForgeryToken]
        [Route("CreatePerson")]
        public async Task<ActionResult> Create(PersonEdit person)
        {
            try
            {
                var p = await _personEditDataPortal.UpdateAsync(person);
                return Ok(p);
            }
            catch
            {
                return BadRequest(500);
            }
        }
    }
}

PersonEdit (CSLA Business Object)

using Csla;
using Csla.Rules;
using Csla.Core;
using LearningNet7.CSLA.Dal.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearningNet7.CSLA.BusinessLibrary
{
    [Serializable]
    public class PersonEdit : BusinessBase<PersonEdit>
    {
        public static readonly PropertyInfo<int> PersonIdProperty = RegisterProperty<int>(nameof(PersonId));
        public int PersonId
        {
            get => GetProperty(PersonIdProperty); 
            set => SetProperty(PersonIdProperty, value); 
        }

        public static readonly PropertyInfo<string> FirstnameProperty = RegisterProperty<string>(nameof(Firstname));
        public string Firstname
        {
            get => GetProperty(FirstnameProperty); 
            set => SetProperty(FirstnameProperty, value); 
        }

        public static readonly PropertyInfo<string> LastnameProperty = RegisterProperty<string>(nameof(Lastname));
        public string Lastname
        {
            get => GetProperty(LastnameProperty); 
            set => SetProperty(LastnameProperty, value); 
        }

        public static readonly PropertyInfo<DateTime> DateOfBirthProperty = RegisterProperty<DateTime>(nameof(DateOfBirth));
        public DateTime DateOfBirth
        {
            get =>  GetProperty(DateOfBirthProperty); 
            set => SetProperty(DateOfBirthProperty, value); 
        }

        public static readonly PropertyInfo<int> AgeProperty = RegisterProperty<int>(nameof(Age));
        public int Age
        {
            get =>  GetProperty(AgeProperty); 
            set => SetProperty(AgeProperty, value); 
        }

        public static readonly PropertyInfo<string> GenderProperty = RegisterProperty<string>(nameof(Gender));
        public string Gender
        {
            get =>  GetProperty(GenderProperty); 
            set => SetProperty(GenderProperty, value); 
        }

        public static readonly PropertyInfo<string> CreatedByProperty = RegisterProperty<string>(nameof(CreatedBy));
        public string CreatedBy
        {
            get =>  GetProperty(CreatedByProperty); 
            set => SetProperty(CreatedByProperty, value); 
        }

        public static readonly PropertyInfo<DateTime> CreatedDateProperty = RegisterProperty<DateTime>(nameof(CreatedDate));
        public DateTime CreatedDate
        {
            get =>  GetProperty(CreatedDateProperty); 
            set => SetProperty(CreatedDateProperty, value); 
        }

        public static readonly PropertyInfo<string> ModifiedByProperty = RegisterProperty<string>(nameof(ModifiedBy));
        public string ModifiedBy
        {
            get
            {
                return GetProperty(ModifiedByProperty);
            }
            set
            {
                SetProperty(ModifiedByProperty, value);
            }
        }

        public static readonly PropertyInfo<DateTime> ModifiedDateProperty = RegisterProperty<DateTime>(nameof(ModifiedDate));
        public DateTime ModifiedDate
        {
            get => GetProperty(ModifiedDateProperty); 
            set => SetProperty(ModifiedDateProperty, value); 
        }

        public static readonly PropertyInfo<byte[]> lastchangedproperty = RegisterProperty<byte[]>(nameof(lastchanged));
        public byte[] lastchanged
        {
            get =>  GetProperty(lastchangedproperty); 
            set => SetProperty(lastchangedproperty, value); 
        }

        [Create]
        private void Create()
        {
            CreatedBy = "Myself";
            CreatedDate = DateTime.Now;
        }

        [Fetch]
        private void Fetch(int id, [Inject] IPersonDal dal)
        {
            var data = dal.FetchById(id);
            using (BypassPropertyChecks)
            {
                Csla.Data.DataMapper.Map(data, this);
            }
            BusinessRules.CheckRules();
        }

        [Insert]
        private void Insert([Inject] LearningNet7.CSLA.Dal.Interfaces.IPersonDal dal)
        {
            using (BypassPropertyChecks)
            {
                var data = new LearningNet7.CSLA.Dal.PersonDto
                {
                    PersonId=PersonId,
                    Firstname = Firstname,
                    Lastname = Lastname,
                    Gender = Gender,
                    Age = Age,
                    DateOfBirth = DateOfBirth
};
                var result = dal.Insert(data);

            }
        }
    }
}

Here the setproperty is returning error

enter image description here

When we are trying into insert the data from swagger it's displaying an error and it's refering to the Empty ApplicationContext enter image description here

Can anyone pinpoint what's going wrong here? Appreciated your help

I tried different samples from CSLA git hub and couldn't find a proper example for my scenario (Web API).

0 Answers0