如何从代码启动Windows Azure存储模拟器V3.0

时间:2014-04-08 12:36:52

标签: c# azure azure-storage azure-storage-emulator

自从我安装了新的Windows Azure SDK 2.3后,我收到了来自csrun的警告:

“通过CSRun进行的DevStore交互已被删除。请改用WAStorageEmulator.exe。”

所以有两个问题: 1)如何从代码中正确启动新的存储模拟器? 2)如何从代码中确定存储模拟器是否已在运行?

4 个答案:

答案 0 :(得分:5)

我自己找到了解决方案。这是我的C#代码。用于SDK 2.2的旧代码已注释掉。

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 :(得分: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);
        }
    }
}

答案 2 :(得分:1)

请参阅我的回答here。它实际上使用WAStorageEmulator状态API,而不是简单地依赖于测试该进程是否存在,如@ huha的own answer

答案 3 :(得分:0)

请参阅我的答案here。它使用一个简洁的NuGet程序包来辅助以编程方式启动/停止Azure存储模拟器:RimDev.Automation.StorageEmulator