简体   繁体   中英

Reference Azure Table Storage into a Portable Class Library (PCL)

I've just created a portable class library that used the Azure Table storage v7.0.1-preview .

And I have some problem making this works properly. The code below throw an exception ( TypeLoadException ):

 var cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
 var cloudTable = cloudStorageAccount.CreateCloudTableClient().GetTableReference(settingsTableName);
 var seg = await cloudTable.ExecuteQuerySegmentedAsync(new TableQuery(), null);
 return seg.Results;

Could not load type 'Microsoft.WindowsAzure.Storage.Table.TableQuerySegment' from assembly 'Microsoft.WindowsAzure.Storage, Version=7.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

I've created a portable class library :

使用Visual Studio 2015创建可移植的类库

I've added a reference to the WindowsAzure.Storage v7.0.1-preview:

Install-Package WindowsAzure.Storage -Version 7.0.1-preview

使用Nuget Package Manager控制台添加WindowsAzure.Storage v7.0.1预览

So to reproduce the problem, I've got a single class in this library:

using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;

public class AzureTableUtilitiy
{
    public static async Task<IList<DynamicTableEntity>> RetrieveAsync(string connectionString, string settingsTableName)
    {
        var cloudStorageAccount = CloudStorageAccount.Parse(connectionString);
        var cloudTable = cloudStorageAccount.CreateCloudTableClient().GetTableReference(settingsTableName);
        var seg = await cloudTable.ExecuteQuerySegmentedAsync(new TableQuery(), null);
        return seg.Results;
    }
}

And I just wanted to test it ... using a console app (named AzureTableStorageTest) => I've added a reference to WindowsAzure.Storage v7.0.1-preview also in this project.

The Console App has a reference to the Portable CLass Library.

class Program
{
    static void Main(string[] args)
    {
        var result = AzureTableUtilitiy.RetrieveAsync(
         ConfigurationManager.ConnectionStrings["StorageAccountConnectionstring"].ConnectionString,
         "ExternalSettingsStore").Result;
    }
}

So the code above throw the exception. But If I execute the code directly from the console app, every thing work fine:

class Program
{
    static void Main(string[] args)
    {
        var cloudStorageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageAccountConnectionstring"].ConnectionString);
        var cloudTable = cloudStorageAccount.CreateCloudTableClient().GetTableReference("ExternalSettingsStore");
        var seg = cloudTable.ExecuteQuerySegmentedAsync(new TableQuery(), null).Result;
        var result = seg.Results;
    }
}

Can someone explain me what I am missing ^^ ?

EDIT : Details of the exception:

A System.TypeLoadException is thrown.

  • Message: Could not load type 'Microsoft.WindowsAzure.Storage.Table.TableQuerySegment' from assembly 'Microsoft.WindowsAzure.Storage, Version=7.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

  • StackTrace:

    at AzureTableStorageUtility.AzureTableUtilitiy.RetrieveAsync(String connectionString, String settingsTableName)

    at AzureTableStorageTest.Program.Main(String[] args) in C:\\Projects\\AzureTableStorageTest\\AzureTableStorageTest2\\Program.cs:line 24

EDIT

this is a sample app that reproduce the issue :

this is a shot in the dark here, but could you try changing the value of "Copy Local" in your PCL's Microsoft.WindowsAzure.Storage reference to true.

Here's an article that talks about this: https://blogs.msdn.microsoft.com/asiatech/2013/01/09/how-to-resolve-the-could-not-load-file-or-assembly-issues-on-windows-azure-web-sites/

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