RegAsm DLL .net2.0到.net4.0 [英] RegAsm dll .net2.0 to .net4.0

查看:365
本文介绍了RegAsm DLL .net2.0到.net4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DLL,精细注册使用.Net2.0的regasm,而是试图与.NET4.0 regasm注册的时候,我得到的错误无法加载文件或程序集的 FILENAMEHERE 的或者它的一个依赖是不支持操作(从HRESULT异常:。0x8013515 code和汇编低于

STARTelnet.cs

  / **
*史蒂芬T.诺里斯创建时间:2012/3/27
*最后更新:史蒂芬T.诺里斯最新更新时间:2012/3/27
*
* /

使用系统;
使用MinimalisticTelnet;
使用的System.Net.Sockets;

/ **
 * @brief用于连接,读取,并以明星终端会话作出回应。
 *
 *史蒂芬T.诺里斯创建时间:2012/3/27
 * /
命名空间STARTelnet
{
    / **
     *使用类来连接,读取,并以明星终端会话作出回应。
     * /
    公共类STARConnection
    {
        私人TelnetConnection的康涅狄格州;
        私人字符串输出;
        私人字符串命令;
        私人字符串提示;

        / **
         *实例化新STARConnection。 < BR />
         *推荐登录超时是2000< BR />
         *推荐整体超时时间为500< BR />
         *抛出SocketException,请PromptException,LoginException异常
         *
         *参数[中]字符串用户名:用​​户名进行登录
         *参数[中]字符串密码:登录密码
         *参数[中] INT loginTimeout:超时毫秒登录
         *参数[中] INT overallTimeout:超时毫秒会议
         * /
        公共STARConnection(用户名字符串,字符串密码,INT loginTimeout,INT overallTimeout)
        {
            输出=;
            康恩=新TelnetConnection的(HOSTHOSTHOST,23);
            this.SetTimeout(overallTimeout);
            尝试
            {
                输出= conn.Login(用户名,密码,loginTimeout);
                如果(output.Contains(你输入了一个无效的登录名或密码))
                {
                    抛出新LoginException异常(无法登录);
                }
                this.ParsePrompt();
            }
            赶上(例外五)
            {
                如果(e.Message.Contains(登录提示))
                {
                    抛出新PromptException(登录,无法找到登录提示);
                }
                否则,如果(e.Message.Contains(口令提示))
                {
                    抛出新PromptException(密码,无法找到密码提示);
                }
                其他
                {
                    扔ê;
                }
            }
        }

        / **
         *设置超时时间以毫秒为单位的会议
         *参数[中] INT超时:超时会话
         * /
        公共无效的setTimeout(INT超时)
        {
            conn.MainTimeOutMs =超时;
            conn.TimeOutMs =超时;
        }

        / **
         *获取当前超时时间以毫秒为单位的会议
         *参数[出] INT:timout会话
         * /
        公众诠释GetTimeout()
        {
            返回conn.TimeOutMs;
        }

        / **
         *写一个命令到STAR会议
         *参数[中]字符串命令:命令写入
         * /
        公共无效写入(字符串命令)
        {
            this.command =命令;
            conn.Write(this.command);
            this.command = this.command.Replace(\ N,{}换行);
        }


        / **
         *写命令后面加上一个新行(\ n)的星空会议
         *参数[中]字符串命令:命令写入
         * /
        公共无效的WriteLine(字符串命令)
        {
            this.command =命令;
            conn.WriteLine(this.command);
            this.command + ={}换行;
        }

        / **
         *从STAR会话输出。假设给定的超时时间内没有数据表示流的末尾
         *参数[出]字符串:从STAR会话输出
         * /
        公共字符串阅读()
        {
            输出= conn.Read();
            this.ParsePrompt();
            返回输出;
        }

        / **
         *从STAR会话超时输出变仅此读。假设在没有数据
         *超时表示流的末尾。
         *参数[中] INT超时:超时这个只读
         *参数[出]字符串:从STAR会话输出
         * /
        公共字符串读(INT超时)
        {
            INT TEMP = this.GetTimeout();
            this.SetTimeout(超时);
            this.Read();
            this.SetTimeout(临时);
            返回输出;
        }

        / *
         *从输出解析提示
         * /
        私人无效ParsePrompt()
        {
            提示= output.Substring(output.LastIndexOf(\ N)+ 1);
        }

        / **
         *获取输出从上次读
         *参数[出]字符串:从去年读输出
         * /
        公共字符串GetOutput()
        {
            返回输出;
        }

        / **
         *获取进入最后的命令
         *参数[出]字符串:最后输入的命令
         * /
        公共字符串GetCommand()
        {
            返回的命令;
        }

        / **
         *获取由最后一次读取的提示
         *参数[出]字符串:最后提示
         * /
        公共字符串GetPrompt()
        {
            返回提示;
        }

        / **
         *检查连接
         *参数[出]布尔:连接状态
         * /
        公共BOOL IsConnected()
        {
            返回conn.IsConnected;
        }
    }

    / **
     *异常的登录失败
     * /
    类LoginException异常:异常
    {

        私人字符串罪犯=;
        公共LoginException异常():碱(){}
        公共LoginException异常(字符串消息):基地(消息){}

        / **
         *创建例外
         * @参数字符串罪犯:元素造成的异常
         * @参数字符串消息:异常消息
         * /
        公共LoginException异常(字符串罪犯,字符串消息)
            :碱(消息)
        {
            this.offender =罪犯;
        }

        / **
         *为字符串的方法获取异常字符串
         *参数[出]字符串:字符串重新$ P $异常psentation
         * /
        公共重写字符串的ToString()
        {
            如果(罪犯==)
            {
                返回this.GetType()+:+ this.Message +\ N+ this.StackTrace;
            }
            其他
            {
                返回不正确的登录:+罪犯+ - + this.Message +\ N+ this.StackTrace;
            }
        }
    }

    / **
     *异常失败之星提示
     * /
    类PromptException:异常
    {

        私人字符串提示=;
        公共PromptException():碱(){}
        公共PromptException(字符串消息):基地(消息){}

        / **
         *创建exeption
         * @参数字符串提示:提示引起异常
         * @参数字符串消息:异常消息
         * /
        公共PromptException(字符串提示,字符串消息)
            :碱(消息)
        {
            this.prompt =提示;
        }

        / **
         *为字符串的方法获取异常字符串
         *参数[出]字符串:字符串重新$ P $异常psentation
         * /
        公共重写字符串的ToString()
        {
            如果(提示==)
            {
                返回this.GetType()+:+ this.Message +\ N+ this.StackTrace;
            }
            其他
            {
                返回提示失败:+提示+ - + this.Message +\ N+ this.StackTrace;
            }
        }

    }
}
 

的AssemblyInfo.cs

 使用的System.Reflection;
使用System.Runtime.CompilerServices;
使用了System.Runtime.InteropServices;

//有关程序集的常规信息通过下列控制
//属性集。更改这些属性值可修改信息
//与程序集关联。
[总成:AssemblyTitle(STARTelnet)
[总成:AssemblyDescription()]
[总成:AssemblyConfiguration()]
[总成:AssemblyCompany(「本公司」)
[总成:AssemblyProduct(STARTelnet)
[总成:AssemblyCopyright(版权所有©公司2012)]
[总成:AssemblyTrademark()]
[总成:AssemblyCulture()]

//将ComVisible设置为false使此程序集中的类型不可见
//对COM组件。如果你需要从访问类型在本次大会
// COM,设置标记有ComVisible特性的属性为true的类型。
[总成:标记有ComVisible特性(真)

//下列GUID用于类型库的ID,如果该项目向COM公开,
[总成:GUID(d7ae512d-c840-4ebc-8057-73a10f286225)]

//版本信息组件由以下四个值:
//
//主版本
//次版本
//内部版本号
//修订
//
//您可以指定所有这些值,也可以默认生成和修订号
//通过使用'*',如下所示:
// [总成:的AssemblyVersion(1.0 *)]
[总成:的AssemblyVersion(1.0.0.0)]
[总成:AssemblyFileVersion(1.0.0.0)]
 

解决方案

有些特殊规则适用于像你这样的COM服务器。在.NET CLR的4支持过程并排侧版本的CLR,允许一个进程主办CLR的多个版本。这是COM服务器尤为重要,它解决了CLR版本注射的问题,previously,不可能可靠地创建托管外壳扩展。 CLR版本2及更早版本支持CLR的一个版本的一个过程。随着副作用,无论COM服务器首次加载CLR,就不可能加载COM服务器后,需要在CLR的更高版本。尤其是坏的,如果说第一个COM服务器装载的1.0或1.1版本的CLR。

如果你不想针对.NET 4,那么你要问用户安装.NET 3.5 SP1,这样服务器可以注册。你还必须提供一个app.exe.config文件,客户端程序来告诉您知道您的COM服务器是专为在CLR的早期版本,它是好的,与第4版CLR运行的CLR 。其中prevents从正在使用的版本2 CLR。 .config文件应该是这样的:

 <结构>
    <启动useLegacyV2RuntimeActivati​​onPolicy =真正的>
        < supportedRuntime版本=V4/>
        < supportedRuntime版本=V2.0.50727/>
    < /启动>
< /结构>
 

显然,你会通过靶向.NET 4只具有4并千万记住,你实际上可能preFER得到2 CLR加载如果客户端程序是完全原生版本的用户会提前,这可能该版本中,你已经与测试您的code。第4版是高度兼容,但它确实有一些bug修复的错误,你的code无意间可能依赖。

I have a dll that registers fine using .Net2.0's regasm, but when attempting to register with a .NET4.0 regasm, I get the error "Could not load file or assembly 'FILENAMEHERE' or one of it's dependencies. Operation is not supported. (Exception from HRESULT: 0x8013515 Code and assembly are below

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")]

解决方案

Somewhat special rules apply to a COM server like yours. The CLR in .NET 4 supports in-process side-by-side versioning for the CLR, allowing a process to host more than one version of the CLR. This is particularly important for COM servers, it solves the CLR version injection problem that previously made it impossible to reliably create managed shell extensions. CLR version 2 and earlier only supported one version of the CLR in a process. With the side effect that whatever COM server loaded the CLR first, would make it impossible to load COM servers later that require a later version of the CLR. Particularly bad if that first COM server loaded the 1.0 or 1.1 version of the CLR.

If you don't want to target .NET 4 then you'll have to ask users to install .NET 3.5 SP1 so the server can be registered. You also have to provide a app.exe.config file for the client program to tell the CLR that you are aware that your COM server was built for an earlier version of the CLR and that it is okay to run it with the version 4 CLR. Which prevents the version 2 CLR from being used. The .config file should look like this:

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

Clearly you'll be ahead by targeting .NET 4 for users that only have 4. And do keep in mind that you actually might prefer getting the version 2 CLR loaded if the client program is entirely native, that's probably the version you've tested your code with. Version 4 is highly compatible but it does have a number of bug fixes for bugs that your code unintentionally might depend on.

这篇关于RegAsm DLL .net2.0到.net4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆