【问题标题】:RegAsm dll .net2.0 to .net4.0RegAsm dll .net2.0 到 .net4.0
【发布时间】:2012-04-02 22:56:05
【问题描述】:

我有一个使用 .Net2.0 的 regasm 注册良好的 dll,但是当尝试使用 .NET4.0 regasm 注册时,我收到错误“无法加载文件或程序集'FILENAMEHERE ' 或其依赖项之一。不支持操作。(来自 HRESULT 的异常:0x8013515 代码和汇编如下

STARTelnet.cs

/**
*Steven T. Norris     Created: 3/27/2012
*Last Updated By: Steven T. Norris     Last Updated On: 3/27/2012
*
*/

using System;
using MinimalisticTelnet;
using System.Net.Sockets;

/**
 * @brief Used to connect to, read, and respond to a STAR terminal session.
 * 
 * Steven T. Norris     Created: 3/27/2012
 */
namespace STARTelnet
{
    /**
     * Class used to connect to, read, and respond to a STAR terminal session. 
     */
    public class STARConnection
    {
        private TelnetConnection conn;
        private string output;
        private string command;
        private string prompt;

        /**
         * Instantiates new STARConnection. <br/>
         * Recommended login timeout is 2000. <br/>
         * Recommended overall timeout is 500. <br/>
         * Throws SocketException, PromptException, LoginException
         * 
         * @param [in] string username:Username for login
         * @param [in] string password:Password for login
         * @param [in] int loginTimeout:timeout milliseconds for login
         * @param [in] int overallTimeout:timeout milliseconds for session
         */
        public STARConnection(string username, string password, int loginTimeout, int overallTimeout)
        {
            output = "";
            conn = new TelnetConnection("HOSTHOSTHOST", 23);
            this.SetTimeout(overallTimeout);
            try
            {
                output = conn.Login(username, password, loginTimeout);
                if(output.Contains("You entered an invalid login name or password"))
                {
                    throw new LoginException("Failed to login");
                }
                this.ParsePrompt();
            }
            catch(Exception e)
            {
                if(e.Message.Contains("login prompt"))
                {
                    throw new PromptException("Login", "Could not find login prompt");
                }
                else if(e.Message.Contains("password prompt"))
                {
                    throw new PromptException("Password", "Could not find password prompt");
                }
                else
                {
                    throw e;
                }
            }
        }

        /**
         * Sets the timeout for the session in milliseconds
         * @param [in] int timeout:timeout for session
         */
        public void SetTimeout(int timeout)
        {
            conn.MainTimeOutMs = timeout;
            conn.TimeOutMs = timeout;
        }

        /**
         * Gets the current timeout for the session in milliseconds
         * @param [out] int:timout for session
         */
        public int GetTimeout()
        {
            return conn.TimeOutMs;
        }

        /**
         * Writes a command to the STAR session
         * @param [in] string command:command to write
         */
        public void Write(string command)
        {
            this.command = command;
            conn.Write(this.command);
            this.command = this.command.Replace("\n", "{newLine}");
        }


        /**
         * Writes a command followed by a new line (\n) to the STAR session
         * @param [in] string command:command to write
         */
        public void WriteLine(string command)
        {
            this.command = command;
            conn.WriteLine(this.command);
            this.command += "{newLine}";
        }

        /**
         * Reads output from STAR session. Assumes no data within given timeout denotes end of stream
         * @param [out] string:output from STAR session
         */
        public string Read()
        {
            output = conn.Read();
            this.ParsePrompt();
            return output;
        }

        /**
         * Reads output from STAR session with timeout changed for only this read. Assumes no data within
         * timeout denotes end of stream.
         * @param [in] int timeout:timeout for this read only
         * @param [out] string:output from STAR session
         */
        public string Read(int timeout)
        {
            int temp = this.GetTimeout();
            this.SetTimeout(timeout);
            this.Read();
            this.SetTimeout(temp);
            return output;
        }

        /*
         * Parse prompt from output
         */
        private void ParsePrompt()
        {
            prompt = output.Substring(output.LastIndexOf("\n") + 1);
        }

        /**
         * Gets output from last read
         * @param [out] string:output from last read
         */
        public string GetOutput()
        {
            return output;
        }

        /**
         * Gets last command entered
         * @param [out] string:last command entered
         */
        public string GetCommand()
        {
            return command;
        }

        /**
         * Gets prompt from last read
         * @param [out] string:last prompt
         */
        public string GetPrompt()
        {
            return prompt;
        }

        /**
         * Checks for connection
         * @param [out] bool:connection status
         */
        public bool IsConnected()
        {
            return conn.IsConnected;
        }
    }

    /**
     * Exception for failed logins
     */
    class LoginException: Exception
    {

        private string offender = "";
        public LoginException() : base() { }
        public LoginException(string message) : base(message) { }

        /**
         * Creates exception
         * @param string offender:element causing exception
         * @param string message:exception message
         */
        public LoginException(string offender, string message)
            : base(message)
        {
            this.offender = offender;
        }

        /**
         * To String method for getting exception string
         * @param [out] string:string representation of exception
         */
        public override string ToString()
        {
            if(offender == "")
            {
                return this.GetType() + ": "+this.Message+"\n"+this.StackTrace;
            }
            else
            {
                return "Incorrect login: " + offender + "--" + this.Message + "\n" + this.StackTrace;
            }
        }
    }

    /**
     * Exception for failed STAR prompts
     */
    class PromptException: Exception
    {

        private string prompt = "";
        public PromptException() : base(){ }
        public PromptException(string message) : base(message){ }

        /**
         * Creates exeption
         * @param string prompt:prompt causing exception
         * @param string message:exception message
         */
        public PromptException(string prompt, string message)
            : base(message)
        {
            this.prompt = prompt;
        }

        /**
         * To String method for getting exception string
         * @param [out] string:string representation of exception
         */
        public override string ToString()
        {
            if(prompt == "")
            {
                return this.GetType() + ": " + this.Message + "\n" + this.StackTrace;
            }
            else
            {
                return "Prompt failed: " + prompt + "--" + this.Message + "\n" + this.StackTrace;
            }
        }

    }
}

AssemblyInfo.cs

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("STARTelnet")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("COMPANY")]
[assembly: AssemblyProduct("STARTelnet")]
[assembly: AssemblyCopyright("Copyright © COMPANY 2012")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("d7ae512d-c840-4ebc-8057-73a10f286225")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

【问题讨论】:

  • 不要混淆错误信息,HEXVALUEHERE 不能帮助我们帮助您。仅当您实际将项目更改为面向 .NET 4 时才使用版本 4 Regasm。
  • 无法将项目更改为 .NET 4。出于兼容性问题,它必须是 .NET2.0。我的印象是 .NET 是向后兼容的。我已经调整了上面的十六进制代码。
  • 那为什么要注册到版本 4 Regasm 而不是使用版本 2?
  • @SoMoS 系统中有些人只有.NET 4.0。有些只有 .NET2.0。

标签: .net dll .net-4.0 .net-2.0 regasm


【解决方案1】:

有些特殊规则适用于像您这样的 COM 服务器。 .NET 4 中的 CLR 支持 CLR 的进程内并行版本控制,允许进程托管多个版本的 CLR。这对于 COM 服务器尤为重要,它解决了以前无法可靠地创建托管 shell 扩展的 CLR 版本注入问题。 CLR 版本 2 和更早版本仅支持进程中的一个 CLR 版本。副作用是,无论 COM 服务器首先加载 CLR,都无法在以后加载需要更高版本 CLR 的 COM 服务器。如果第一个 COM 服务器加载了 1.0 或 1.1 版本的 CLR,则尤其糟糕。

如果您不想以 .NET 4 为目标,则必须要求用户安装 .NET 3.5 SP1 以便注册服务器。您还必须为客户端程序提供一个 app.exe.config 文件,以告诉 CLR 您知道您的 COM 服务器是为早期版本的 CLR 构建的,并且可以使用版本 4 CLR 运行它.这会阻止使用第 2 版 CLR。 .config 文件应如下所示:

<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true"> 
        <supportedRuntime version="v4"/>
        <supportedRuntime version="v2.0.50727"/>
    </startup>
</configuration>

很明显,您可以通过针对只有 4 个用户的 .NET 4 来领先。请记住,如果客户端程序完全是本机程序,您实际上可能更喜欢加载第 2 版 CLR,这可能是您的版本已经测试了你的代码。版本 4 高度兼容,但它确实为您的代码无意中可能依赖的错误修复了许多错误。

【讨论】:

  • 在哪里可以找到/编辑 app.exe.config 文件以分发给用户 PC,以及如何将其与他们的 RegAsm 绑定?我假设这是在“框架”目录中的某个地方?用户将使用本地 PC 上的终端窗口通过 RegAsm 注册此 dll。
  • 你必须创建它。 Notepad.exe 会很好。它必须使用客户端程序的名称存储在包含 client 应用程序 EXE 的目录中。所以如果 c:\foo\bar.exe 使用你的 COM 服务器,那么你必须创建 c:\foo\bar.exe.config
  • 使用我的 COM 服务器是什么意思?这一切都在单个用户 PC 上本地运行。
  • “服务器”在这种情况下并不意味着“服务器机器”。您编写了一个“进程内 COM 服务器”,这就是它在 COM 术语中的名称。与“COM 客户端”相反,使用您的 DLL 的程序。
  • 这是高度特定于 COM 服务器的。一个常规的 .NET 2 DLL 可以在 .NET 4 中正常工作,无需任何配置。但这要求主程序也必须是 .NET 程序。毫无疑问,将使用哪个版本的 CLR,EXE 程序会加载它。如果您的主程序也是 .NET 程序,正如您所暗示的,那么您根本不应该创建 COM 服务器。这是不必要且低效的。
猜你喜欢
  • 1970-01-01
  • 2011-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
相关资源
最近更新 更多