Unable to reference new C# class library project - c#

Background
I am working on a reasonably large legacy ASP.NET MVC solution.
Currently all of the code resides in just two projects (one containing model first C# generated code, the other containing the rest of the application).
Problem
I've added 2 projects to the solution - one for unit tests & one containing some code to be used as a service by the web application.
When I reference the service project from the main project there's a yellow warning sign by the reference.
As I said this is a legacy project with code all over the place & I'm not sure why this is happening.
I've tried Googling for a solution but haven't been able to find anything that gives some clues to this problem...
Note: There are just two classes in the service project
Here's a picture of the warning:

Make sure that both projects are set to the same version of the .NET Framework.
For example if your Main project targets to .NET Framework 4.0 and your Services project targets to .NET Framework 4.5.2, you can expect some compatibility issues.

Related

Surprise frontend requirement after splitting backend into 2 projects

I had 2 projects in my solution. One WPF frontend and one C# library backend. This worked fine for month, but now I decided that a part of the backend has become complicated enough that I would split it off into it's own, third project.
So now I have 3 projects: 1 with most of the backend code that depends on nothing, 1 other backend code that depends on the first for interfaces, and 1 frontend that depends on the other 2 projects.
There were some namespace changes that I had to fix throughout the code, but else it's a straight forward change. Or so I would think. Because now my frontend suddenly has a requirement for Microsoft.Xaml.Behaviors that was not there before I split the projects (the error tells me it's required for the Xaml triggers that worked fine before).
I tried around adding references to the backend projects thinking that they may be required for some inheritance reason, but that didn't change anything. Only when I imported the Microsoft.Xaml.Behavior.WPF Nuget package to the frontend was the reference resolved.
The weird thing is that I have 0 frontend changes. I can check out the previous commit and the requirement is gone. All that is changes for the frontend are some namespaces and the added reference to the new project.
Where does the requirement suddenly come from?
Microsoft dropped support for System.Windows.Interactivity & Microsoft.Expression.Interactions in .NET 4.8. The functionality in these assemblies was replaced with a NuGet package: Microsoft.Xaml.Behaviors.Wpf.
Most likely, you recently moved to .NET 4.8.
Referencing the NuGet package: Microsoft.Xaml.Behaviors.Wpf was the right course of action.
Here's a blog post that talks about the issue:
https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/

ASP.NET Core reference my object model library

I am building an ASP.NET Core app for WebAPI and I have a regular .NET class library containing my object model. Is there an easy way to reference that in my ASP.NET core app? I tried doing "Add Reference" and pointing to the dll in our release folder (this is how we normally go about adding references) but that gave me an error stating that only .NET assemblies could be referenced. It is a .NET assembly (.NET 4.61 - my ASP.NET Core app is targeting the full framework - also .NET 4.61). I also tried adding the existing project to my solution and referencing it as a project reference. This will add it to my project.json but I get an error that it can't resolve the reference. I did follow this guide and it gave me the same error - could not resolve reference. It doesn't seem like it should be this hard to get this to work. I would imagine that most developers would want to abstract their object model out from a particular web app and be able to reference it. At this point, I'm not using anything crazy. I just have a collection of interfaces using standard .NET types. Though down the road I also want to be able to add references to COM libraries (I'm not exactly confident right now that that is going to work). If anyone has any insight for me, it would be much appreciated.
Thanks.

Reverse engineering using EF in core 1.0

I've got a Database in SQLServer 2016 and I'm trying to reverse engineer it in VS2015. Because I want the Models to be in their own class and Not directly in my MVC Project. I create a Core 1.0 Class Library project and I then followed the instructions here Reverse Engineer however although the package manager suggested all installed without issue, when I look in the project.json file, under dependencies it red lines "Micorsoft.EntityFrameworkCore.Tools": 1.0.0-preview2-final it states that this reference does not support framework .Net Standard,version = v1.6. It works if I do the same in an MVC/Web Api based project, but not a class library. Any ideas how I can get this to work, it's part of my separation concern and tiering.
within the Solution I created a standard class library project setting the target framework to 4.6.1. (not .NetCore). See screen shot attached. you can see the references and the project type. I used the reverse engineering guide from my question to this project. Once in here I simple copied the classes that I wanted to my .Net Core Class Library project. This all worked fine.

MVC4 and MVC5 in one solution

I have an odd question. Can we have a solution comprising of both MVC4 and MVC5 projects?
The scenario is that there is a common project which is on MVC4 and being used by many other applications, but now we want to create a project in MVC5 or convert the existing template to MVC5, but also want to include that MVC4 project in the new solution.
Will that work? Will the new MVC5 project be able to work with old MVC4 projects in one solution?
I was following the guideline of conversion but it says to run the upgrade-package command and this command updates all packages in the MVC4 project as well, which we don't want to do.
Both projects are targeting .NET Framework 4.5 and using Visual Studio 2015.
A solution is just a collection of projects; there's no restriction they they use the same frameworks or dependencies, so it should be fine to have them both in one solution.
You probably cannot, however, deploy them to the same web application. They will likely need to be isolated to different applications in IIS or wherever you deploy them.
If you have dependencies between the MVC apps you will likely have problems. I would recommend moving as much logic that is not critical to the MVC app (classes, business logic, repositories, etc) out of the MVC apps into a common library that is independent of the app that uses it.

Visual Studio 2012 How to debug "Unable to add reference to project x" error?

In Visual Studio 2012 in a blank new Class Library I am trying to add reference to an existing project in the solution, also a Class Library, MonoGame.Framework.Windows8 (https://github.com/mono/MonoGame), but get the error:
Unable to add reference to Project MonoGame.Framework.Windows8
I have built the existing library, MonoGame, successfully and made it a dependency of the new one. What could be the problem?
(It's extremely annoying the error message does not give a reason!)
Trying to add the built dll has a reference gave a more explicit error message which has solved the problem for me, it said:
“A reference to ‘x’ could not be added. The project targets ‘.NetFramework’ while the file reference targets ‘.NetCore’. This is not a supported scenario”
The MonoGame Class Library is a Class Library (Windows Store apps) type! (The bit in parenthesis is important! Presumably the difference is the Windows Store apps type use .Net Core which is not the same as .Net Framework).
So to get it working your project must be a Class Library (Windows Store apps) which is available under the Windows Store option in Add New Project.
I suspect the problem is to do with the 'Target Framework' in project options.
I don't know how MonoGame works for Windows 8 but when you're developing for Android the target framework options are the different versions of Android [e.g. Android 2.2 (Froyo)]. When you create a regular class library you have choices between the different versions of the .NET framework or Mono [e.g. Mono / .NET 4.0].
Unfortunately, these frameworks are not compatible with each other. You can't add a project reference for Android to a .NET framework class library and visa-versa.
The solution is to use a Portable Class Library. They are specifically designed to deal with this issue. The downside is that you will only have access to the subset of assemblies provided in the lowest denominator you choose to target.
You might also want to consider code sharing between projects using linked files. It can make maintenance a little trickier but gives you a little more control over code that should compile in both target frameworks but doesn't fit into the portable class library. Here's a related question:
Project reference vs. file links in Mono multi-target projects
The key to all of this once you understand what's going on here is how you structure your solution. With a little care you can have nearly all your code shared across different platforms.
I might be because of both projects are using different Framework versions and change the Framework versions in both Projects of properties section then your problem might solve.
Same question I guess here:
Visual Studio 11 metro apps unable to add reference?
The answer was:
What are you adding a reference to? Metro style apps can only add references to other Metro style class libraries or portable class libraries

Categories