简体   繁体   中英

ASP.net Core 1.1.0 uploading file to azure storage - Visual Studio 2017

I imported a project from Visual Studio 2015 into Visual Studio 2017, I had an upload service that worked in 2015 that took a posted IFormFile and uploaded to a container in Azure Storage.

I have the following code:

public async Task<string> UploadFileToBlob(IFormFile file, string fileName)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(AppSettings.AzureStorageConnectionString);

        // Create a blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Get a reference to a container 
        CloudBlobContainer container = blobClient.GetContainerReference(AppSettings.AzureStorageContainer);

        // If container doesn’t exist, create it.
        await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Blob, null, null);

        // Get a reference to a blob 
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

        // Create or overwrite the blob with the contents of a local file
        using (var fileStream = file.OpenReadStream()) // file.OpenReadStream())
        {
            await blockBlob.UploadFromStreamAsync(fileStream);
        }

        return blockBlob.Uri.AbsoluteUri;
    }

I'm getting an error on the line using (var fileStream = file.OpenReadStream()) that says:

"Module 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' should be referenced"

I looked around to try to find that, tried to go to add a reference like I have in the past, and there isn't an option to even select COM objects?

Any idea what's up?

"Module 'System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' should be referenced"

According to your mentioned that it indicates that System.Private.CoreLib is not loaded

Please have a try to add it with the following code in the Startup.cs :

  public void ConfigureServices(IServiceCollection services)
        {
            var assembly = typeof(Startup).GetTypeInfo().Assembly;
            var assemblies = assembly.GetReferencedAssemblies().Select(x => MetadataReference.CreateFromFile(Assembly.Load(x).Location))
            .ToList();
            assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Private.Corelib")).Location));
            services.AddMvc();
        }

For my case, i had the similar problem. By updating Resharper to the version 2017.2 EAP 3, it solved my problem. If you're also using Resharper, try to update it :

Download & install 2017.2 EAP 3 https://www.jetbrains.com/resharper/eap/

My code to upload a file :

            [HttpPost]
            public async Task<IActionResult> UploadFile(IFormFile file)
            {
                string filePath = "UploadedFiles/" + Guid.NewGuid() + Path.GetExtension(file.FileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                   await file.CopyToAsync(fileStream);
                }
                return Json("file uploaded successfully");
            }

file.OpenReadStream(); ==> is working also without error

For info : https://youtrack.jetbrains.com/issue/RSRP-464676#u=1494270865373

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM