How to reference csproj from kproj - c#

I was playing around with VS2015 and ASP.NET vNext, and got stuck on trying to add a reference from vNext class library (kproj) to a regular class library (csproj) in the same solution. Visual Studio 2015 shows the following error message:
"The following projects are not supported as references".
Is it possible at all to add references to csproj from vNext class libraries?

Note: The kpm command has been replaced by dnu.
Visual Studio 2015 Preview (as of writing this) comes with the ASP.NET 5 stable release beta1. In this version there is no way to reference a csproj project from an ASP.NET 5 project.
However, on the development feed of ASP.NET 5 the command kpm wrap was introduced to support referencing csproj-projects from ASP.NET 5 projects. See the github issue #827 in the aspnet/KRuntime repository and pull request #875 which closes the issue.
Here is an example how you would use kpm wrap:
Make sure the newest version of the KRuntime is installed (check this with the kvm list command) (I tested this with version 1.0.0-beta2-10709).
Create an ASP.NET 5 class library project, I used the name ClassLibrary1.
Create a "normal" csproj class library, I named this ClassLibrary2 (make sure you put this in the src folder).
From the commandline, from the solutiondirectory run the command
kpm wrap .\src\ClassLibrary2
This gives the output:
Wrapping project 'ClassLibrary2' for '.NETFramework,Version=v4.5'
Source C:\Users\andersns\Source\ClassLibrary1\src\ClassLibrary2\ClassLibrary2.csproj
Target C:\Users\andersns\Source\ClassLibrary1\wrap\ClassLibrary2\project.json
Adding bin paths for '.NETFramework,Version=v4.5'
Assembly: ../../src/ClassLibrary2/obj/debug/ClassLibrary2.dll
Pdb: ../../src/ClassLibrary2/obj/debug/ClassLibrary2.pdb
Now in the project.json of ClassLibrary1 (which is ASP.NET 5) you can add a reference to ClassLibrary2 with this:
...
"dependencies": {
"ClassLibrary2": ""
},
...
Note: kpm wrap did not run properly for me with cmd, I needed to launch powershell to make it run.

Starting with (Visual Studio 2015 RC) the kpm command has been replaced by dnu
The dnu command stands for (.NET Development Utility)
dnu wrap .\src\ClassLibrary2\ClassLibrary2.csproj
New ASP.NET Features and Fixes in Visual Studio 2015 RC
http://blogs.msdn.com/b/webdev/archive/2015/04/29/new-asp-net-features-and-fixes-in-visual-studio-2015-rc.aspx

I have found it easiest to simply create a corresponding .kproj for the .csproj I want to reference. The .kproj does not require listing every included file, so this is rather straightforward.
You can create YourProject.kproj as a text file with the contents below, and only replace the [REPLACE_WITH_UNIQUE_GUID] and [ROOT_NAMESPACE].
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>[REPLACE_WITH_UNIQUE_GUID]</ProjectGuid>
<RootNamespace>[ROOT_NAMESPACE]</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<ProduceOutputsOnBuild>True</ProduceOutputsOnBuild>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
You can then add this project to your solution and reference it from your MVC 6 project.

In addition to this answer I found out that you need to use the if directive (#if) to make the call without errors:
Something like:
#if ASPNET50
using class2
#endif
When you use it in a call you need to do the same.
#if ASPNET50
ViewBag.Message = class2.Class1.Greetings()
#endif

Related

error CS8773: "Feature 'global using directive' is not available in C# 9.0" after downgrade from net6.0 to net5.0

I have a project that was initially created for .NET 6 but then I needed to downgrade it to .NET 5.
I changed Target framework in Project Properties and tried to compile. As a result I received a bunch of the errors:
GlobalUsings.g.cs(2,1,2,29): error CS8773: Feature 'global using directive' is not available in C# 9.0. Please use language version 10.0 or greater.
File GlobalUsings.g.cs is created automatically and it reappears every time after compilation.
Finally I found that the reason is an extra property ImplicitUsings in the project file that is not supported by .net 5.0.
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
I needed to remove ImplicitUsings from the file.
remove <ImplicitUsings>enable</ImplicitUsings> in the csproj project file, then can build success
find this solution from here
Remove the tag indeed work.
But just change the value of it did the trick as well!
<ImplicitUsings>disable</ImplicitUsings>
To get rid of this error which is caused by downgrading below net6.0.
Remove the following items from the .csproj file:
<ImplicitUsings>
<Using Include="..." />
If you don't want to remove the ImplicitUsings or made changes to the project file. You can tell the build cli to disable it during build process by
dotnet build --configuration "Release" --framework "net5.0" /p:ImplicitUsings=false /p:PublishReadyToRun=false

C# Visual Studio 2019, Meta Numeric, Error: Could not load file or assembly

Visual Studio 2019
C#
Project 1:
Dependencies: Meta.Numerics 4.1.4. I added Meta Numerics via the Manage NuGet Packages for Solution
Project 1 is a Class Library
Will be compiled and .dll will be shared with an associate who will use it in the main application
Project 1 builds just fine using Debug.
using System;
using Meta.Numerics;
namespace LeakDetection
{
public class LeakDetectionOperations
{
public LeakDetectionOperations(int co = 24)
{ }
public int leakCheck()
{
double result = ComplexMath.Abs(10);
return 0;
}
}
}
Project 2
Dependencies: Project 1. Imported via Add references, browser, and selected the .dll from project 1
Project 2 is just a simple test project that I'm using to test the .dll object.
It runs, but throws an exception when it attempts to call the ABS function of Meta.Numerics.
using System;
using LeakDetection;
namespace LeakTest
{
class Program
{
static void Main(string[] args)
{
LeakDetectionOperations obj = new LeakDetectionOperations();
int ret;
ret = obj.leakCheck();
Console.WriteLine("Hello World!");
}
}
}
I've followed the instructions from the Meta.Numeric gitrepo regarding installation. The installation was done as they suggested to install the package. I've also cleaned build, and rebuilt fresh. I also changed from debug to release to see if there was anything related to the debug that was causing the error. As you can see at the above code, its fairly minimum, as this is not my actual code. Its a bit more elaborate, but rather than posting the full code, this is the minimum usable code that replicates the issue I'm having. Nothing from the Meta.numeric library is usable.
I usually work in Python, have some experience in C and C++, but I have used make files to compile in linux. Using C#, visual studio is fresh for me.
Any suggestions as to where I should look would be much appreciated.
UPDATE:
Per the suggestion by #kit, I've included the .csproj file for project 1 below
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Leak_Detection</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Meta.Numerics" Version="4.1.4" />
</ItemGroup>
</Project>
.csproj for project 2
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="Leak Detection">
<HintPath>..\..\Leak Detection\Leak Detection\bin\Debug\netcoreapp3.1\Leak Detection.dll</HintPath>
</Reference>
</ItemGroup>
</Project>
I was able to kind of replicate your issue (not exactly but I got the same kind of error). I believe your test project is in .NET framework. If I do not reference any nuget package in test project, failure occurs. But, if I install any nuget package and later on uninstall it, program runs successfully. For testing, I installed and uninstalled Newtonsoft.Json package. So, there is a bug in initialization of nuget package engine for .NET framework where in absence of any nuget packages, it is not able to resolve transitive nuget dependencies. If I create test project in .NET core, test project runs without any issue.
I have also put code in github
https://github.com/dheerajjain11/MissingDLLIssue/
MetaNumerics is .NET Standard library
MetaNumericsTest is .NET Framework Test project where I installed and uninstalled nuget package. Now, it runs successfully
MetaNumerics2 is .NET Framework Test project which fails
MetaCore is dotnet core project which runs without any issue and no workarounds
I don't know if this is normal or not, but what resolved the issue for me was installing the package via the Nuget package manager on both the Library and the Console application. Both builds need a reference.
What I was doing previously, was only installing the package for the library build. Seeing how the console application was then compiling with the .dll being referenced and called, the calling application also needed the package installed.

dotnet core 3.1 standalone app fails with "No such file or directory", Am I missing a dependency?

I'm just trying to get dotnet core running on an NVidia Jetson Nano.
I've created a simple "hello world" app in dotnet core and packaged it as a stand-alone app targeting linux-arm.
When I put it on my Synology NAS, I can navigate to the publish directly and type ./HelloDotNetCore and the app runs, albeit with a few errors.
/HelloDotNetCore/HelloDotNetCore/bin/Release/netcoreapp3.1/linux-arm$ ./HelloDotNetCore
./HelloDotNetCore: /lib/libstdc++.so.6: no version information available (required by ./HelloDotNetCore)
./HelloDotNetCore: /lib/libstdc++.so.6: no version information available (required by ./HelloDotNetCore)
./HelloDotNetCore: /lib/libstdc++.so.6: no version information available (required by ./HelloDotNetCore)
./HelloDotNetCore: /lib/libstdc++.so.6: no version information available (required by ./HelloDotNetCore)
./HelloDotNetCore: /lib/libstdc++.so.6: no version information available (required by ./HelloDotNetCore)
Hello World!
I can run it on my Raspberry Pi, as sudo
/HelloDotNetCore/HelloDotNetCore/bin/Release/netcoreapp3.1/linux-arm $ sudo ./HelloDotNetCore
Hello World!
I've "installed" dotnet core by following the tutorial here:
https://blog.headforcloud.com/2019/04/03/jetson-nano-a-quick-start-with-.net-core-3/
(it's not actually an install, just exposing the binary to bash)
/code/HelloDotNetCore/HelloDotNetCore$ dotnet run
Hello World!
However, attempting to run this as a stand-alone app on my NVidia Jetson results in "No such file or directory". I've tried the old obvious chmod +x and chmod 777 tricks along with running as sudo, but there's no other clue as to what it's looking for that isn't there.
/code/HelloDotNetCore/HelloDotNetCore/bin/Release/netcoreapp3.1/linux-arm$ ./HelloDotNetCore
-bash: ./HelloDotNetCore: No such file or directory
So it seems that something that should be packaged with this stand-alone app isn't there, but I'm lost as for how to figure out what it needs. Any ideas?
I found the culprit. The runtime for the NVidia Jetson needs to be explicitly set to linux-arm64, and not linux-arm. If you run the application from a Jetson using the dotnet command
dotnet run
it will compile the application into the associated debug or release folder and then you can run it from that folder using
./HelloDotNetCore
However, in order to "publish" the app from visual studio, I had to update my Microsoft.NETCore.Platforms package via NuGet from here
https://www.nuget.org/packages/Microsoft.NETCore.Platforms/
This automatically updated my .csproj file to
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.Platforms" Version="3.1.3" />
</ItemGroup>
</Project>
Then manually alter the RuntimeIdentifier element of the .pubxml file to reflect the linux-arm64 architecture.
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>bin\Release\netcoreapp3.1\publish\</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifier>linux-arm64</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>True</PublishSingleFile>
<PublishTrimmed>False</PublishTrimmed>
</PropertyGroup>
</Project>
I was then able to publish the app using the publish command in Visual Studio, which built a stand-alone application inside a folder called 'publish'
Now, I get the expected result.
Jetson:/code/publish$ ./HelloDotNetCore
Hello World!

Custom .NET Standard Nuget package not installing correctly

I have a .NET Standard class library project with a number of POCO's. This project is built using TeamCity and published as a Nuget package using the built-in Nuget server.
The problem I'm having is when it's installed into my solution with a number of .NET Framework class library projects and ASP.NET MVC and Web API projects (set to .NET Framework 4.7.1), it seems to be stuck on an older version and is not recognising any new classes or methods I add to the project - e.g. NewMethod1()
Project File for Nuget package
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461;net462;net47</TargetFrameworks>
<Version>1.0.0</Version>
</PropertyGroup>
<PropertyGroup>
<NetStandardImplicitPackageVersion>2.0.0</NetStandardImplicitPackageVersion>
<Description>Standard entities used within our systems</Description>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<FileVersion>1.0.0.0</FileVersion>
<Authors>Company X</Authors>
<Company>Company X</Company>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<OutputPath>bin\Release</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath>bin\Debug</OutputPath>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>
</Project>
TeamCity is using the 'dotnet' restore, build & pack options.
The package is stored in my Nuget cache located in C:\Users\antho.nuget\packages. When I use Object Browser to inspect the dll, it contains the new classes and methods (e.g. NewMethod1()).
When I install this package into my .NET Framework solution, no errors occur during the installation. If I try and use the new method - NewMethod1() - the code doesn't compile.
If I create a brand new solution and ASP.NET MVC project and install the package, the new method can be used in code and it compiles successfully.
What could be causing the new version not to be installed correctly? It's tricky to provide a sample reproducing the issue because it seems to work in a new project.
Update
If I add a new project to the solution and install the Nuget package, it gets the latest version.
Project A
<PackageReference Include="AutoGuru.Shared.Utilities" Version="1.0.369" />
public class Class1
{
public void Test()
{
"dfdfdfdf".SanitizeVehicleRego();
}
}
Project B
<PackageReference Include="AutoGuru.Shared.Utilities">
<Version>1.0.369</Version>
</PackageReference>
public class Class1
{
public void Test()
{
"fdfdf".SanitizeVehicleRego();
}
}
Project B compiles successfully and Project A doesn't. SanitizeVehicleRego() is a string extension method in the AutoGuru.Shared.Utilities package.
My answer it quite big, so I add an answer instead of comment.
Step 1
Check the output when you restore the package, sometimes dotnet restore resolve an other version automatically ( you should have a warning for this kind of things in your console ).
Step 2
If any information was found in Step 1. Try to clean all your local nuget cache.
dotnet nuget locals all --clear
I'm not sure if local nuget packages are under cache stategy. But if they are, it should resolve the correct version of your package.

How to publish for all target frameworks

I am building a set of build template in TeamCity for .Net Core projects. Everything is working great, except for console projects. The problem is that when I go to publish the solution, I need to specify the framework version. At no other point in the build do I need to know the framework. At least this is true when publishing .sln files, with a console project that only has a single framework targeted.
So now I am in a situation where I need to figure out what framework the console project should target. I could read various XML files, but I'm hoping I don't need to. Is there some builtin way that I can query for the frameworks in use for a given solution?
For example, something like (PowerShell)
$frameworks = & dotnet.exe --<what I want> .\MySolution.sln
for ($framework in $frameworks) {
& dotnet.exe publish -f $framework .\MySolution.sln
}
That way I don't need to modify the build system every time a new framework is in use. I've poked around in the CLI repo, but I can't find a command that does what I need. Is opening .csproj files my only hope?
If you want to publish projects that target multiple frameworks, the default Publish target fails, but you can create a custom target that performs the multi-targeting itself. To do this, create a file named Directory.Build.props in the solution folder (with MSBuild > 15.1 this can and should be named Directory.Build.targets because there was a bug with multi-targeting projects):
<Project>
<Target Name="PublishProjectIfFrameworkSet"
DependsOnTargets="Publish"
Condition=" '$(TargetFramework)' != '' " />
<Target Name="PublishProjectForAllFrameworksIfFrameworkUnset" Condition=" '$(TargetFramework)' == '' ">
<ItemGroup>
<_PublishFramework Include="$(TargetFrameworks)" />
</ItemGroup>
<MSBuild Projects="$(MSBuildProjectFile)" Targets="Publish" Properties="TargetFramework=%(_PublishFramework.Identity)" />
</Target>
<Target Name="PublishAll"
DependsOnTargets="PublishProjectIfFrameworkSet;PublishProjectForAllFrameworksIfFrameworkUnset" />
</Project>
Then you can publish the projects for all defined frameworks by executing this in the solution directory:
$ dotnet msbuild /t:PublishAll /p:Configuration=Release
Microsoft (R) Build Engine version 15.1.1012.6693
Copyright (C) Microsoft Corporation. All rights reserved.
app2 -> /Users/martin/testproj/app2/bin/Release/netcoreapp1.1/app2.dll
app1 -> /Users/martin/testproj/app1/bin/Release/netcoreapp1.1/app1.dll
app1 -> /Users/martin/testproj/app1/bin/Release/netcoreapp1.0/app1.dll

Categories