Object referencing across projects in same solution - c#

I had a solution that contained two projects, one for the web layer that also contained the models, and another for a Windows forms project that did some other "stuff". The forms project referenced the web project, and all was fine up until I needed to reference something from the web project in the forms project, which I could not do for circular reference reasons.
So I created a new solution, put the data layer in one project and the web layer in another, so later on I could add the third project, and put in the references I need. Now when I run the web project, I get the following error;
So I understand that the web project is struggling to find my namespace from the data project, but I have referenced it, so I don't know what more to do.
***EDIT
This comes as no shock, but it's the view that's the issue. If I edit my Index.cshtml, it recognises the referenced project. I tried adding;
#using LottoData.Models
and intellisense completed as I typed. However, when I run it now, I get;
Help!

This was my bad. I forgot to change the output type for the data project to "Class Library", it was still set to "Console Program". Changed that, removed and re-added the reference, cleaned and rebuilt and it works fine now. Thanks #David and #Armand for your input.

Related

Cycles detected

My project does not add reference to another in the same solution.
I created a project for a website and and abstracted all the parts of the program into .Net Class Libraries (My models in a different class library, Interfaces in a different class library, business logic in a different class library). I referenced all the projects correctly and I also tried creating something I call EntityRepository which I initially kept the DbContext inside. But due to the reason that I wanted to use Microsofts' implementation of Identity and then scaffold the logic out, I could not find the DbContext from the other project which is required when scaffolding so I had to exclude that project out of the solution. Normally when you create a project and select Single user authentication, .Net core adds an initial DbContext into the project. So when scaffolding the implementation of AspNetCore Identity into my project, i have to choose the DbContext. Because of that, my business logic has broken into two sides. The other logic is inside another project in the same solution, the Identity and authentication is inside the startup project which is the web application. When I tried calling the services which implement the business logic from the other project into the web project, it could not add reference to the services project. I now added reference manually. After that I now saw an error written
Detail Error:
Severity Code Description Project File Line Suppression State
Error NU1108 Cycle detected.
CBTSoftware.Web.Host -> CBTSoftware.Services -> CBTSoftware.Web.Host. CBTSoftware.Web.Host C:\Users\Tavershima\source\repos\CBTSoftware\CBTSoftware.Web.Host\CBTSoftware.Web.Host.csproj 1```
How can I resolve this?
Fist I want to show gratitude to those who answered above because they put on the right track to solving this issue. In my case the issue was caused by untrack files causing NU1108 and was resolved by running git clean -fxd
*git clean documentation
I know that this is old but I'd like to add here as well.
I had the same situation as Sebastian Widz answer but what really worked for me was opening the Properties of the problematic project.
This reloaded the project files/dependencies and fixed the "Cycle Detected" issue.
In my case the problem had nothing to do with actual dependencies.
One day I opened a solution (which was fine the day before) and could not compile it. NU1108 Cycle detected error was reported in error log for several projects.
Solution:
Examine all projects in the solution, check the solution content.
If for some projects you see wrong content like if the project had files attached from a different project, expand its nodes and wait a bit, VS should refresh the nodes after a while
You may also try to Clean Solution and Reload each project.
Consider making a project to contain your EntityRepository, like CBTSoftware.Data, and adding a reference to it from your services project:
CBTSoftware.Services -> CBTSoftware.Data
Then, you can continue referencing your services project from your web project:
CBTSoftware.Web.Host -> CBTSoftware.Services
You'll still be able to configure your EntityRepository in your Startup.cs file because it will know about your CBTSoftware.Data project transitively. Just make sure to remove the reference to your web project from your services project, since this is creating a cycle.
I have solved the problem by deleting the DbContext which i created in the Web Project and using the one which I created in another project which is a .Net Library then I added this line of code in my StartUp.cs file
services.AddDbContext<CBTDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
By adding it, I was able to find the DbContext in the Web Project.
Then another problem has arisen when I'm trying to add migrations. Which I would open in another post
I also had this issue
In my case, I was using multiple projects in one solution. And in one of those projects, it's Dependencies was showing warning signs, within these Dependensies, there was a folder with included projects also each showing a warning sign.
I could solve it by right-clicking on the Dependencies, click on 'Add Project Reference' and then unselect all projects related to it/showing the warning signs.
After rebuilding, the cycle problem was gone.

How to find error in compile time instead of execution time in referenced DLL?

In Solution 1 i have a project with name MainProject this project referenced to Another Project with Name Model and a DLL(assembly) with name Presentation.
This DLL building in another solution, This Solution Contains Presentation project and ModelPerim(this project have same assembly with Model Project) project. In Presentation project i worked with ModelPrim.
Question:
In MainProject If exist Conflict with Model (such as a property in ModelPrim Not exist in Model) this conflict raised in execution time, How to i know this conflict in compile time?!
Hope i can to illustrate this problem:
There may be multiple ways of solving your problem.
If possible I would strongly suggest that you make model and modelperim the same library and reference that project in both your solutions.
If that is not possible you could set up a build server that run unit tests to spot inconsistencies. The tests could be written to compare both libraries.
Yet another solution would be to make a code generation template that would generate modelperim from model or wise versa. You would run that (automatically if possible) when you make changes generate a new "clone".
I reference Model dll instead of ModelPrim project in solution2 and then build solution2, So conflict props were seen.

Adding this project as a reference would cause a circular dependecy

I'm working on an MVC 3 project. I was told to get all the models and viewmodels out of the projects and put them in a class library so that they can be referenced from different types of projects. However, now that I've transferred all the viewmodels and models from the web project to a class library, and removed all the references to the web project, I cannot set reference to the class library from my web project with the reason stated in the question title. WHy is this happening? In my class library I'm not referencing the main project anywhere!!! Any suggestions? Thanks a lot!!
Experienced this earlier. Check the project that you are going to add if it has the reference to the project you are adding in it.
Example: Project A with reference to Project B. Then in Project B, you're adding Project A as reference.
well this usually happens for a reason, and this is that there is a cirrular reference,maybe not a direct one but an indirect one (through third project, how many projects do you have in your solution?).
In your library project remove all other projects references from solution, and try it again.
good luck
almir

Different location of assemblies stopped the type casting

I am writing a custom Control class in C# for my main project.
There're 2 projects, one for my Control and one for my main project. These 2 projects are in the same solution. I add a reference from my main project to my Control project. I notice that the first time after I drag my Control from the Tool Panel onto my main winform, an assembly folder was generated at the C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies, and the folder name is something like "jlebh-py01".
The first build is always OK, but after I rebuild my Control class or whole solution, a new assembly folder will be generated at C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies, and then problem arises, my Control fails to behave well because Visual Studio says that the two types "originates from different location". The error message is as below:
[A]MyControl.TypeXXX cannot be cast to
[B]MyControl.TypeXXX. Type A orginates
from assemblyXXX at location
'C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies\jlebh-py01\MyControl.dll'
Type B originats from assemblyXXX at
location
'C:\Users\XXX\AppData\Local\Microsoft\VisualStudio\9.0\ProjectAssemblies\ue4i-z3j01\MyControl.dll'
If I reference the Control DLL directly instead of through project reference, or never rebuild the Control project after use my Control in the main project, things seem to be OK.
Does anyone knows why? Is it the proper way to develop a control and a main project within the same solution?
From what you explained - it seems that the main project in your solution is not updating the reference to the control library that you have. I have been working in ASP.NET for a couple of years at least, and have had similar problems with referenced assemblies, but there was always a very simple fix to it - Rebuild the main project. This should clean it, and then run a fresh build.
Another thing you can try is add a variable assembly version to your control. In the project properties, assembly information, try set the version number to 1.0.* This will force the last two version numbers be based on the day and the time of the day, and each time you rebuild the control- it will have a different version. As long as the main project keeps the version updated - which it should - there shouldn't be any problems..

ASMX page returning old Web Methods

I must be dumb (and i m sure i am making mistake but cant figure out at the moment)
Actually i deployed ASMX page containing reference to a Class Library which contains few web methods. It exposes those methods when i browse the site. But some how it is not showing me any further methods which i have added in it. The DLL is strong name assembly.
any clue plz?
I tried re adding the reference but there is no difference.
It worked but i dont know weather the files are not updated or references, so what i did, i removed the referenced ,deleted the files from bin and GAC and then re-added the files.After rebuilding , i deployed them again and it worked.
You need to update the web reference that you added to your project. If the class library contains the web reference, update the reference in it and then re-add/update your project's reference to the class library. If you don't update, then the only stubs/methods that your project will know about are the ones created when the web reference was first added.
Or is it something about GAC. If your class library assembly lies in GAC as an old version it won't be updated.

Categories

Resources