.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 for February, 2011

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.

Operator new does not throw a bad_alloc exception on failure in Visual C++

Symptoms
Operator new does not throw a bad_alloc exception when it fails. It simply returns a null pointer.
Resolution
Operator new does call the new handler function after it fails to procure the requested block of memory, but before it returns the null pointer. An application could install a new handler to throw a bad_alloc exception as follows:

#include <new>#include <new.h>int my_new_handler(size_t) {throw std::bad_alloc();return 0;}int main () {_PNH _old_new_handler;_old_new_handler = _set_new_handler(my_new_handler);/* … application processing … */_set_new_handler(_old_new_handler);return 0;} To call new handler when malloc fails to obtain the requested block of memory, use the _set_new_mode function.
To install the new handler before your global objects are initialized, create a class that sets the new handler in its constructor and installs the old new handler in its destructor. Create a global object of that type and use the init_seg pragma to force this global object to be initialized before any of your global objects. The example below demonstrates this. It also demonstrates the use of _set_new_mode to cause a failed malloc call to generate an exception. Note that to do this, the code below must reside in its own source file. You cannot change the initializations segment more than once per translation unit (source file) with the pragma init_seg.

#include <new>#include <new.h>#pragma init_seg(lib)int my_new_handler(size_t) {throw std::bad_alloc();return 0;}struct my_new_handler_obj{_PNH _old_new_handler;int _old_new_mode;_tag_g_new_handler_obj() {_old_new_mode = _set_new_mode(1);_old_new_handler = _set_new_handler(my_new_handler);}~_tag_g_new_handler_obj() {_set_new_handler(_old_new_handler);_set_new_mode(_old_new_mode);}} _g_new_handler_obj; Operator new, as implemented by Visual C++ 5.0, ignores the function exception specification. So new(std::nothrow) still generates an exception if your new handler is installed to throw an exception as the examples above demonstrate. To change this behavior, override operator new as follows:

void *__cdecl operator new(size_t cb, const std::nothrow_t&) throw(){char *p;try {p = new char[cb];}catch (std::bad_alloc) {p = 0;}return p;}

Microsoft Reader Error Message: Unhandled Exception 0xC0000005: AV

Symptoms
When you start Microsoft Reader on a desktop or a laptop computer, youmay receive the following error message:

Unhandled exception 0xC0000005: AV
Resolution
This behavior can occur if both of the following conditions are true:Less than 300 megabytes (MB) of space is available on yoursystem hard disk (the hard disk on which Microsoft Windowsis installed).Less than 300 MB of space is available on the hard disk onwhich the Microsoft Reader software is installed.

ManagementEventWatcher.Start() may throw an Invalid Parameter Exception

Symptoms
Source: Microsoft Support
Resolution
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION.THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Internet Explorer Uses Proxy Server for Local IP Address Even if the “Bypass Proxy Server for Local Addresses” Option Is Turned On

Symptoms
When you connect to a Web server using the Internet Protocol (IP) address or Fully Qualified Domain Name (FQDN) on the local network, Microsoft Internet Explorer or Windows Internet Explorer connects through an assigned proxy server even if the Bypass proxy server for local addresses option is turned on.
However, if you connect to a Web server using the host name (for example, http://webserver) instead of the IP address (for example, http://10.0.0.1) or FQDN (for example, http://webserver.domainname.com), the proxy server is bypassed and Internet Explorer connects directly to the server.
Resolution
By default, only host names are checked when the Bypass proxy server for local addresses option is turned on.

FIX: An EXCEPTION_STACK_OVERFLOW exception may occur when you try to run a Transact-SQL query in SQL Server 2005

Symptoms
This article describes the following about this hotfix release: The issues that are fixed by this hotfix packageThe prerequisites for applying the hotfix packageWhether you must restart the computer after you apply the hotfix packageWhether the hotfix package is replaced by any other hotfix packageWhether you must make any registry changes after you apply the hotfix packageThe files that are contained in the hotfix package
Resolution
When you try to run a Transact-SQL query in Microsoft SQL Server 2005, an EXCEPTION_STACK_OVERFLOW exception may occur. When this problem occurs, the SQL Server service may stop unexpectedly.
Note This problem may only occur in SQL Server 2005 x64 editions.