-1

Similar question:

  1. The located assembly's manifest definition does not match the assembly reference

Let's say we have a basic .NET Standard library, which references an external DLL we got from Nuget such as System.Data.SqlClient.

A class relies on the external DLL to function:

namespace BasicLibrary
{
    public class Class1
    {

        public void Method() 
        {
            var foo = new System.Data.Sql.SqlNotificationRequest();
        }
    }
}

We now build our library, targeting .NET Standard 2.0.

After doing that, we create a sample Console Application (.NET Core 3.0) and we add a reference to the library we previuosly built.

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var bar = new BasicLibrary.Class1();
            bar.Method();
        }
    }
}

The code builds fine, but as expected it crashes because it is missing a reference to System.Data.SqlClient

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Data.SqlClient, Version=4.6.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

Is there a way to check at build time for missing assemblies?

Acerbic
  • 100
  • 1
  • 9
  • 3
    Im confused how did you compile the other dll with the class (Class1) without the reference? You shouldn't have been able to compile `BasicLibrary` without the reference... – Trevor Dec 12 '19 at 16:33
  • `System.Data.SqlClient` is referenced in the `BasicLibrary` project, but not in the `ConsoleApp3` project – Acerbic Dec 12 '19 at 16:35
  • Is there a way to check if i am missing `System.Data.SqlClient` in `ConsoleApp3 ` before running the program itself? – Acerbic Dec 12 '19 at 16:38
  • if it's the same solution add reference to Project not assembly, if it's different then create local nuget repo, push `BasicLibrary` there and add reference to `BasicLibrary` package from local nuget ... – Selvin Dec 12 '19 at 16:38
  • `Is there a way to check id i am missing System.Data.SqlClient in ConsoleApp3` if nothing is using it, why check if it's missing, it doesn't make sense. If your missing a dll that you are using, it wont compile because references can't be resolved. – Trevor Dec 12 '19 at 16:39
  • @Çöđěxěŕ `ConsoleApp3` does not _directly_ use `System.Data.SqlClient`. However, it uses a class which uses it – Acerbic Dec 12 '19 at 16:40
  • If your missing the assembly, please tell me how you were able to compile `BasicLibrary`, I would love to know. – Trevor Dec 12 '19 at 16:42
  • I think i was unable to get my point through. `BasicLibrary` is successfully built, because it correctly references `System.Data.SqlClient`. After building `BasicLibrary` to a dll file, we add it to `ConsoleApp3`, which crashes, because it does not reference `System.Data.SqlClient` – Acerbic Dec 12 '19 at 16:43
  • After a little better explanation I see what you mean. Open up your `csproj` file and add `PackageReference` that to it, rebuild and try again. – Trevor Dec 12 '19 at 17:11
  • @Acerbic have you tried the above I left in a comment. I was able to replicate your issue and doing the above should resolve your issue. – Trevor Dec 13 '19 at 13:13

1 Answers1

-1

You can create targets which will do this job for you. Something like below (it checks for NUGET packages, in your case, it will be different path and library to check):

<Target Name="EnsureRequiredLibrariesExists" BeforeTargets="PreBuild">
    <PropertyGroup>
      <!--<ErrorText>YOUR CUSTOM ERROR MESSAGE</ErrorText>-->
      <ErrorText>Please include required dlls</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..\packages\Microsoft.Net.Compilers.1.1.1\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Microsoft.Net.Compilers.1.1.1\build\Microsoft.Net.Compilers.props'))" />    
</Target>

Also,

  • Please refer this link
  • and as mentioned here, Go through your projects in a text editor and look for references with tags in them. Like True or False. “Private” is a synonym for “Copy Local.”
sam
  • 1,937
  • 1
  • 8
  • 14