1

我正在使用 MVC4 测试项目尝试将文件上传到 Azure 媒体服务。

我可以很好地创建我的 CloudMediaContext。我还可以创建一个 IAsset,并在其中创建一个 IAssetFile。 但是,当我尝试向其上传新文件时,出现以下异常:

Server Error in '/' Application.

Could not load file or assembly 'Microsoft.WindowsAzure.Storage, Version=3.0.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

我不确定错误是否存在于 MVC4 中,因为无法从其自己的目录上传文件?根据错误,我缺少一些引用或程序集,但我唯一添加的是 Azure.MediaServices 的 NuGet 包,它安装了 Azure MediaServices .NET SDK ...

这是上传的 HomeController 代码:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
    string localStorage = Server.MapPath("~/UploadedMedia");
    string uploadedFileName = file.FileName;
    if (!Directory.Exists(localStorage))
        Directory.CreateDirectory(localStorage);
    file.SaveAs(string.Format(@"{1}\{0}", file.FileName, localStorage));
    _mediaServicesContext = new MediaServicesContext();
    _mediaServicesContext.UploadFile(string.Format(@"{1}\{0}", file.FileName, localStorage));

    return View(new UploadModel() { FileName = uploadedFileName, Post = true, Result = true });
}

这是我的 MediaServices 课程(做这项工作)

public class MediaServicesContext
{
    #region ATTRIBUTES
    private const string MEDIACONTEXTNAMEKEY = "MediaServiceAccountName";
    private const string MEDIACONTEXTPASSKEY = "MediaServiceAccountAccessKey";
    private const string MEDIASTORAGENAMEKEY = "StorageAccountName";
    private const string MEDIASTORAGEPASSKEY = "StorageAccountAccessKey";

    private readonly CloudMediaContext _mediaContext;
    #endregion

    #region CTORS
    public MediaServicesContext()
    {
        _mediaContext = new CloudMediaContext(accountName: ConfigurationManager.AppSettings[MEDIACONTEXTNAMEKEY],
                                                accountKey: ConfigurationManager.AppSettings[MEDIACONTEXTPASSKEY]);
    }
    #endregion

    #region PROPERTIES
    public CloudMediaContext MediaContext
    {
        get { return _mediaContext; }
    }
    #endregion

    #region METHODS
    public void UploadFile(string path)
    {
        // Create a .NET console app
        // Set the project properties to use the full .NET Framework (not Client Profile)
        // With NuGet Package Manager, install windowsazure.mediaservices
        // add: using Microsoft.WindowsAzure.MediaServices.Client;
        var uploadFilePath = path;
        var context = _mediaContext;
        var uploadAsset = context.Assets.Create(Path.GetFileNameWithoutExtension(uploadFilePath), AssetCreationOptions.None);
        var assetFile = uploadAsset.AssetFiles.Create(Path.GetFileName(uploadFilePath));
        assetFile.Upload(uploadFilePath);
    }
    #endregion
}
4

1 回答 1

1

出于某种原因,以下解决了我的问题。

在 NuGet PackageManager 控制台中,我输入了以下内容:

PM> Install-Package WindowsAzure.Storage

这似乎更新了 Microsoft.WindowsAzure.Storage 依赖项并允许我的程序正常工作。很奇怪,微软。

于 2014-02-09T18:40:41.360 回答