简体   繁体   English

任何人都可以向我指出控制台应用程序访问azure存储的示例

[英]Can anyone point me to an example where a console application accesses azure storage

I am trying to access Azure Storage from a console app like this: 我试图从控制台应用程序访问Azure存储,如下所示:

CloudStorageAccount storageAccount = CloudStorageAccount.FromConfigurationSetting("myConnectionString");  

where the connection string is something like: 连接字符串的位置如下:

DefaultEndpointsProtocol=https;AccountName=XXX;AccountKey=XXX DefaultEndpointsProtocol = https和帐户名= XXX; AccountKey = XXX

but i get an exception: 但我得到一个例外:

System.Runtime.InteropServices.SEHException was caught Message=External component has thrown an exception. 捕获到System.Runtime.InteropServices.SEHException消息=外部组件引发了异常。 Source=msshrtmi ErrorCode=-2147467259 Source = msshrtmi ErrorCode = -2147467259

StackTrace: 堆栈跟踪:

   at RoleEnvironmentGetConfigurationSettingValueW(UInt16* , UInt16* , UInt64 , UInt64* )

   at  Microsoft.WindowsAzure.ServiceRuntime.Internal.InteropRoleManager.GetConfigurationSetting(String name, String& ret)

   at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(String configurationSettingName)

   at AzureUpload.Program.<Init>b__2(String configName, Func`2 configSetter) in C:\Users\siddjain\Documents\Visual Studio 2010\Projects\ConsoleAplication1\Program.cs:line 51

   at Microsoft.WindowsAzure.CloudStorageAccount.StorageAccountConfigurationSetting..ctor(String configurationSettingName)

   at Microsoft.WindowsAzure.CloudStorageAccount.FromConfigurationSetting(String settingName)

   at AzureUpload.Program.UploadBlob(String directory, String searchPattern, String container) in C:\Users\siddjain\Documents\Visual Studio 2010\Projects\ConsoleApplication1\Program.cs:line 87

InnerException 的InnerException

Do I need to start up azure services or something before running my app? 在运行我的应用程序之前,是否需要启动azure服务?

As Steve mentioned, you're trying to retrieve settings from the Azure configuration settings, which don't exist in a console app. 正如Steve所提到的,您正在尝试从Azure配置设置中检索设置,这些设置在控制台应用程序中不存在。

If you want to write your code to run in either non-Azure or Azure environments, you can specify a configuration publisher. 如果要编写代码以在非Azure或Azure环境中运行,则可以指定配置发布者。 When in Azure, it's simply a wrapper. 在Azure中,它只是一个包装器。 Otherwise, you basically redirect to app.config / web.config. 否则,您基本上会重定向到app.config / web.config。

There's a great CodeProject article that demonstrates this. 有一篇很棒的CodeProject文章可以证明这一点。 Here's a code snippet from that article. 这是该文章的代码片段。 In essence, you'd specify the configuration publisher in your OnStart method: 实质上,您需要在OnStart方法中指定配置发布者:

CloudStorageAccount.SetConfigurationSettingPublisher(
    StorageAccountFactory.GetConfigurationSettingPublisher()
);

In this case, you'd have a factory method deciding where to store/retrieve config settings, based on whether you're in Azure (RoleEnvironment.IsAvailable): 在这种情况下,您将有一个工厂方法,根据您是否在Azure(RoleEnvironment.IsAvailable)中决定存储/检索配置设置的位置:

public static Action<string, Func<string,bool>> GetConfigurationSettingPublisher()
{
    if (RoleEnvironment.IsAvailable)
      return (configName, configSetter) => 
    configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
    return (configName, configSetter) => 
    configSetter(ConfigurationManager.AppSettings[configName]);
}

FromConfigurationSetting uses the role runtime API, which tries to read configuration settings. FromConfigurationSetting使用角色运行时API,它尝试读取配置设置。 That will not work when running outside Windows Azure. 在Windows Azure外部运行时无效。

Use CloudStorageAccount.Parse("<myConnectionString>") instead. 请改用CloudStorageAccount.Parse("<myConnectionString>") (Or fetch the config setting from app.config or somewhere else and then pass it into Parse()). (或者从app.config或其他地方获取配置设置,然后将其传递给Parse())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 谁能向我解释这个浮点怪异现象? - Can anyone explain this floating point weirdness to me? 控制台应用程序中有关点的信息存储在哪里? - Where to store information about point in console application? 任何人都可以给我看一个MethodImplOptions.ForwardRef的例子 - Can anyone show me an example of MethodImplOptions.ForwardRef 谁能指出我用于解释堆栈计算机的C#代码? - Can anyone point me to C# code for an interpreted stack machine? 任何人都可以指出我的任何好的MVP文章/教程的方向? - Can anyone point me in direction of any good MVP articles/tutorials? 控制台应用程序 - 将 JSON 文件发送/写入 Azure Blob 存储 - Console Application - Send/Write JSON files to Azure Blob Storage 谁能给我一个使用BouncyCastle将.pem公共DSA密钥导入c#的示例吗? - Can anyone give me an example of using BouncyCastle to import .pem public DSA key into c#? 点查询一张azure存储表 - Point querying an azure storage table 任何人都可以为我简化这个算法吗? - Can anyone simplify this Algorithm for me? 任何人都可以向我解释 CreatedAtRoute() 吗? - Can anyone explain CreatedAtRoute() to me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM