2014-04-08 5 views
5

Da ich den neuen Windows Azure SDK installiert 2.3 Ich habe eine Warnung von csrun starten:.von Windows Azure Storage Emulator V3.0 von Code

„DevStore Interaktion durch CSRun hat depricated worden Verwendung WAStorageEmulator.exe statt. "

So gibt es zwei Fragen: 1) Wie starten Sie den neuen Speicher-Emulator korrekt aus dem Code? 2) Wie kann man aus dem Code ermitteln, wenn der Speicheremulator bereits läuft?

Antwort

5

Ich fand die Lösung selbst. Hier ist mein C# -Code. Der alte Code, der für SDK 2.2 verwendet wird, ist auskommentiert.

public static void StartStorageEmulator() 
{ 
    //var count = Process.GetProcessesByName("DSServiceLDB").Length; 
    //if (count == 0) 
    // ExecuteCSRun("/devstore:start"); 
    var count = Process.GetProcessesByName("WAStorageEmulator").Length; 
    if (count == 0) 
     ExecuteWAStorageEmulator("start"); 
} 

/* 
private static void ExecuteCSRun(string argument) 
{ 
    var start = new ProcessStartInfo 
    { 
     Arguments = argument, 
     FileName = @"c:\Program Files\Microsoft SDKs\Windows Azure\Emulator\csrun.exe" 
    }; 
var exitCode = ExecuteProcess(start); 
Assert.AreEqual(exitCode, 0, "Error {0} executing {1} {2}", exitCode, start.FileName, start.Arguments); 
} 
*/ 

private static void ExecuteWAStorageEmulator(string argument) 
{ 
    var start = new ProcessStartInfo 
    { 
     Arguments = argument, 
     FileName = @"c:\Program Files (x86)\Microsoft SDKs\Windows Azure\Storage Emulator\WAStorageEmulator.exe" 
    }; 
    var exitCode = ExecuteProcess(start); 
    Assert.AreEqual(exitCode, 0, "Error {0} executing {1} {2}", exitCode, start.FileName, start.Arguments); 
} 

private static int ExecuteProcess(ProcessStartInfo start) 
{ 
    int exitCode; 
    using (var proc = new Process { StartInfo = start }) 
    { 
     proc.Start(); 
     proc.WaitForExit(); 
     exitCode = proc.ExitCode; 
    } 
    return exitCode; 
} 
1
using System; 
using System.Diagnostics; 
using System.Linq; 
using System.Threading; 
using Xunit; 

namespace UnitTests.Persistence 
{ 
    public class AzureStorageEmulatorManagerV3 
    { 
     private const string ProcessName = "WAStorageEmulator"; 

     public static void StartStorageEmulator() 
     { 
      var count = Process.GetProcessesByName(ProcessName).Length; 
      if (count == 0) 
       ExecuteWAStorageEmulator("start"); 
     } 

     public static void StopStorageEmulator() 
     { 
      Process process = GetWAstorageEmulatorProcess(); 
      if (process != null) 
      { 
       process.Kill(); 
      } 
     } 

     private static void ExecuteWAStorageEmulator(string argument) 
     { 
      var start = new ProcessStartInfo 
      { 
       Arguments = argument, 
       FileName = @"c:\Program Files (x86)\Microsoft SDKs\Windows Azure\Storage Emulator\WAStorageEmulator.exe" 
      }; 
      var exitCode = ExecuteProcess(start); 
      if (exitCode != 0) 
      { 
       string message = string.Format(
        "Error {0} executing {1} {2}", 
        exitCode, 
        start.FileName, 
        start.Arguments); 
       throw new InvalidOperationException(message); 
      } 
     } 

     private static int ExecuteProcess(ProcessStartInfo start) 
     { 
      int exitCode; 
      using (var proc = new Process { StartInfo = start }) 
      { 
       proc.Start(); 
       proc.WaitForExit(); 
       exitCode = proc.ExitCode; 
      } 
      return exitCode; 
     } 

     public static Process GetWAstorageEmulatorProcess() 
     { 
      return Process.GetProcessesByName(ProcessName).FirstOrDefault(); 
     } 

     [Fact] 
     public void StartingAndThenStoppingWAStorageEmulatorGoesOk() 
     { 
      // Arrange Start 
      AzureStorageEmulatorManagerV3.StartStorageEmulator(); 

      // Act 
      Thread.Sleep(2000); 
      Process WAStorageEmulatorProcess = GetWAstorageEmulatorProcess(); 

      // Assert 
      Assert.NotNull(WAStorageEmulatorProcess); 
      Assert.True(WAStorageEmulatorProcess.Responding); 

      // Arrange Stop 
      AzureStorageEmulatorManagerV3.StopStorageEmulator(); 
      Thread.Sleep(2000); 
      // Act 
      WAStorageEmulatorProcess = GetWAstorageEmulatorProcess(); 

      // Assert 
      Assert.Null(WAStorageEmulatorProcess); 
     } 
    } 
} 
1

Siehe meine Antwort here. Es verwendet tatsächlich die WAStorageEmulator-Status-API, anstatt sich einfach darauf zu verlassen, ob der Prozess wie in @ huhas own answer existiert oder nicht.

+0

Ich bin froh zu sehen, dass dieses Thema sich entwickelt. Deine Lösung ist wirklich schlauer als meine. Vielleicht ist es etwas langsamer beim Testen, wenn der Emulator läuft, aber das sollte nicht wirklich wichtig sein. – huha

Verwandte Themen