asp.net 5 project targeting dnx46 wont build - asp.net-core

I have a solution which contains an asp.net 5 app, a code library, and a test library. The libraries are also the new project.json style. I am not really sure how to refer to these libraries to distinguish them from the existing library project structure.
Anyway, all three projects were targeting dnx451/net451. I decided to try targeting dnx46/net46. The libraries build, but the asp.net dnx app fails. The error is:
The design time host build failed with the following error: Object reference not set to an instance of an object.
It is referencing the file Microsoft.DNX.targets at line 166.
There is another error that just says, "Object reference not set to an instance of the object." It says line 1, but doesn't reference a file.
To test I created a new solution with and empty asp.net 5 project and 2 class libraries. I changed dnx451 to dnx46, and net451 to net46, and it compiles fine. I am not sure what in my existing project is causing this error, or where to start looking.
By the way, dnx46/net46 are the only frameworks targeted. I have removed the core frameworks.

What a load of trial and error. Turns out the issue was the razor precompilation module as described here, http://davidzych.com/view-compilation-in-aspnet-5-with-the-razorprecompilemodule/.
You can read the github issue here, https://github.com/aspnet/dnx/issues/3003.
I first tried to comment out the precompilation class. However just that fact that the file was there, made dnx throw the error. I removed the precomilation file and now it compiles fine.
EDIT:
Alternatively you can add dnx451 back into your project, along with dnx46 and then the precomilation module will work fine.

Related

Visual Studio Code DotNet Core Project "ProjectName.dll" does not exist

I received new project file. Its actually .NET Core Worker Service Project. Created using VS Code. I tried to Manually build the service. but in there no have any dll file. because I only changed the project name after its received. Now I tried to run my project. but I got error. its says
launch: program 'C:My Folders\Glm'
Projects\DBDataService\bin\Debug\netcoreapp5.0\win7-x64\'DBDataService.dll' does not exist
Can you please tell me how to setup my local environment with dll file in VS Code. I am new to VS code actually. How to manually setup dll file in project
VSCode was looking for the DBDataService.dll program, but could not find it.
You only need to configure your environment and then correctly define everything (especially "programs") in your launch.json.
Try to change,
"program": "${file}"
${file} is a predefined variable in VS Code for the current opened file. Refer to:
https://code.visualstudio.com/docs/editor/variables-reference
You can also refer to this post, which may help you:
C# - VS Code - launch:program ... does not exist
Create a debugging file:
https://code.visualstudio.com/docs/editor/debugging

(System.IO.FileNotFoundException) .Net core Web API cannot find child dependency (Dependencies of dependency) when added DLL by "Add Reference"

I am getting System.IO.FileNotFoundException in my .Net Core Web API. So I've set up the below project to demonstrate the problem.
I created a.Net Standard library named DemoLibrary and added QRCoder dependency via NuGet.
Disclaimer: Reason for choosing the QRCoder is that the Web API doesn't use it by default. I don't use it in my project. In fact, I'm getting this exception for EntityFrameworkCore.
I created a new .Net Core Web API DemoWebAPI which has no other dependencies.
Then added the DemoLibrary to DemoWebAPI via Add Reference -> Browse -> DemoLibrary.dll.
This is my solution:
The DemoMethod method in Calculate class just creates the object of QRCodeGenerator.
public class Calculate
{
public static string DemoMethod()
{
QRCodeGenerator qrGenerator = new QRCodeGenerator();
return "";
}
}
And my ValuesController in DemoWebAPI just calls the method:
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2", DemoLibrary.Calculate.DemoMethod() };
}
Now, when I run the DemoWebAPI project I get below exception upon the call to the DemoMethod:
System.IO.FileNotFoundException: 'Could not load file or assembly 'QRCoder, Version=1.3.5.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.'
I understand the fact that I have to copy the QRCoder.dll file somewhere. But I fail to understand where to put it. I've already tried putting it in "bin/debug/netcoreapp2.2" of the DemoWebAPI and "bin/debug/netstandard2.0" of the DemoLibrary.
But I couldn't get it working.
Request: Please post your answer as descriptive as you can because I am new to .Net Core.
Edit:
I am aware of the NuGet servers. I have read topics like hosting a NuGet server in IIS and Azure. The reason behind DLL reference is I want to use my DLLs in two projects one of them is a .net core API and the other is .net framework class library which is compiled by NMAKE. I couldn't find any way to restore NuGet packages in the .MAK files.
It looks like you've merely added the DLL for DemoLibrary to your DemoWebApi project. That's not how you should be adding references. Since these are in the same solution, you should add a project reference. That will fix your issue.
Now, let me explain what's actually going on here. Your DemoLibrary has a dependency on QRCoder. It's a NuGet reference, which means that package will be restored (i.e. downloaded) and included in your DemoLibrary build output. However, it will be included as one or more DLLs along side your DemoLibrary.dll. When you then just reference DemoLibrary.dll, you're missing all these other DLLs that are part of DemoLibrary and thus, things don't work properly.
Now, when it comes to a project reference, things are little more complex. A project reference essentially wraps the referenced project into your other project. You can think of it as sort of a sub project. For all intents and purposes, it's like any dependency of the sub project becomes a dependency of the main project. That means that DemoWebAPI now technically has a NuGet package reference to QRCoder even though there's no explicit package reference in its project file. The dependency comes from your DemoLibary project. As such, with a project reference, all the necessary dependencies will be included, because it's as if the main project included those itself, by way of the sub project.
For what it's worth, you should virtually never include a DLL as a reference directly. That used to be required, but the concept of NuGet packages has all but eliminated the practice. Even if DemoLibrary was not in the same solution as DemoWebAPI (meaning you could no longer do a project reference), the correct way to use it would be to turn DemoLibary into a NuGet package, and then reference it in DemoWebAPI via a package reference, like any other NuGet package. You do not simply add the DLL.

Reference a csproj from same solution as xproj

I have a solution with the following projects:
MySolution.sln
- MySolution.Client.csproj
- MySolution.Service.csproj
- MySolution.Models.csproj
- MySolution.Server.xproj
MySolution.Models is a simple class library which contains shared code that is referenced by MySolution.Client and MySolution.Service - and I would like to reference it in MySolution.Server.
The GUI in VS 2015 RC1 lets me add the reference by right clicking References -> Add Reference. I then see all my projects under Projects -> Solution.
I select MySolution.Models and click Ok, after which I receive the following error in the output log:
Errors in ...PathToSolution\MySolution.Server\project.json
Unable to locate MySolution.Models >= 1.0.0-*
It really feels like this should work, since the GUI allows me to add the reference without any hiccups.
So the first thing to understand is DNX projects have no understanding of traditional .net projects. They don't read or parse csproj files. This is done to keep them cross platform and cross IDE compatible (csproj is a distinctly windows and VS specific thing).
When you add a reference to a "legacy" (I use legacy to mean a .net 4.x csproj based project) behind the scenes the IDE will run dnu wrap but it looks like in your case something broke.
The following should be done automatically.
In solution root global.json a folder "wrap" should be added to the
projects property.
A folder off the root named "wrap" will be created if it doesn't exist.
A /wrap/project.json will be created/updated with a path to the assembly (dll).
Add a reference to the assembly and version to the referencing project's project.json file.
So first thing to check is make sure you have a "wrap" folder and wrap reference in projects property of solution.json. If you don't then likely something "broke". Try removing the reference rebuilding and adding the reference back. Check the build output window for any errors (VS is still RC so there are something error which probably should be halting that are not).
Look for a project.json in the wrap folder. It should look something like this:
{
"version": "1.0.0-*",
"frameworks": {
"net452": {
"wrappedProject": "../../LegacyClassLibrary/LegacyClassLibrary.csproj",
"bin": {
"assembly": "../../LegacyClassLibrary/obj/{configuration}/LegacyClassLibrary.dll",
"pdb": "../../LegacyClassLibrary/obj/{configuration}/LegacyClassLibrary.pdb"
}
}
}
}
Note the framework version. If there is a mismatch then it will fail resolving the dependencies. For example if your MySolution.Models targets .Net 4.6 and thus when wrapped has a dnx46 framework reference but your MySolution.Server project has a reference to dnx452 (in the project.json for MySolution.Server) then it will fail when resolving the dependency to MySolution.Models.
The you quoted could probably be improved. It means that it could not resolve the dependency due to one of the following reasons
It could not find a MySolution.Models assembly (either source code or compiled dll) based on the paths it uses (starting from projects parameter in global.json).
It found a MySolution.Models assembly (either source code or compiled) BUT it was an invalid version. Check version in Models project vs the reference in Server project.json.
It found a MySolution.Models assembly but it can't resolve framework dependencies (i.e. Models requires dnx46 but Server only targets dnx452).
In my experience the third one if the most common. For the DNX templates in VS 2015 RC the default full framework being targeted is dnx452 (or is it dnx451?). New csproj projects will be 4.6 (dnx46) by default and existing projects could be just about anything.
An alternative solution:
I have found the following alternative to result in easier dependency management. If MySolution.Models will only be used by DNX projects then just convert it to a DNX project move it into the source folder and reference it directly. It will be part of the source compilation and you gain the benefits of dynamic compilation.
If MySolution.Models will be referenced by both DNX and legacy (csproj) projects then you can create a side-by-side xproj and project.json files for Models. They will be ignored by the legacy project. In essence you have both a legacy and DNX project using the same source files. You can then just like above reference it directly. Keep in mind the folder structure if the models folder is not under /src (and it probably isn't if this was an existing project) then you will either need to move it or add a reference to the folder in global.json. That sounded more confusing that it really is. Just keep in mind for a DNX project the global.json defines the relative paths to where DNX can find source code. The DNX also can resolve dependencies by nuget or searching the GAC but that is beyond what you are trying to do.

Cannot create controller in VS 2012 MVC 4.0

When I create new project, create a new control have no problem. But when I'm using TFS in Visual Studios 2012 to get the latest code. All code is update, I cannot create new controller. This is my alert error from VS
could not load file or assembly'System.web.mvc, vesion = 3.0.0.0,culture = neutral, pulbickeytoken = 31bf3856ad364e35' or more of its this system cannot find the file specified.
Looks like you have a missing reference to System.Web.Mvc. Got to the "References" part of your project and add the missing reference.
Alternatively you could add the relevant System.Web.Mvc.Extensions Mvc 4 NuGet package which should add all references that your solution might be missing.
There is a discrepancy in your question between the error and your tag of MVC-4. So assuming you are aiming to use MVC 4.
In Visual Studio, go to the Package Manager Console via Tools -> Library Package Manager -> Package Manager Console. and run the following commands with the source : nuget.org as answered by Shiva in a Entity Framework question.
Edit:
Uninstall the previous version of MVC 3.0 and all references to it.
Uninstall-Package Microsoft.AspNet.Mv -Force
Upgrade to MVC 4.0 to match the tag you've added to the
question
Install-Package Microsoft.AspNet.Mvc -Version 4.0.30506
Check that Entity Framework is installed. Check the project file (*.csproj, *.vbproj) references by Right clicking the project file and opening it in a text editor such as notepad++.
Rebuild or Clean (optional) your project. When you have a full compile of the project such as getting the code from TFS, it will check all the references and throw this exception. When you compile the project again this error will most likely not appear as you are not doing a full compile.
Check the TFS version of the project file for any discrepancies.
I got the same problem, the cause was a "Security Update". After this update Project references to System.Web.Mvc.dll are no longer resolved because the assembly version of System.Web.Mvc.dll was incremented.
There are two ways to solve this issue, by installing a nuguet package or by manually updating the reference to System.Web.MVC.dll (don’t use the one in the GAC)
This blog post contains a detailed explanation:
http://blogs.msdn.com/b/webdev/archive/2014/10/16/microsoft-asp-net-mvc-security-update-broke-my-build.aspx

Derived types must either match the security accessibility of the base type or be less accessible

I upgraded my MVC 4 app to MVC 5 a couple of days ago following these instructions and now I'm getting the following error. I updated DotNetOpenAuth to the latest bits using Nuget (v4.3.3.13295) but it still throws this error.
How do I fix this?
Inheritance security rules violated by type:
'DotNetOpenAuth.Messaging.OutgoingWebResponseActionResult'. Derived
types must either match the security accessibility of the base type or
be less accessible.
Solved this finally. Turned out that I needed to make some changes to the source code of DotNetOpenAuth and re-compile it. This wasn’t easy at all since the source code won’t compile after downloading from github. I had to spend ~3 days trying various things and learning the build system meshed into DotNetOpenAuth’s project files until I finally got it to compile. Seems the author of this project has abandoned it. See more about this issue here.
Download the 4.3 code base using this command line: git clone -b v4.3 https://github.com/DotNetOpenAuth/DotNetOpenAuth.git
Edit the /src/version.txt and change it to 4.4.0. This makes this version higher than the official Nuget release so that installing Nuget packages don’t attempt to install old versions of DotNetOpenAuth assemblies from its repository.
Remove all instances of the following string from all AssemblyInfo.cs files under the /src/ directory.
[assembly: AllowPartiallyTrustedCallers]
I had to mess around with the Microsoft.Contracts reference in two projects and point it to /tools/Contracts/PublicAssemblies/v3.5/Microsoft.Contracts.dll and then use a using alias to get classes from this assembly to resolve properly. A few Requires.NotNull() lines refused to compile so I just commented them out… c'est la vie..
Run the following command to skip verification of these assemblies: sn.exe -Vr *,2780ccd10d57b246
Once the assemblies were built I referenced those, re-added Microsoft.AspNet.WebPages.OAuth 3.0 from Nuget, rebuilt my solution, and finally my app is up and running on MVC 5.