.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 ‘assembly’

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 use the binaries from the SQL Server System CLR Types package (SQLSysClrTypes.msi): “Unable to load DLL ‘SqlServerSpatial.dll’: This application has failed to start because …

Symptoms
After you install the SQL Server System CLR Types package (SQLSysClrTypes.msi), you receive the following error message when you try to use the binaries that this package installs:

Unable to load DLL ‘SqlServerSpatial.dll’: This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem. (Exception from HRESULT: 0×800736B1)Additionally, the following information is logged in the event log:
EventID: 32
Description: Dependent Assembly Microsoft.VC80.CRT could not be found and Last Error was The referenced assembly is not installed on your system.
EventID: 59
Description: Resolve Partial Assembly failed for Microsoft.VC80.CRT. Reference error message: The referenced assembly is not installed on your system.
EventID: 59
Description: Generate Activation Context failed for C:\WINDOWS\system32\SqlServerSpatial.dll. Reference error message: The operation completed successfully.
Resolution
This problem occurs because the C Run-Time (CRT) library is not installed. The binary files in the SQL Server System CLR Types package rely on the CRT library.
Note The CRT library is included in the Microsoft Visual C++ 2008 Redistributable Package.

A System.Resources.MissingManifestResourceException exception occurs when you try to access a localized resource

Symptoms
In a Microsoft ASP.NET application, when you try to access a localized resource, a System.Resources.MissingManifestResourceException exception may occur, and your Web browser may display an error message that is similar to the following error message:

Server Error in ‘/MyApp’ Application.
Could not find any resources appropriate for the specified culture (or the neutral culture) in the given assembly. Make sure “MyApp.strings.resources” was correctly embedded or linked into assembly “MyApp”. baseName: MyApp.strings locationInfo: <null> resource file name: MyApp.strings.resources assembly: MyApp, Version=VersionNumber, Culture=neutral, PublicKeyToken=null
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture (or the neutral culture) in the given assembly. Make sure “MyApp.strings.resources” was correctly embedded or linked into assembly “MyApp”. baseName: MyApp.strings locationInfo: <null> resource file name: MyApp.strings.resources assembly: MyApp, Version=VersionNumber, Culture=neutral, PublicKeyToken=nullNotesIn the error message, MyApp is a placeholder for the name of your ASP.NET application.In the earlier error information, VersionNumber is a placeholder for the version number of the primary assembly for your application.
Resolution
This problem occurs if you use a localized resource that exists in a satellite assembly that you created by using a .resources file that has an inappropriate file name. This problem typically occurs if you manually create a satellite assembly.
To manually create a satellite assembly, you must first run the Resource File Generator (Resgen.exe), and then you must run the Assembly Linker (Al.exe). When you run Resgen.exe, if you do not specify the file name of the output file while you convert an XML-based resource format (.resx) file to a .resources file, Resgen.exe creates a .resources file that has the same file name as the input file. If the file name of your XML-based resource format file does not start with the namespace name of your application, the file name of the .resources file will not contain this namespace name either. You may run Al.exe to create a satellite assembly that contains the localized resources that exist in the .resources file. However, when you try to access a localized resource that exists in the satellite assembly, the behavior that is mentioned in the “Symptoms” section occurs.