Reason for "Cannot resolve dependency to assembly" error - c#

When would the following message be shown?
Error 1 Unknown build error, 'Cannot resolve dependency to assembly 'Infragistics2.Win.v10.3, Version=10.3.20103.2015, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.'
I have added few DLLs to an existing working project. I know it's difficult to pin point the reason without the details on the code-base, but what I would like to know the general cause for such error messages to appear?
Note: Just created a fresh WPF project and added the bunch of new DLLs. The issue is present there also. Therefore, it's highly likely that the issue is due to organization and content of the DLLs.

This can happen if your xaml file statically references a view model class and that view model class has a dependency which may not be recognised as missing in the xaml file. You will need to add that dependency to the offending project. Because it is referenced in the xaml file compile time errors aren't clear on the source of the problem.

Proper version of Infragistics DLL was missing and adding it resolved the issue.
Note: The problem is too localized and hence this solution is useful at local system only.

Related

The type is defined in an assembly that is not referenced, how to find the cause?

I know the error message is common and there are plenty of questions on SO about this error, but no solutions have helped me so far, so I decided to ask the question. Difference to most of similar questions is me using App_Code directory.
Error message:
CS0012: The type 'Project.Rights.OperationsProvider' is defined in an
assembly that is not referenced. You must add a reference to assembly
'Project.Rights, version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
Source File:
c:\inetpub\wwwroot\Test\Website\App_Code\Company\Project\BusinessLogic\Manager.cs
Following suggestions here and here, I have deleted all instances of Project.Rights.dll inside C:\Windows\Microsoft.NET/*.*
According to this, I checked if .cs files in question have build action set to "Compile". They do.
I have also double checked that the .cs file containing the "Project.Rights.OperationsProvider" type is deployed to App_Code directory.
For some reason, application is not looking for the type in the App_Code directory. Since I've deleted all instances of Project.Rights.dll (that I know of), I don't know which assembly the error message is mentioning.
When you get this error it isn't always obvious what is going on, but as the error says - you are missing a reference. Take the following line of code as an example:
MyObjectType a = new MyObjectType("parameter");
It looks simple enough and you probably have referenced "MyObjectType" correctly. But lets say one of the overloads for the "MyObjectType" constructor takes a type that you don't have referenced. For example there is an overload defined as:
public MyObjectType(TypeFromOtherAssembly parameter) {
// ... normal constructor code ...
}
That is at least one case where you will get this error. So, look for this type of pattern where you have referenced the type but not all the types of the properties or method parameters that are possible for functions being called on that type.
Hopefully this at least gets you going in the right direction!
Check target framework in the projects.
In my case "You must add a reference to assembly" actually meant, that caller and reference projects didn't have the same target framework. The caller project had .Net 4.5 , but referenced library had target 4.6.1.
I am sure, that MS compiler can be smarter and log more meaningful error message. I've added a suggestion to https://github.com/dotnet/roslyn/issues/14756
In my case this was because doing a NuGet package update had only updated references to a dll dependency in some but not all projects in my solution - resulting in conflicting versions. Using a grep-style tool to search text within *.csproj files in my solution it was then easy to see the projects that still needed to be updated.
When you get this error, it means that code you are using makes a reference to a type that is in an assembly, but the assembly is not part of your project so it can't use it.
Deleting Project.Rights.dll is the opposite of what you want. You need to make sure your project can reference the assembly. So it must either be placed in the Global Assembly Cache or your web application's ~/Bin directory.
Edit-If you don't want to use the assembly, then deleting it is not the proper solution either. Instead, you must remove all references to it in your code. Since the assembly isn't directly needed by code you've written, but instead by something else you're referencing, you'll have to replace that referenced assembly with something that doesn't have Project.Rights.dll as a dependency.
In my case, I was referencing a library that was being built to the wrong Platform/Configuration (I had just created the referenced library).
Furthermore, I was unable to fix the problem in Visual Studio Configuration Manager -- unable to switch and create new Platforms and Configurations for this library. I fixed it by correcting the entries in the ProjectConfigurationPlatforms section of the .sln file for that project. All its permutations were set to Debug|Any CPU (I'm not sure how I did that). I overwrote the entries for the broken project with the ones for a working project and changed the GUID for each entry.
Entries for functioning project
{9E93345C-7A51-4E9A-ACB0-DAAB8F1A1267}.Release|x64.ActiveCfg = Release|x64
{9E93345C-7A51-4E9A-ACB0-DAAB8F1A1267}.Release|x64.Build.0 = Release|x64
Entries for corrupted project
{94562215-903C-47F3-BF64-8B90EF43FD27}.Release|x64.ActiveCfg = Debug|Any CPU
{94562215-903C-47F3-BF64-8B90EF43FD27}.Release|x64.Build.0 = Debug|Any CPU
Corrupted entries now fixed
{94562215-903C-47F3-BF64-8B90EF43FD27}.Release|x64.ActiveCfg = Release|x64
{94562215-903C-47F3-BF64-8B90EF43FD27}.Release|x64.Build.0 = Release|x64
I hope this helps someone.
It just happened to me that different projects were referencing different copies of the same dll.
I made sure all referenced the same file on disk, and the error disappeared as I expected.
Unloading and reloading the class library in Visual Studio solved this for me.
For me, this was caused by the project both directly and indirectly (through another dependency) referencing two different builds of Bouncy Castle that had different assembly names. One of the Bouncy Castle builds was the NuGet package, the other one was a debug build of the source downloaded from GitHub. Both were nominally version 1.8.1, but the project settings of the GitHub code set the assembly name to BouncyCastle whereas the NuGet package had the assembly name BouncyCastle.Crypto. Changing the project settings, thus aligning the assembly names, fixed the problem.
It didn't work for me when I've tried to add the reference from the .NET Assemblies tab.
It worked, though, when I've added the reference with BROWSE to C:\Windows\Microsoft.NET\Framework\v4.0.30319
I had this issue on a newly created solution that used existing projects. For some reason, one project could not "see" one other project, even though it had the same reference as every other project, and the referenced project was also building. I suspect that it was failing to detect something having to do with multiple target frameworks, because it was building in one framework but not the other.
Cleaning and rebuilding didn't work, and restarting VS didn't work.
What ended up working was opening a "Developer Command Prompt for VS 2019" and then issuing a msbuild MySolution.sln command. This completed successfully, and afterwards VS started building successfully also.
one of main reason can be the property of DLL
you must before do any thing to check the specific version property if it true make it false
Reason:
maybe the source code joined with other (old)version when you build it , but this Library upgraded with new update the version now different in the Assembly Cash and your application forbidden to get new DLL ,and after disable specific version property your applacaten will be free to get the new version of DLL references
Maybe a library (DLL file) you are using requires another library. In my case, I referenced a library that contained a database entity model - but I forgot to reference the entity framework library.
This can also mean you use a library, which exposes (public) types that are defined in a library. Even when you do not use these specifically in your library (the one that doesn't build).
What this probably prevents is you writing code that uses a class (which in its signature has the types from a library not referenced) that you cannot use.
For me the reason why the error appeared was that the WebForm where the error was reported has been moved from another folder, but the name of its codefile class remained unchanged and didn't correspond to the actual path.
Initial state:
Original file path: /Folder1/Subfolder1/MyWebForm.aspx.cs
Original codefile class name: Folder1_Subfolder1_MyWebForm
After the file was moved:
File path: /Folder1/MyWebForm.aspx.cs
Codefile class name (unchanged, with the error shown): Folder1_Subfolder1_MyWebForm
The solution:
Rename your codefile class Folder1_Subfolder1_MyWebForm
to one corresponding with the new path: Folder1_MyWebForm
All at once - problem solved, no errors reporting..
The type 'Domain.tblUser' is defined in an assembly that is not
referenced. You must add a reference to assembly 'Domain,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
**Solved:**
Add reference of my domain library layer to my web app libary layer
Note: Make sure your references are correct according to you DI container
In my case this was because I used
Implicit Operator
between BLL and DAL classes.when I want to use BLL Layer In Application Layer I got this error.
I changed
implicit operator
to
explicit operator
it be OK.
Thanks
In my case the version of the dll referenced was actually newer than the one that I had before.
I just needed to roll back to the previous release and that fixed it.
I have a similar problem, and I remove the RuntimeFrameworkVersion, and the problem was fixed.
Try to remove 1.1.1 or
My problem was that the Output Type for one of my projects was set to Console Application. To fix this, I right-clicked the project, chose Properties, clicked the Application tab, and change Output Type (from Console Application) to Class Library. After I re-compiled, this error went away.
Clean your solution and rebuild worked for me (in Visual Studio, these are options you get when you right click in your solution explorer), the error is gone in my project.

Unable to resolve the type 'http://schemas.microsoft.com/netfx/2009/xaml/activities:Activity'

I just checked out a version of MVC3 project and am getting this error when building it:
Unable to resolve the type 'http://schemas.microsoft.com/netfx/2009/xaml/activities:Activity' or one of its arguments. Ensure that the type name is correct or that the Assembly containing the type is loaded.
The files the compiler appears to be having a problem with is under 'BuildProcessTemplates' and are DefaultTemplate.xaml, LabDefaultTemplate.xaml and UpgradeTemplate.xaml. I don't know what these files are but removing them gives me even more errors.
An error previous to this was 'Project file must include the .NET Framework assembly 'WindowsBase, PresentationCore, PresentationFramework' in the reference list' which, from googling, concerns xaml files' Build Action must be reset.
I don't know where to go from here. Can anyone shed some light on this?
Adding reference to System.Activities will solve this issue,
I would recommend to add reference to System.Xaml as well in case if you haven't added it, this is also need for WF development

Visiblox invalid assembly reference

I am struggling with a problem regarding to visiblox. I am quite new to the WPF concept, but I am getting the hang of it. I have quite some expirence with the classic forms.
My problem is that the designer keeps telling me that the XAML contains errors. And I get the folling error in my error list:
Unable to load the metadata for assembly 'Visiblox.Charts'.
This assembly may have been downloaded from the web.
See http://go.microsoft.com/fwlink/?LinkId=179545.
The following error was encountered during load: Could not load file or assembly
'Visiblox.Charts, Version=2.1.4.31043, Culture=neutral, publicKeyToken=1543c03f04c4461b' or one of its dependencies.
I have added the reference to the project, and I have added the line:
xmlns:charts="clr-namespace:Visiblox.Charts;assembly=Visiblox.Charts"
I can build and run the project, but the designer keeps telling me that my assemblies are not correct. I tried to clean and rebuilt the project. Restarted Visual Studio and the computer, but nothing seems to work. Anyone any clue? Thank you in advance.
I'd suggest following the link it provides: http://go.microsoft.com/fwlink/?LinkId=179545
The problem is that the dll is "locked" by windows security and you need to unlock it before you can reference it. The link specified tells you how.

log4net dependency problem

I have an issue with log4net which has been bugging me for a while and I've resolved to sort it.
I have a class library which references log4net. If I reference this class library in another project I must then reference log4net in this project otherwise I get a build error
Unknown build error, 'Cannot resolve dependency to assembly 'log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821' because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.'
I'm aware that the error message is probably telling me the solution, unfortunately I don't speak gibberish...
Cheers guys
Alex..
Here is the link which describes what is happening and how to fix it:
https://learn.microsoft.com/en-us/archive/blogs/jmstall/viewing-types-with-reflection-only
An excerpt from the above link:
So what happened was that it tried to
get the System.Type for Bar, but to
resolve the type it needs to load the
base class, which is in another dll.
Reflection-Only context doesn't do
binding policy so it can't find that
dll. The LoaderException hint says to
use the ReflectionOnlyAssemblyResolve,
which provides more information about
this.
To use the reflection API, you have to resolve all the dependencies used.
I had the same problem with log4net when creating a new WPF project and adding a reference to another project that referenced log4net. I resolved the issue by adding the log4net.dll to the GAC using these instructions: http://msdn.microsoft.com/en-us/library/dkkx7f79.aspx
I know it's been a while but I wanted to share what solved it for me.
It looks like the main cause for this problem is the way log4net requires it's config file in the assembly info of the class library. WPF doesn't seem to like that. Referencing log4net in the wpf app itself caused the log4net config file to be overwritten for me, as the wpf app is later in the build order and generates a default log4net config file.
So to fix your class library for use with assembly preloading, edit it as follows:
In the class library delete this line in AssemblyInfo.cs :
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config")]
Now in the entry point of your library setup your logger like this:
FileInfo configFileInfo = new FileInfo("log4net.config");
log4net.Config.XmlConfigurator.ConfigureAndWatch(configFileInfo);
If you setup log4net in the app.config file, just reference that one.
I found this solution here.
I had the same problem. I still don't fully understand it, but I can tell you how I solved my problem.
I had a unit test project B with a project reference to project A, which references log4net. So to me the gibberish means when Visual Studio is trying to create the .accessor file for the unit test project, it reflects on project A. This means it tries to load project A's references, but the assembly loader can't find it because I don't have log4net in the GAC, just locally for project A to reference. In my case, adding log4net to my DEVPATH (GAC would work, too) was the solution.

Error loading XAML Markup at runtime when hosted in strong named assembly

I have a mixed Winform/WPF application which frequently throws the exception:
Could not load file or assembly <Full Assembly Name> or one of it's dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT 0x80131040) Error at object <Object Name> in markup file <file>.xaml
The WPF portion consists of a base custom control and multiple User Controls which inherit from that base. All of the WPF classes and templates are located within the same assembly.
This exception only started occuring after I strongly named all the assemblies in the application.
All of the assemblies used by the project are loaded in to a single visual studio solution.
Now for the confusing part. This exception only occurs part of the time. If I rebuild the entire solution sometimes it will be a good build and everything will work fine. Other times I'll rebuild the solution and it will be a bad build and the WPF sections will always throw this exception.
To fix it, I end up using the Clean Solution menu item and rebuilding multiple times until it gives me a "good" build.
It seems clear that the issue is the linker mismatching assembly versions from previous builds. However, I'm stumped why it would do this. The build order and project dependencies is set correctly.
Yeah, I've seen this kind of thing before too. Not sure if it's an option for you, but could you try explicitly setting the version number of all signed assemblies rather than auto-incrementing it?

Categories

Resources