运行在Azure上本地code [英] Running native code on Azure

查看:97
本文介绍了运行在Azure上本地code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行在Azure上一个C可执行文件。我有很多workerRoles,他们不断地检查作业队列。如果在队列中的作业,辅助角色根据存储在一个作业类的命令行参数运行C可执行的实例作为一个过程。的C可执行程序通常创建一些日志文件。我不知道如何访问这些创建的文件。背后是什么逻辑?其中,存储在创建的文件?谁能解释一下吗?我是新来的Azure和C#。

I am trying to run a C executable on Azure. I have many workerRoles and they continuously check a Job Queue. If there is a job in the queue, a worker role runs an instance of the C executable as a process according to the command line arguments stored in a job class. The C executable creates some log files normally. I do not know how to access those created files. What is the logic behind it? Where are the created files stored? Can anyone explain me? I am new to azure and C#.

另外一个问题是,所有的C-可执行需要的工作实例的读取的数据文件。我怎样才能分发所需的文件?

One other problem is that all of the working instances of the C executable need to read a data file. How can I distribute that required file?

推荐答案

首先,要认识到Windows Azure中,您的辅助角色简直是在Windows 2008 Server环境(无论是SP2或R2)内运行。当你部署应用程序,你可以部署你的C可执行程序,以及(或从Blob存储抓住它,但是这更多的是位高级)。要找出您的应用程序位于磁盘,调用 Environment.GetEnvironmentVariable(RoleRoot) - 返回的路径。你通常有你的应用程序坐在一个角色根目录下名为为approot文件夹中。你会发现你的C可执行那里。

First, realize that in Windows Azure, your worker role is simply running inside a Windows 2008 Server environment (either SP2 or R2). When you deploy your app, you would deploy your C executable as well (or grab it from blob storage, but that's a bit more advanced). To find out where your app lives on disk, call Environment.GetEnvironmentVariable("RoleRoot") - that returns a path. You'd typically have your app sitting in a folder called AppRoot under the role root. You'd find your C executable there.

接下来,你会希望您的应用程序的文件写入到你在命令行上指定的输出目录。您可以设置存储在本地的VM你的角色的属性。看看本地存储选项卡,并配置命名的本地存储区:

Next, you'll want your app to write its files to an output directory you specify on the command line. You can set up storage in your local VM with your role's properties. Look at the Local Storage tab, and configure a named local storage area:

现在,你可以得到的路径存储区域,在code,并把它作为一个命令行参数:

Now you can get the path to that storage area, in code, and pass it as a command line argument:

var outputStorage = RoleEnvironment.GetLocalResource("MyLocalStorage");
var outputFile = Path.Combine(outputStorage.RootPath, "myoutput.txt");
var cmdline = String.Format("--output {0}", outputFile);

下面是启动您Myapp.exe进程的一个例子,用命令行参数:

Here's an example of launching your myapp.exe process, with command line arguments:

var appRoot = Path.Combine(Environment.GetEnvironmentVariable("RoleRoot")
            + @"\", @"approot");

var myProcess = new Process()
{
   StartInfo = new ProcessStartInfo(Path.Combine(appRoot, @"myapp.exe"), cmdline)
   {
      CreateNoWindow = false,
      UseShellExecute = false,
      WorkingDirectory = appRoot
   }
};
myProcess.WaitForExit();

通常你会设置CreateNoWindow为真,但它更容易调试,如果你能看到的命令shell窗口。

Normally you'd set CreateNoWindow to true, but it's easier to debug if you can see the command shell window.

最后一件事:一旦程序完成创建文件,你想要么:

Last thing: Once your app is done creating the file, you'll want to either:


  • 进程并删除它(它不是一个持久的地方,以便最终它会消失)

  • 更改您的存储使用云驱动器(耐贮运)

  • 您的文件复制到BLOB(耐贮运)

在生产中,您需要添加异常处理,你可以重新路由输出和错误被捕获。但这个样本code应该足以让你开始。

In production, you'll want to add exception-handling, and you can re-route stdout and stderr to be captured. But this sample code should be enough to get you started.

OOPS - 多了一个'一件事':将当你的MyApp.exe将到您的项目,一定要到它的属性,然后设置复制到输出目录为一直拷贝 - 否则你的myapp .exe文件将不会在Windows Azure中结束了,你会想知道为什么事情不工作。

OOPS - one more 'one more thing': When adding your 'myapp.exe' to your project, be SURE to go to its Properties, and set 'Copy to Output Directory' to 'Copy Always' - otherwise your myapp.exe file won't end up in Windows Azure and you'll wonder why things don't work.

编辑:推结果一个blob - 一个简单的例子

Pushing results to a blob - a quick example

首先获得设立一个存储账户,并添加到您的角色的设置。说你把它叫做'AzureStorage - 现在把它架在code,得到一个blob容器的引用,获得该容器内的BLOB的引用,然后执行一个文件上传到BLOB:

First get set up a storage account and add to your role's Settings. Say you called it 'AzureStorage' - now set it up in code, get a reference to a blob container, get a reference to a blob within that container, and then perform a file upload to the blob:

        CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("AzureStorage");
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer outputfiles = blobClient.GetContainerReference("outputfiles");
        outputfiles.CreateIfNotExist();

        var blobname =  "myoutput.txt";
        var blob = outputfiles.GetBlobReference(blobname);
        blob.UploadFile(outputFile);

这篇关于运行在Azure上本地code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆