.NET Questions and Solutions

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘target of an invocation’

PRB: “System.Reflection.TargetInvocationException” Error Message When You Call the MethodInfo.Invoke Method

Symptoms
When you call the MethodInfo.Invoke method, you may receive the following error message:

An unhandled exception of type ‘System.Reflection.TargetInvocationException’ occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an invocation.
You may also receive an additional error message that is similar to the following error message:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.IO.FileLoadException: ‘ClassLibrary1′ is not a valid file.
File name: “ClassLibrary1″
at ClassLibrary2.Class1.GetString()
Resolution
You receive the System.Reflection.TargetInvocationException error because the common language runtime calls the MethodInfo.Invoke method by using reflection.
When you load an assembly by using the Assembly.LoadFrom method, the common language runtime places the loaded assembly in the LoadFrom context of your application. Any probes for the dependencies of the assembly first probe the current application directory. If this probe fails, the common language runtime then probes the LoadFrom context of your application.
You may load an assembly that has a simply-named dependency that has the same file name as a file in the current application directory. When you try to invoke a method in the loaded assembly by using the MethodInfo.Invoke method, and the invoked method uses the dependency, the common language runtime first probes the current directory path. When the common language runtime finds a file that has the same file name as the dependency, the probe stops. However, if this file does not have the same assembly identity as the dependency, the assembly bind fails, and the common language runtime generates a System.IO.FileLoadException error that is passed to the System.Reflection.TargetInvocationException error. Therefore, you may notice the behavior that is mentioned in the “Symptoms” section.

Error message when you upgrade to Microsoft Dynamics CRM 4.0: “Exception has been thrown by the target of an invocation”

Symptoms
When you upgrade to Microsoft Dynamics CRM 4.0, the upgrade process cannot be completed. Additionally, you receive the following error message in the CRM40setup.log file:

Time|Info| Updating precision and scale of decimal attributes
Time|Error| System.Exception: Action
Microsoft.Crm.Setup.Server.InstallDatabaseAction failed. —>
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.Data.SqlClient.SqlException: The statistics ‘hind_1090102924_1A_2A_4A_9A_15A’ is dependent on column ‘Quantity’.
ALTER TABLE ALTER COLUMN Quantity failed because one or more objects access this column.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Microsoft.Crm.CrmDbConnection.InternalExecuteNonQuery(IDbCommand command)
at Microsoft.Crm.CrmDbConnection.ExecuteNonQuery(IDbCommand command, Boolean impersonate)
at Microsoft.Crm.CrmDbConnection.ExecuteNonQuery(IDbCommand command)
at Microsoft.Crm.Setup.DiffBuilder.UpdatePrecisionAndScaleOnDecimalAttributes()
— End of inner exception stack trace —
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at Microsoft.Crm.Setup.Database.DllMethodAction.Execute()
at Microsoft.Crm.Setup.Database.DatabaseInstaller.ExecuteReleases(ReleaseCollection releases, Int32 currentProgress)
at Microsoft.Crm.Setup.Database.DatabaseInstaller.installInternal()
at Microsoft.Crm.Setup.Database.DatabaseInstaller.Install(Int32 languageCode, String configurationFilePath, Boolean upgradeDatabase)
at Microsoft.Crm.Setup.Server.InstallDatabaseAction.Do(IDictionary parameters)
at Microsoft.Crm.Setup.Common.Action.ExecuteAction(Action action, IDictionary parameters, Boolean undo)
— End of inner exception stack trace —, Error, RetryCancel, Option1Note By default, the CRM40setup.log file is in the following folder:
C:\Documents and Settings\upgrading user\Application Data\Microsoft\MSCRM\logs
This problem occurs if you have custom statistics in the OrgName_MSCRM database.
Resolution
This problem occurs because the upgrade process cannot handle custom statistics in the OrgName_MSCRM database.

Error message when you try to save a record in Microsoft Dynamics CRM 4.0: “Object reference not set to an instance of an object”

Symptoms
When you try to save a record in Microsoft Dynamics CRM 4.0, you receive the following error message:

Object reference not set to an instance of an object.If you have enabled platform tracing, you receive an error message that resembles the following:

Application:
MSCRM Error Report:
Error: Exception has been thrown by the target of an invocation.
Error Number: 0×80040265
Error Message: Object reference not set to an instance of an object.
Error Details: Object reference not set to an instance of an object.
Platform.Sdk:
Web Service Plug-in failed in SdkMessageProcessingStepId: {F6D4EBBB-0075-407D-8208-26575BFA39AF}; EntityName: new_businessrules; Stage: 20; MessageName: Create; AssemblyName:
Microsoft.Crm.ObjectModel.MultiCurrencyPlugin, Microsoft.Crm.ObjectModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35; ClassName: Microsoft.Crm.ObjectModel.MultiCurrencyPlugin; Exception:
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Crm.ObjectModel.CalculateBaseMoney.ProcessCalculatedColumns(DynamicEntity entity, BusinessEntity entityPreImage, EntityMetadata entityMetadata, String exchangeRateFieldName, Boolean isCreateOperation, ArrayList unchangedAttributes)
Resolution
This problem occurs because a money attribute is created for the entity. When you do this, Microsoft Dynamics CRM 4.0 creates an exchangerate attribute to work together with the money attribute. The money attribute is published as part of the entity. However, if you delete the money attribute, the exchangerate attribute is not removed. Therefore, when you try to run a Microsoft Dynamics CRM plug-in, the plug-in can find the exchangerate attribute. However, the plug-in cannot find the money attribute.