5

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

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

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

4

4 回答 4

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;
}
于 2014-04-08T12:43:43.247 回答
1

在这里查看我的答案。它实际上使用了 WAStorageEmulator 状态 API,而不是像 @huha自己的回答那样简单地依赖测试进程是否存在。

于 2015-01-18T22:24:28.617 回答
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);
        }
    }
}
于 2014-06-11T12:11:46.297 回答
0

在这里查看我的答案。它使用一个简洁的小 NuGet 包来帮助以编程方式启动/停止 Azure 存储模拟器:RimDev.Automation.StorageEmulator

于 2019-07-22T06:43:31.330 回答