title: Could not load file or assembly 'Microsoft.Data.SqlClient
body: <p>I am calling a .net standard 2.0 library from a framework 4.7.2 test project sucessfully.
  <a href="https://i.stack.imgur.com/abhbv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/abhbv.png" alt="references"></a></p>
  
  <p>If I take my .net standard 2.0 dll (SBD.Standard) and create a new winforms project that references it then I get asked to add </p>
  
  <pre><code>Microsoft.EntityFrameworkCore, 
  then Microsoft.EntityFrameworkCore.SqlServer, 
  then Microsoft.Data.SqlClient 
  </code></pre>
  
  <p>and then my project runs successfully. (Although it would be ideal if the extra packages were added automatically)</p>
  
  <p><a href="https://i.stack.imgur.com/jaFgt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jaFgt.png" alt="enter image description here"></a></p>
  
  <p>However trouble occurs if I try to distribute my library via a Nuget Package built using Azure DevOps.</p>
  
  <p>However when I distribute the standard library via Nuget ( Using Azure DevOps to create the Nuget ) and include it in a new project I have an error.</p>
  
  <p>The call stack is</p>
  
  <pre><code>System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' or one of its dependencies. The system cannot find the file specified.
     at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
     at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()
     at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)
     at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
     at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
     at SBD.Standard.Helpers.HandyFuncs.QueueCommand(ApiDbContext connect, WorkTypeEnum workType, String 
  </code></pre>
  
  <p>The line of code that is causing the issue is </p>
  
  <pre><code>public static int QueueCommand(ApiDbContext connect, WorkTypeEnum workType, string description, int jobId, int signature, XElement elem)
  {
      var command =
          connect.EngineCommandQueues.SingleOrDefault(
              c =&gt;
                  (c.Status == 0|| c.Status == 2) &amp;&amp;
                  c.Signature == signature);
  </code></pre>
  
  <p>I did find that I needed to install the following packages</p>
  
  <pre><code>Microsoft.EntityFrameworkCore 3.1.1
  Microsoft.EntityFrameworkCore.SqlServer 3.1.1 which has a dependency on Microsoft.Data.SqlClient &gt;=1.019269.1
  </code></pre>
  
  <p>I see that Microsoft.Data.SQLClient v1.0.19269.1 is intalled</p>
  
  <p>I tried installing System.Linq</p>
  
  <p>I see the note in Nuget for Microsoft.Data.SqlClient</p>
  
  <blockquote>
    <p>When running on Windows, this library has a dependency on
    Microsoft.Data.SqlClient.SNI on .NET Framework</p>
  </blockquote>
  
  <p>and have Microsoft.Data.SqlClient.SNI v1.0.19235.1 installed</p>
  
  <p>I tried upgrading Microsoft.Data.SqlClient to 1.1 and the error changed to </p>
  
  <pre><code>System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.TdsParser' threw an exception. ---&gt; System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception. ---&gt; System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---&gt; System.ComponentModel.Win32Exception: Failed to load C:\Dev2\Combridge\SBD.ComBridge\UnitTestProjectStandard\bin\Debug\x86\SNI.dll ---&gt; System.ComponentModel.Win32Exception: The specified module could not be found
      at Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 64
  --- End of inner exception stack trace ---
      at Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize()
     at Microsoft.Data.SqlClient.SNILoadHandle..ctor() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs:line 36
     at Microsoft.Data.SqlClient.SNILoadHandle..cctor() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs:line 17
  --- End of inner exception stack trace ---
      at Microsoft.Data.SqlClient.TdsParser..cctor() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParser.cs:line 166
  --- End of inner exception stack trace ---
      at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:line 1411
     at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:line 1310
     at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal&amp; connection) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 357
     at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:line 773
     at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionClosed.cs:line 72
     at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1860
     at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1853
     at Microsoft.Data.SqlClient.SqlConnection.Open() in E:\agent1\_work\31\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1421
     at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
     at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
     at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
     at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
     at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
     at lambda_method(Closure , QueryContext )
     at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
     at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
     at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
  </code></pre>
  
  <p>[Update]
  Reading <a href="https://github.com/dotnet/SqlClient/issues/211" rel="nofollow noreferrer">This GitHub post</a> it seems it could have something to do with Azure Devops</p>
  
  <p>The post mentions</p>
  
  <blockquote>
    <p>Win32Exception: Failed to load
    C:__source_code\repo1\Simplified\bin\x86\SNI.dll So it's trying to
    load the 32 bit version of sni.dll and it isn't present.</p>
    
    <p>That makes me suspect that you build it on a 64 bit system and then
    just transferred the files to the other machine. You need to publish
    the project for an x86 target to get the right native dependency
    resolved. Give it a try.</p>
  </blockquote>
  
  <p>I am running Windows 10 so I dont see why it would be trying to load a 32 bit version of sni.dll</p>
  
  <p>I can see in the Azure Artifiacts</p>
  
  <blockquote>
    <p>Microsoft.Data.SqlClient.SNI 1.0.19235.1</p>
  </blockquote>
  
  <p>[Update]</p>
  
  <p>I see from <a href="https://stackoverflow.com/questions/43995432/could-not-load-file-or-assembly-microsoft-extensions-dependencyinjection-abstrac">this question</a> that I need to force MSBuild to create / update a YourProject.dll.config file containing the necessary binding redirects.
  So Nuget should be creating that config file too. I wonder how that effects things.</p>
  
  <p>My azure-pipelines.yml is</p>
  
  <pre><code># .NET Desktop
  # Build and run tests for .NET Desktop or Windows classic desktop solutions.
  # Add steps that publish symbols, save build artifacts, and more:
  # https://docs.microsoft.com/azure/devops/pipelines/apps/windows/dot-net
  
  trigger:
  - master
  
  pool:
    vmImage: 'windows-latest'
  
  variables:
    solution: '**/*.sln'
    buildPlatform: 'Any CPU'
    buildConfiguration: 'Release'
    Major: '2'
    Minor: '0'
    Patch: '0'
  
  steps:
  - task: NuGetToolInstaller@0
    inputs:
      versionSpec: '&gt;=4.3.0'
      checkLatest: true
  
  - task: NuGetCommand@2
    inputs:
      restoreSolution: '$(solution)'
  
  - task: VSBuild@1
    inputs:
      solution: '$(solution)'
      platform: '$(buildPlatform)'
      configuration: '$(buildConfiguration)'
  
  - task: NuGetCommand@2
    inputs:
      command: pack
      packagesToPack: '**/*.csproj'
      versioningScheme: byPrereleaseNumber
      majorVersion: '$(Major)'
      minorVersion: '$(Minor)'
      patchVersion: '$(Patch)'
  
  - task: NuGetCommand@2
    inputs:
      command: pack
      packagesToPack: '**/*.vbproj'
      versioningScheme: byPrereleaseNumber
      majorVersion: '$(Major)'
      minorVersion: '$(Minor)'
      patchVersion: '$(Patch)'
  
  - task: NuGetCommand@2
    displayName: 'NuGet push'
    inputs:
      command: push
      publishVstsFeed: 'SBDCommonFeed'
      allowPackageConflicts: true
  </code></pre>
  
  <p>[Update]</p>
  
  <p>I tried changing the yaml to use dotnet pack but get an error</p>
  
  <p><a href="https://i.stack.imgur.com/RU5ck.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RU5ck.png" alt="error"></a></p>
  
  <p>[Update]</p>
  
  <p>I tried replacing the pack section of the yaml with </p>
  
  <pre><code>- task: DotNetCoreCLI@2
    inputs:
      command: 'pack'
      selectOrConfig: 'config'
      nugetConfigPath: '$(System.DefaultWorkingDirectory)/NuGet.config'
      externalEndpoints: $(externalFeedCredential)
  </code></pre>
  
  <p>Now I get <a href="https://i.stack.imgur.com/vUOy5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vUOy5.png" alt="resource authorization issue"></a> Resource Authorization Issue</p>
  
  <p>When I click the button to authorize I get a message: insufficient permissions or missing resources. Even though I am logged in as the repository owner.</p>
  
  <p>I guess I need to learn what to put in externalEndpoints</p>
  
  <p>[Update]</p>
  
  <p>I tried this instead</p>
  
  <pre><code>- task: DotNetCoreCLI@2
    inputs: 
      command: 'pack'
      outputDir: '$(Build.ArtifactStagingDirectory)/TestDir'
  </code></pre>
  
  <p>Now the error becomes</p>
  
  <pre><code> error MSB4057: The target "pack" does not exist in the project.
  </code></pre>
  
  <p>investigating <a href="https://stackoverflow.com/questions/41021034/error-msb4057-missing-target-pack-when-building-netstandard-nuget-package">this answer</a></p>