How to start Windows Azure Storage Emulator V3.0 from code
Asked Answered
G

4

5

Since I installed the new Windows Azure SDK 2.3 I got a warning from csrun:

"DevStore interaction through CSRun has been depricated. Use WAStorageEmulator.exe instead."

So there are two questions: 1) How to start the new storage emulator correctly from code? 2) How to determine from code if the storage emulator is already running?

Geldens answered 8/4, 2014 at 12:36 Comment(0)
G
5

I found the solution myself. Here is my C# code. The old code used for SDK 2.2 is commented out.

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;
}
Geldens answered 8/4, 2014 at 12:43 Comment(0)
A
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);
        }
    }
}
Aversion answered 11/6, 2014 at 12:11 Comment(0)
P
1

See my answer here. It actually uses the WAStorageEmulator status API instead of simply relying on testing whether or not the process exists as in @huha's own answer.

Polad answered 18/1, 2015 at 22:24 Comment(1)
I'm glad to see that this topic evolves. Your solution is really smarter than mine. Maybe it´s a little slower in testing if the emulator is running, but that shouldn´t really matter.Geldens
F
0

See my answer here. It uses a neat little NuGet package to assist with starting/stopping the Azure Storage Emulator programmatically: RimDev.Automation.StorageEmulator.

Flameproof answered 22/7, 2019 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.