2012-04-02 9 views
1

Ich habe eine DLL, die gut registriert mit .Net2.0 regasm, aber wenn ich versuche, mit einem .NET4 regasm registrieren, bekomme ich den Fehler "Konnte Datei nicht laden oder Assembly 'FILENAMEHERE' oder eine seiner Abhängigkeiten der Betrieb wird nicht unterstützt (Ausnahme von HRESULT:.. 0x8013515 -Code und Montage sind nachfolgendRegAsm dll .net2.0 bis .net4.0

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

Verbergen Sie keine Fehlermeldungen, HEXVALUEHERE hilft uns nicht weiter. Verwenden Sie nur die Version 4 von Regasm, wenn Sie das Projekt tatsächlich in .NET 4 umgewandelt haben. –

+0

Das Projekt kann nicht in .NET 4 geändert werden. Es muss ein .NET2 für Kompatibilitätsprobleme sein. Ich hatte den Eindruck, dass .NET rückwärtskompatibel ist. Ich habe den obigen Hex-Code angepasst. – steventnorris

+0

Warum möchten Sie es dann mit Version 4 registrieren anstatt Version 2 zu verwenden? –

Antwort

2

Einige spezielle Regeln gelten für einen COM-Server wie Ihren. Die CLR in .NET 4 unterstützt die Side-by-Side-Versionierung für die CLR, sodass ein Prozess mehr als eine Version der CLR hosten kann. Dies ist besonders wichtig für COM-Server. Es löst das CLR-Versionsinjektionsproblem, das es bisher unmöglich machte, verwaltete Shell-Erweiterungen zuverlässig zu erstellen. CLR Version 2 und früher unterstützte nur eine Version der CLR in einem Prozess. Mit dem Nebeneffekt, dass der COM-Server, der zuerst die CLR geladen hat, es unmöglich macht, später COM-Server zu laden, die eine spätere Version der CLR erfordern. Besonders schlecht, wenn der erste COM-Server die Version 1.0 oder 1.1 der CLR geladen hat.

Wenn Sie .NET 4 nicht als Ziel verwenden möchten, müssen Sie die Benutzer bitten, .NET 3.5 SP1 zu installieren, damit der Server registriert werden kann. Sie müssen außerdem eine app.exe.config-Datei für das Clientprogramm bereitstellen, um der CLR mitzuteilen, dass Sie wissen, dass Ihr COM-Server für eine frühere Version der CLR erstellt wurde und dass die Ausführung mit der CLR der Version 4 in Ordnung ist . Dies verhindert, dass die CLR Version 2 verwendet wird. Die CONFIG-Datei sollte wie folgt aussehen:

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

Sie klar voraus sein von .NET 4 für die Nutzer-Targeting, die nur 4 haben Und bedenken Sie, dass Sie könnte tatsächlich die Version 2 CLR geladen bevorzugen immer Wenn das Client-Programm vollständig nativ ist, ist dies wahrscheinlich die Version, mit der Sie Ihren Code getestet haben. Version 4 ist sehr kompatibel, aber es gibt eine Reihe von Bugfixes für Bugs, von denen Ihr Code unbeabsichtigt abhängig sein könnte.

+0

Wo finde/bearbeite ich die app.exe.config-Datei zur Verteilung an einen Benutzer-PC und wie würde ich sie an ihre RegAsm binden? Ich nehme an das ist irgendwo im "Framework" Verzeichnis? Die Benutzer würden diese DLL über RegAsm mit ihrem Terminal-Fenster auf ihrem lokalen PC registrieren. – steventnorris

+0

Sie müssen es erstellen. Notepad.exe geht gut. Es muss in dem Verzeichnis gespeichert werden, das die EXE der * client * -Anwendung enthält, wobei der Name des Client-Programms verwendet wird. Wenn also c: \ foo \ bar.exe Ihren COM-Server verwendet, müssen Sie c: \ foo \ bar.exe.config erstellen. –

+0

Was meinen Sie mit meinem COM-Server? Dies wird lokal auf einem einzelnen Benutzer-PC ausgeführt. – steventnorris