"Failed to load file or assembly" XXX "or one of its dependencies. The system cannot find the specified file "solution

Source: Internet
Author: User
Tags error code reflection stack trace

Today in the write reflection call failed, after debugging and analysis, the final solution, I will specify the following.

One, the phenomenon of error

Failed to load file or assembly "Dalsql" or one of its dependencies. The system could not find the specified file. Description: An unhandled exception occurred during the execution of the current WEB request. Check the stack trace for more information about the error and where the error occurred in the code. Exception Details: System.IO.FileNotFoundException: Failed to load file or assembly "Dalsql" or one of its dependencies. The system could not find the specified file. The error code is as follows:

<span style= "FONT-SIZE:14PX;" >  ///<summary>
        ///Universal Object Reflection (including caching)
        ///</summary>
        ///<param name= "ClassName" > Class name to reflect </param>
        ///<returns></returns> public
        static T CreateObject (String className)
        {
            var typeName = assemblystring + "." + ClassName;
            Determine if the object is cached, read directly from the cache if it is already cached, or directly reflect and cache
            var obj = (T) cachehelper.getcache (typeName);
            if (obj = = null)
            {
                obj = (T) assembly.load (assemblystring). CreateInstance (TypeName, true);
                Cachehelper.add (TypeName, obj, true);
            }
            return obj;
        } </span>
Two, analyze the reasons and solve

is the path or name of the DLL wrong. After all, the reason is not deep enough to reflect the reflection of the call should be: Assembly.Load ("Assembly"). CreateInstance ("namespace. Class"), then let's check if our code is like this. This is the line (T) Assembly.Load (assemblystring). CreateInstance (TypeName, true); there are no mistakes. Finally found that the DLL was mistaken, "assembly" reads the corresponding DLL under the Web-tier Bin folder, that is, reflection-loaded assembly DLLs must be in the Web-layer Bin folder, otherwise there will be an error with the title. We can set the file name of the generated assembly by right-clicking the-> property on the class library item.

Third, configuration instructions

In the code we are accessing: private static readonly string assemblystring = configurationmanager.appsettings["DAL"]; The following settings should be set in the configuration file:

<appSettings>
<add key= "DAL" value= "Dalsql"/>
</appSettings>

OK, it's done, to strengthen the understanding of reflection.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.