简体   繁体   中英

System.Reflection.TargetException was unhandled by user code - Non-static method requires a target?

thanks for viewing: I'm receiving the following error in one of my Service Classes and I'm completely clueless to exactly what it entails / and why I'm receiving it [I've just started Programming]:

System.Reflection.TargetException was unhandled by user code HResult=-2146232829 Message=Non-static method requires a target.
Source=mscorlib StackTrace: at System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index) at Orchard.ContentManagement.InfosetHelper.Store[TPart,TRecord,TProperty](TPart contentPart, Expression 1 targetExpression, TProperty value) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard\\ContentManagement\\InfosetHelper.cs:line 122 at Orchard.ContentManagement.ContentPart 1.Store[TProperty](Expression 1 targetExpression, TProperty value) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard\\ContentManagement\\ContentPart.cs:line 130 at EA.Profile.Models.AddressPart.set_Street1(String value) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard.Web\\Modules\\EA.Profile\\Models\\AddressPart.cs:line 14 at EA.Profile.Services.AddressService.CreateAddress(AddressPartRecord addressdetails) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard.Web\\Modules\\EA.Profile\\Services\\AddressService.cs:line 27 at EA.Profile.Controllers.AddressController.Create(AddressPartRecord addressdetails) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard.Web\\Modules\\EA.Profile\\Controllers\\AddressController.cs:line 50 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 1 targetExpression, TProperty value) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard\\ContentManagement\\ContentPart.cs:line 130 at EA.Profile.Models.AddressPart.set_Street1(String value) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard.Web\\Modules\\EA.Profile\\Models\\AddressPart.cs:line 14 at EA.Profile.Services.AddressService.CreateAddress(AddressPartRecord addressdetails) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard.Web\\Modules\\EA.Profile\\Services\\AddressService.cs:line 27 at EA.Profile.Controllers.AddressController.Create(AddressPartRecord addressdetails) in e:\\Projects\\_Code\\_ORCHARD\\orchard - Copy\\src\\Orchard.Web\\Modules\\EA.Profile\\Controllers\\AddressController.cs:line 50 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(Controlle rContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 2.CallEndDelegate(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End() at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag) at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.b__3f() at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass48.b__41() InnerException:

The Service in question, is as follows:

using EA.Profile.Models;
using Orchard;
using Orchard.ContentManagement;

namespace EA.Profile.Services
{
    public interface IAddressService : IDependency {
        AddressPart CreateAddress(AddressPartRecord addressdetails);
    }

    public class AddressService : IAddressService {
        private readonly IOrchardServices _orchardServices;

        public AddressService(IOrchardServices orchardServices) {
            _orchardServices = orchardServices;
        }

        public AddressPart CreateAddress(AddressPartRecord addressdetails) {
            var address = _orchardServices.ContentManager.New("Address");
            //var profilePart = address.As<ProfilePart>();
            var addressPart = address.As<AddressPart>();

            addressPart.Street1 = addressdetails.Street1; <error>
            addressPart.Street2 = addressdetails.Street2;
            addressPart.City = addressdetails.City;
            addressPart.State = addressdetails.State;
            addressPart.PostCode = addressdetails.PostCode;
            addressPart.Country = addressdetails.Country;

            _orchardServices.ContentManager.Create(address);

            return addressPart;
        }
    }
}

...if it makes a difference, I'm trying to learn Orchard CMS [So far the Orchard Forum hasn't been exactly fruitful with support], with this code coming from a custom module I've created with all appropriate Handlers / Drivers / Models / Migrations etc. Initially I though there may've been something wrong with my Migrations.cs:

namespace EA.Profile
{
    public class Migrations : DataMigrationImpl {

        private readonly IOrchardServices _orchardServices;

        public Migrations(IOrchardServices orchardServices)
        {
            _orchardServices = orchardServices;
        }
        public int Create() {

            //Profile
            SchemaBuilder.CreateTable("ProfilePartRecord",
                table => table
                    .ContentPartRecord()
                    //ProfilePartRecord_id [Auto Key]
                    .Column<string>("FirstName")
                    .Column<string>("LastName")
                    //System
                    .Column<DateTime>("CreatedAt")
                );

            //Address
            SchemaBuilder.CreateTable("AddressPartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<int>("ProfilePartRecord_id") //FK
                    .Column<string>("Street1")
                    .Column<string>("Street2")
                    .Column<string>("City")
                    .Column<string>("State")
                    .Column<short>("PostCode")
                    .Column<string>("Country")
                );

            //Address Type
            SchemaBuilder.CreateTable("AddressTypePartRecord",
                table => table
                    .ContentPartRecord()
                    .Column<int>("AddressPartRecord_id") //FK
                    .Column<string>("Type")
                );

            ContentDefinitionManager.AlterPartDefinition("ProfilePart",
                builder => builder.Attachable());

            ContentDefinitionManager.AlterTypeDefinition("Profile", t => t
                .WithPart(typeof(ProfilePart).Name)
                .WithPart("UserPart")
                );

            ContentDefinitionManager.AlterTypeDefinition("User", t => t
                .WithPart("ProfilePart")
                );


            // ORCHARD Settings: I believe the positioning of these settings is critical. For I had them at the start of this
            // method, and although the checkboxes in admin were indeed changed, they were not saved; I had to uncheck [save], recheck [save]
            // inorder for the settings to trigger/have any effect.
            var registrationSettings = _orchardServices.WorkContext.CurrentSite.As<RegistrationSettingsPart>();
            registrationSettings.UsersCanRegister = true;
            registrationSettings.UsersAreModerated = true;

            return 1;
        }

        public int UpdateFrom1()
        {
            ContentDefinitionManager.AlterPartDefinition("AddressPart",
                builder => builder.Attachable());

            ContentDefinitionManager.AlterTypeDefinition("Address", t => t
                .WithPart(typeof(AddressPart).Name)
                .WithPart("ProfilePart")
                );

            ContentDefinitionManager.AlterTypeDefinition("Profile", t => t
                .WithPart("AddressPart")
                );

            return 2;
        }
    }
} 

...But upon inspecting the DB, all tables are there and functioning.

Where the debugger breaks is at (Here):

public static void Store<TPart, TRecord, TProperty>(this TPart contentPart,
    Expression<Func<TRecord, TProperty>> targetExpression,
    TProperty value)
    where TPart : ContentPart<TRecord> {

    var propertyInfo = ReflectionHelper<TRecord>.GetPropertyInfo(targetExpression);
    var name = propertyInfo.Name;
    var versioned = typeof(ContentPartVersionRecord).IsAssignableFrom(typeof(TRecord));
    propertyInfo.SetValue(contentPart.Record, value, null); //<Here>
    contentPart.Store(name, value, versioned);
}

which for some reason as per:

Non-static method requires a target C#

contentPart.Record [expecting to be AddressPart] is not of AddressPart, and I cannot work out why that is for the life of me? Am I completely blind [inexperienced] to see the obvious, or can someone hopefully put me out of my misery. Thanks for you're support, Ron

//AddressPartRecord.cs

using System.ComponentModel.DataAnnotations;
using Orchard.ContentManagement.Records;

namespace EA.Profile.Models
{
    public class AddressPartRecord : ContentPartRecord
    {
        public virtual int ProfilePartRecord_id { get; set; }
        [Required(ErrorMessage = "Street number and address required.")]
        public virtual string Street1 { get; set; }
        public virtual string Street2 { get; set; }
        [Required(ErrorMessage = "City required.")]
        public virtual string City { get; set; }
        [Required(ErrorMessage = "State required.")]
        public virtual string State { get; set; }
        [Required(ErrorMessage = "Post Code required.")]
        public virtual short PostCode { get; set; }
        [Required(ErrorMessage = "Country required.")]
        public virtual string Country { get; set; }
    }
}

//AddressPart.cs

using Orchard.ContentManagement;

namespace EA.Profile.Models
{
    public class AddressPart : ContentPart<AddressPart> {

        public int ProfilePartRecord_id
        {
            get { return Retrieve(x => x.ProfilePartRecord_id); }
            set { Store(x => x.ProfilePartRecord_id, value); }
        }
        public string Street1
        {
            get { return Retrieve(x => x.Street1); }
            set { Store(x => x.Street1, value); }
        }

        public string Street2
        {
            get { return Retrieve(x => x.Street2); }
            set { Store(x => x.Street2, value); }
        }

        public string City
        {
            get { return Retrieve(x => x.City); }
            set { Store(x => x.City, value); }
        }

        public string State
        {
            get { return Retrieve(x => x.State); }
            set { Store(x => x.State, value); }
        }

        public short PostCode
        {
            get { return Retrieve(x => x.PostCode); }
            set { Store(x => x.PostCode, value); }
        }

        public string Country
        {
            get { return Retrieve(x => x.Country); }
            set { Store(x => x.Country, value); }
        }

        public ProfilePart Profile
        {
            get { return this.As<ProfilePart>(); }
        }
    }
}

As far as I can see the error is here:

public class AddressPart : ContentPart<AddressPart> { ... }

should be

public class AddressPart : ContentPart<AddressPartRecord> { ... }

Backing record is a separate class, which needs to contain all properties of the table you created in migrations. Seems like you missed that one thing. There is a good example in the docs .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM