Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SDK v. 3.7.0 in netstandard 2.0 project results in Could not load file or assembly exception #1308

Closed
zengouu opened this issue Mar 26, 2020 · 4 comments

Comments

@zengouu
Copy link

zengouu commented Mar 26, 2020

Describe the bug
After installing Microsoft.Azure.Cosmos v. 3.7.0 to a netstandard2.0 class library that is called from .NET Framework 4.7.2 Console app throws an error of:

Could not load file or assembly 'Microsoft.Azure.Cosmos.Client, Version=3.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

To Reproduce

  • Start up local cosmos db emulator
  • Create a solution and add a new .netstandard 2.0 class library project
  • Add package Microsoft.Azure.Cosmos v. 3.7.0 via NuGet
  • Add a new .NET Framework 4.7.2 Console App Project
  • Call method in class library that creates a new cosmos db database

Code:

using System.Threading.Tasks;
using TestLibrary;

namespace TestConsole
{
	class Program
	{
		static async Task Main(string[] args)
		{
			var lib = new Class1();
			await lib.TestCosmos(); // App crashes when this is called.
		}
	}
}

using System;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;

namespace TestLibrary
{
	public class Class1
	{
		public async Task TestCosmos()
		{
			// This line is never reached

			var data = new Dto
			{
				Id = Guid.NewGuid(),
				Name = string.Empty
			};

			using ( CosmosClient client = new CosmosClient( "https://localhost:8081", "C2y6yDjf5/....." ) )
			{
				var dbResponse = await client.CreateDatabaseIfNotExistsAsync( "QueryDb" );
				var container = await dbResponse.Database.CreateContainerIfNotExistsAsync( "container", "/cont" );

				ItemResponse<Dto> response = await container.Container.CreateItemAsync( data, null );
			}
		}
	}

	public class Dto
	{
		public Guid Id { get; set; }
		public string Name { get; set; }
	}
}

Expected behavior
To my understanding this should work without a problem. Microsoft.Azure.Cosmos nuget changelog says the following:
Targets .NET Standard 2.0, which supports .NET framework 4.6.1+ and .NET Core 2.0+

Actual behavior
Calling class library method results in a following error:
Could not load file or assembly 'Microsoft.Azure.Cosmos.Client, Version=3.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Environment summary
SDK Version: 3.7.0
OS Version: Windows 10 Pro, Version 10.0.18363 Build 18363

Additional context
I also tried with following versions of Microsoft.Azure.Cosmos:
3.6.0
3.4.0

@ealsur
Copy link
Member

ealsur commented Mar 26, 2020

You don't need to add the netstandard package.

What Visual Studio version are you using?

@ealsur
Copy link
Member

ealsur commented Mar 26, 2020

If I create a new NET 4.7.2 console application and just add the Microsoft.Azure.Cosmos Nuget, this is what the CSPROJ looks like:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{2D7E3FB8-79D9-41CD-B773-924C9FB0BD2E}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>ConsoleApp1</RootNamespace>
    <AssemblyName>ConsoleApp1</AssemblyName>
    <TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Microsoft.Azure.Cosmos.Client, Version=3.7.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>packages\Microsoft.Azure.Cosmos.3.7.0\lib\netstandard2.0\Microsoft.Azure.Cosmos.Client.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Azure.Cosmos.Core, Version=2.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>packages\Microsoft.Azure.Cosmos.3.7.0\lib\netstandard2.0\Microsoft.Azure.Cosmos.Core.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Azure.Cosmos.Direct, Version=3.7.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>packages\Microsoft.Azure.Cosmos.3.7.0\lib\netstandard2.0\Microsoft.Azure.Cosmos.Direct.dll</HintPath>
    </Reference>
    <Reference Include="Microsoft.Azure.Cosmos.Serialization.HybridRow, Version=2.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>packages\Microsoft.Azure.Cosmos.3.7.0\lib\netstandard2.0\Microsoft.Azure.Cosmos.Serialization.HybridRow.dll</HintPath>
    </Reference>
    <Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <HintPath>packages\Newtonsoft.Json.10.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
    <Reference Include="System" />
    <Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
    </Reference>
    <Reference Include="System.Configuration" />
    <Reference Include="System.Configuration.ConfigurationManager, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>packages\System.Configuration.ConfigurationManager.4.5.0\lib\net461\System.Configuration.ConfigurationManager.dll</HintPath>
    </Reference>
    <Reference Include="System.Core" />
    <Reference Include="System.Data.OracleClient" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.IdentityModel" />
    <Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll</HintPath>
    </Reference>
    <Reference Include="System.Net" />
    <Reference Include="System.Numerics" />
    <Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
    </Reference>
    <Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>packages\System.Runtime.CompilerServices.Unsafe.4.5.1\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
    </Reference>
    <Reference Include="System.Runtime.Serialization" />
    <Reference Include="System.Security.AccessControl, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>packages\System.Security.AccessControl.4.5.0\lib\net461\System.Security.AccessControl.dll</HintPath>
    </Reference>
    <Reference Include="System.Security.Permissions, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>packages\System.Security.Permissions.4.5.0\lib\net461\System.Security.Permissions.dll</HintPath>
    </Reference>
    <Reference Include="System.Security.Principal.Windows, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>packages\System.Security.Principal.Windows.4.5.0\lib\net461\System.Security.Principal.Windows.dll</HintPath>
    </Reference>
    <Reference Include="System.ServiceModel" />
    <Reference Include="System.ServiceModel.Primitives, Version=4.5.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <HintPath>packages\System.ServiceModel.Primitives.4.5.0\lib\net461\System.ServiceModel.Primitives.dll</HintPath>
    </Reference>
    <Reference Include="System.ServiceProcess" />
    <Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
    </Reference>
    <Reference Include="System.Transactions" />
    <Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
      <HintPath>packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
    </Reference>
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
    <None Include="packages.config" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Project="packages\Microsoft.Azure.Cosmos.3.7.0\build\netstandard2.0\Microsoft.Azure.Cosmos.targets" Condition="Exists('packages\Microsoft.Azure.Cosmos.3.7.0\build\netstandard2.0\Microsoft.Azure.Cosmos.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('packages\Microsoft.Azure.Cosmos.3.7.0\build\netstandard2.0\Microsoft.Azure.Cosmos.targets')" Text="$([System.String]::Format('$(ErrorText)', 'packages\Microsoft.Azure.Cosmos.3.7.0\build\netstandard2.0\Microsoft.Azure.Cosmos.targets'))" />
  </Target>
</Project>

@zengouu
Copy link
Author

zengouu commented Mar 27, 2020

Thanks for the answer @ealsur, installing Microsoft.Azure.Cosmos also to the console application indeed fixes the problem. The code which uses Cosmos Db can still be located in the class library.

I'd appreciate if someone could share some light on why this approach works but when the nuget is only installed on the class library it does not :) Just asking to get a better understanding of this situation.

In any case this solves the problem I am facing so I'll close the issue.

@zengouu zengouu closed this as completed Mar 27, 2020
@ealsur
Copy link
Member

ealsur commented Mar 27, 2020

TLDR: Add <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to your NET Framework CSPROJ within a <PropertyGroup> node.

This is not an issue of the SDK, it is rather an issue of NET Framework with NET Standard class libraries and VS.

See the Workarounds section at: dotnet/standard#481

This is reported in a bunch of other places:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants