我在ASP.NET MVC5中连接到Azure Blob存储帐户时遇到问题。
我查看了一些教程,并提出了一个带有BlobStorageContainerFactory
的简单存储库模式,因此我可以注入命名存储库。当我使用Azure存储本地开发模拟器时,此代码工作正常。这是工厂的代码:
public class BlobStorageContainerFactory
{
private CloudStorageAccount _storageAccount;
private CloudBlobClient _blobClient;
public CloudBlobContainer Get(string containerName)
{
//i want to get the connection string from the web.config eventually
//_storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["BattleBornPrepsStorageConnection"].ConnectionString);
//error occurs here
_storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=myaccountnamealllower;AccountKey=theprimaryaccountkeyfromazuremanagementportal;");
_blobClient = _storageAccount.CreateCloudBlobClient();
var container = _blobClient.GetContainerReference(containerName);
if (container.CreateIfNotExist())
{
// configure container for public access
var permissions = container.GetPermissions();
permissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permissions);
}
return container;
}
}
当我尝试解析连接字符串时出错:
An exception of type 'System.FormatException' occurred in Microsoft.WindowsAzure.StorageClient.dll but was not handled in user code
Additional information: Invalid account string.
堆栈追踪:
[FormatException: Invalid account string.]
Microsoft.WindowsAzure.CloudStorageAccount.<Parse>b__0(String err) +34
Microsoft.WindowsAzure.CloudStorageAccount.ParseStringIntoSettings(String s, Action`1 error) +344
Microsoft.WindowsAzure.CloudStorageAccount.TryParse(String s, CloudStorageAccount& accountInformation, Action`1 error) +19
Microsoft.WindowsAzure.CloudStorageAccount.Parse(String value) +135
MyProject.Data.BlobStorageContainerFactory.Get(String containerName) in f:\!ClientFiles\MyFiles\MyProject\LocalRepository\trunk\Release\MyProject\MyProject.Data\BlobStorageContainerFactory.cs:19
MyProject.Data.StorageRepository..ctor(String containerName) in f:\!ClientFiles\MyFiles\MyProject\LocalRepository\trunk\Release\MyProject\MyProject.Data\StorageRepository.cs:20
MyProject.Data.PublicationStorageRepository..ctor() in f:\!ClientFiles\MyFiles\MyProject\LocalRepository\trunk\Release\MyProject\MyProject.Data\PublicationStorageRepository.cs:12
DynamicInjectorc89300dbc50f4bd49b75f3ea31ffe3e6(Object[] ) +71
Ninject.Activation.Providers.StandardProvider.Create(IContext context) +884
Ninject.Activation.Context.ResolveInternal(Object scope) +218
Ninject.Activation.Context.Resolve() +216
Ninject.<>c__DisplayClass15.<Resolve>b__f(IBinding binding) +86
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4098209
Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) +169
Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) +747
Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) +269
Ninject.Activation.Providers.<>c__DisplayClass4.<Create>b__2(ITarget target) +69
System.Linq.WhereSelectArrayIterator`2.MoveNext() +66
System.Linq.Buffer`1..ctor(IEnumerable`1 source) +335
System.Linq.Enumerable.ToArray(IEnumerable`1 source) +77
Ninject.Activation.Providers.StandardProvider.Create(IContext context) +847
Ninject.Activation.Context.ResolveInternal(Object scope) +218
Ninject.Activation.Context.Resolve() +277
Ninject.<>c__DisplayClass15.<Resolve>b__f(IBinding binding) +86
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4098209
Ninject.Web.Mvc.NinjectDependencyResolver.GetService(Type serviceType) +145
Castle.Proxies.Invocations.IDependencyResolver_GetService.InvokeMethodOnTarget() +138
Castle.DynamicProxy.AbstractInvocation.Proceed() +117
Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed() +48
Glimpse.Core.Extensibility.ExecutionTimer.Time(Action action) +195
Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult) +198
Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context) +45
Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation) +183
Castle.DynamicProxy.AbstractInvocation.Proceed() +483
Castle.Proxies.IDependencyResolverProxy.GetService(Type serviceType) +187
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +87
[InvalidOperationException: An error occurred when trying to create a controller of type 'MyProject.Ui.Mvc.Areas.Admin.Controllers.PublicationController'. Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +438
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +257
Castle.Proxies.Invocations.IControllerFactory_CreateController.InvokeMethodOnTarget() +197
Castle.DynamicProxy.AbstractInvocation.Proceed() +117
Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed() +48
Glimpse.Core.Extensibility.ExecutionTimer.Time(Action action) +195
Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult) +198
Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context) +45
Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation) +183
Castle.DynamicProxy.AbstractInvocation.Proceed() +483
Castle.Proxies.IControllerFactoryProxy.CreateController(RequestContext requestContext, String controllerName) +202
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +157
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
答案 0 :(得分:2)
它看起来blob存储连接字符串不允许字符串末尾的;
。
他们的顺序并不重要。
答案 1 :(得分:0)
<add name="AzureStorageAccount" connectionString="DefaultEndpointsProtocol=https;AccountName=testdata;AccountKey=1gy3lpE7Du1j5ljKiupgKzywSw2isjsdfdsfsdfsdsgfsgfdgfdgfd/YThisv/OVVLfIOv9kQ==;"/>
</connectionStrings>
但有时它不会起作用并且会通过错误
An unhandled exception of type 'System.FormatException' occurred in Microsoft.WindowsAzure.Storage.dll
其他信息:找不到有效的帐户信息组合。 然后请尝试下面的代码:测试和100%工作
var accountName = "test2rdsfdg462";
var keyValue = "1gy3lpE7Du1j5ljKiupgKzywSfsdfdsfsdfsdfsdfsdfsdqGxd7/YThisv/OVVLfIOv9kQ==";
var useHttps = true;
var connValid = true;
var storageCredentials = new StorageCredentials(accountName, keyValue);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps);
var conString = storageAccount.ToString(connValid);
CloudStorageAccount sa = CloudStorageAccount.Parse(connString);