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

Topics: Troubleshooting, Writing modules
Jun 20, 2014 at 9:34 AM
Hey Guys, I have a Migrations file which attaches a Profile Record to the Orchard User [which all seems to function as expected], but I also have an Address Record [i'm hoping it's done right] which is meant to be attached to the Profile record as a 1-N.

MIGRATION:
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;
        }
    }
}
When I attempt to populate an address record, however - I get the following Error:
An exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll but was not handled in user code

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`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext 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.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
       at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass48.<InvokeActionMethodFilterAsynchronouslyRecursive>b__41()
  InnerException: 
In my AddressService:
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;
        }
    }
}
Can anyone point me in the right direction as to what could be causing this? I've spent yesterday, and today, going around in circles - thanks for any advice/help, it will be very much appreciated, cheers Ron
Jun 23, 2014 at 11:43 AM
Hi guys, I am completely lost - has anyone got any advice, whatsoever, no matter how trivial? Thanks for your time, Ron
Jun 24, 2014 at 12:35 PM
Edited Jun 24, 2014 at 1:06 PM
Hmm, could it be because the naming of AddressPart is restricted? I just renamed the Models and references and no longer get that error; I have some others to contend with now, but I feel that may've been it? Can anyone shed some light? Cheers
Scrap the above, still have the same error - i'm going mad?
Jun 24, 2014 at 11:59 PM
Huge thanks to Piotr re: http://stackoverflow.com/questions/24382794/system-reflection-targetexception-was-unhandled-by-user-code-non-static-method answer

As far as I can see the error is here:

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

should be

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

Cheers once again, mate.
Marked as answer by OrcNoob on 6/24/2014 at 3:59 PM