Application won't run without runtimeconfig.dev.json in the directory? - c#

I'm trying to build an application for release. However, no matter what I do, it needs to have the runtimeconfig.dev.json in the directory, otherwise it won't even run, I get no errors when it does this.
I've tried compiling it so it will produce a single file but then it doesn't work at all even when runtimeconfig.dev.json is included in the directory.
Whilst trying to publish through the interface in Visual Studio the parameters are as followed:
Configuration: release
Target Framework: netcoreapp3.1
Target Runtime: win-x64
Also using the dotnet publish -c Release command, this still results in a file dependent on runtime.dev.json
I've also tested the output of the publish on another computer and it just doesn't run at all without any error. Even with the runtimeconfig.dev.json In the directory. Even after changing the framework from 3.1 to 5.0 and then back, it still doesn't work on another computer. I don't understand why as the program works perfectly fine when I run it via visual studio with both the debug and release configurations it's when I'm trying to do it independently is when there is problems.
Here is what my .csproj file looks like.
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<UseWPF>true</UseWPF>
<ApplicationIcon>Starter-Edit-Icon.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DocumentationFile></DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Properties\DataSources\**" />
<EmbeddedResource Remove="Properties\DataSources\**" />
<None Remove="Properties\DataSources\**" />
<Page Remove="Properties\DataSources\**" />
</ItemGroup>
<ItemGroup>
<None Remove="Starter-Edit-Icon.png" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Costura.Fody" Version="5.7.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Extended.Wpf.Toolkit" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<Resource Include="Starter-Edit-Icon.png" />
</ItemGroup>
</Project>
This is what the runtimeconfig.dev.json file contains
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\MyUserName\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\MyUserName\\.nuget\\packages"
]
}
}

I fixed this issue by removing the unused frameworks from my dependencies, I also uninstalled any NuGet packages that was unused which happened to be both of them I had imported anyway.

Related

Roslyn generator fails to load referenced assembly present in nuget packages

In my VS 2022 solution I have two projects, a roslyn generator and a console app that I use to test the generator and other stuff.
Whenever I try to build the test app I get this warning:
CS8784: Generator 'Generator' failed to initialize. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly 'Microsoft.SqlServer.ConnectionInfo, Version=16.200.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91' or one of its dependencies. The system cannot find the file specified.'
So it seems that I'm missing an assembly.
But actually Microsoft.SqlServer.ConnectionInfo.dll is present in my nuget package directory (under .nuget\packages\microsoft.sqlserver.sqlmanagementobjects\161.47021.0\lib\netstandard2.0, netcoreapp3.1 and net462) and has the right version for all three frameworks.
I also tried to reference assembly path in visual studio, and it didn't give a different result.
Whatever program is running the generator don't seem to be able to resolve it.
Moreover, my test app references the exact the exact same packages and run without any problem.
My guess is that running the generator manually (with my own entrypoint) would allow me to see where it tries to find the assemblies, but I have no idea how to do that with a generator.
Also one of the package gives me this warning for the generator but not the test app:
NU1701: Package 'Microsoft.SqlServer.Management.SmoMetadataProvider 161.47021.0' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project.
I would like to know how I can make my generator work, or at least, have some way to debug it to get more information on why it fails to load the assembly.
Here are the .csproj for the generator:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
<IsRoslynComponent>true</IsRoslynComponent>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.SqlServer.Management.SmoMetadataProvider" Version="161.47021.0" />
<PackageReference Include="Microsoft.SqlServer.Management.SqlParser" Version="160.22506.0" />
</ItemGroup>
<ItemGroup>
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
</ItemGroup>
</Project>
and for the test console app:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.Management.SmoMetadataProvider" Version="161.47021.0" />
<PackageReference Include="Microsoft.SqlServer.Management.SqlParser" Version="160.22506.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\sql_modeler.generator\sql_modeler.generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\sql_modeler\sql_modeler.csproj" />
</ItemGroup>
</Project>

get error Assets file 'obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v2.2'

I upgrade my MVC Core Project from 2.2 to 3.0 with
microsoft
and change many recommended here: https://stackoverflow.com/
It works fine when run it in Local, but when I want publish in local folder I get this error :
Assets file 'obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v2.2'. Ensure that restore has run and that you have included 'netcoreapp2.2' in the TargetFrameworks for your project
Ii have 3 projects and all of them upgrade to MVC core 3.0 also upgrade all packages to 3.0
also remove object folder and bin folder and build projects again, close VS and open it again but the error stil exists.
UPDATE: mvc project csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<!--<PackageReference Include="Microsoft.AspNetCore.App" />-->
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0"
/>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design"
Version="3.0.0" />
</ItemGroup>
<ItemGroup>
<Folder Include="Areas\Admin\Data\" />
<Folder Include="Areas\Admin\Models\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\project.Model\project.Model.csproj" />
<ProjectReference Include="..\project.Repo\project.Repo.csproj" />
</ItemGroup>
</Project>
My project.Model.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference
Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design"
Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers;
buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer"
Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design"
Version="1.1.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools"
Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers;
buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
In our case we had a very similar error when Publishing, after switching from netcoreapp30 to netcoreapp31 as the target Framework. We solved it by:
Closing Visual Studio
Deleting the file \obj\project.assets.json
Opening the solution again
Rebuild Solution
After that we were able to Publish the project fine.
Make sure your Publish Profile says netcoreapp3.0 for the TargetFramework.
In your Package Manager Console run the following command:
dotnet restore SolutionName.sln
This happens usually when you upgrade the .net core version.
The solution is to create new publish profile. Not edit, simple create new publish profile and target the new .net core version that you have upgraded.
If you are doing this in a lambda then make sure to update aws-lambda-tools-defaults.json and serverless.template files.
If you run publish with the command line (dotnet publish), make sure you specified the correct framework with option "-f".

RunCommand property not defined on WPF app migrated to new csproj format

I am trying to migrate a WPF app built under the old csproj format to the new csproj format defined for VS2017.
I've been able to get the app to compile, but I when I try to launch it in the debugger under VS2017 I get the following error message:
Unable to run your project. The "RunCommand" property is not defined.
Interestingly, if I double-click the exe within File Explorer it launches just fine.
FYI, the project was initially a console app, which I then modified to be a WPF app. Here's the csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LanguageTargets>$(MSBuildExtensionsPath)\$(VisualStudioVersion)\Bin\Microsoft.CSharp.targets</LanguageTargets>
<OutputType>winexe</OutputType>
<TargetFramework>net47</TargetFramework>
<ApplicationIcon />
<OutputTypeEx>winexe</OutputTypeEx>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx" Generator="ResXFileCodeGenerator" LastGenOutput="Resources.Designer.cs" />
<Compile Update="Properties\Resources.Designer.cs" DesignTime="True" AutoGen="True" DependentUpon="Resources.resx" />
<Compile Update="Settings.Designer.cs" AutoGen="True" DependentUpon="Settings.settings" />
<None Update="Settings.settings" LastGenOutput="Settings.Designer.cs" Generator="SettingsSingleFileGenerator" />
<Page Include="**\*.xaml" SubType="Designer" Generator="MSBuild:Compile" Exclude="App.xaml" />
<Compile Update="**\*.xaml.cs" SubType="Designer" DependentUpon="%(Filename)" />
<Resource Include="assets\*.*" />
<ApplicationDefinition Include="App.xaml">
<Generator>MsBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac" Version="4.6.0" />
<PackageReference Include="Autofac.Extras.CommonServiceLocator" Version="4.0.0" />
<PackageReference Include="Extended.Wpf.Toolkit" Version="3.0.0" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.0.8" />
<PackageReference Include="MaterialDesignColors" Version="1.1.3" />
<PackageReference Include="MaterialDesignThemes" Version="2.3.0.823" />
<PackageReference Include="MvvmLightLibs" Version="5.3.0" />
<PackageReference Include="Serilog" Version="2.4.0" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\WPFUtilities\J4JUI\J4JUI.csproj" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.ComponentModel.DataAnnotations" />
</ItemGroup>
<Import Project="$(MSBuildSDKExtrasTargets)" Condition="Exists('$(MSBuildSDKExtrasTargets)')" />
</Project>
Where is the RunCommand property set, and how do I set it?
Update
Playing around with the project settings, I configured the debug options to launch the executable created by the project (the default is to "run" the project).
This lets me launch the app in the debugger within VS 2017...and makes me think this might be a bug in VS 2017, with the RunCommand property not being defined by the build environment the way it should be.
For the 1.0.0 and 1.1.0 SDK, the Microsoft.NET.Sdk.targets file tries to set the RunCommand property when Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework' and '$(OutputType)' == 'Exe'". But since the project specifies <OutputType>winexe</OutputType> (which it needs to), the condition is not satisfied (and of course none of the others are either).
This seems to have been fixed in the upcoming 2.0.0 SDK (source, you'll have to explore the other files to find the _IsExecutable property), which should be shipped in the next VS2017 update.
In the meantime I opted to set the property manually in my .csproj: <RunCommand>bin\Debug\net47\MyApp.exe</RunCommand> (I could've spent more time to use more SDK-defined properties, but I'd have to schedule it after the SDK targets are imported, which I've left out for simplicity)
What kind of "migrate" are we talking here? There is no in-built migration tool, and it's way more than the previous "VS 2012 to VS2013" type upgrade, it's a fundamentally different format all together.
I know some people have tried (unsuccessfully) to manually update the .csproj XML, it usually ends in frustration and getting no where with it.
Honestly I would 100% recommend making brand new projects and manually copying your files in. Will take a while, but at least you'd be totally sure it was actually going to work, and you don't spend hours trying banging your head on the wall with the barely documented new XML format.
Of course a better option would be something built in to do it for you.

Nuget Conflicting projectreference and packagereference

I'm running a ASP.NET Core app on the .NET 4.6.1 framework. I have 1 solution with multiple projects in it. All of the projects are class libraries that reference each other via PackageReferences in their .csproj (this way we can build, package and version them independently). However, I want to be able to test their integration with one another without needing to push them up to NuGet first - aka I want to use them as ProjectReferences in the solution, but PackageReferences when building them through my Jenkins build process in order to version the components separately.
When .NET Core was project.json based, this worked fine. I would set the version at the top of the project.json and if a project existed with that version in the solution it would reference it as a project, otherwise it would look for it on my NuGet feed.
The problem with using ProjectReferences is that all project's would get the same version when they are built and sent to NuGet.
Is there any way to do this in csproj? Look for a project reference if it exists, otherwise look at NuGet?
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>1.3.0</VersionPrefix>
<TargetFramework>net461</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AssemblyName>MyProject1</AssemblyName>
<PackageId>MyProject1</PackageId>
<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MyProject2" Version="1.4.0-*" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="1.1.1" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
</Project>
Above is an example, I would like MyProject2 to be referenced by ProjectReference if 1.4.0 exists in the solution.
I recently was try to do the same thing and couldn't find the answer but figured out something that works for me. You can use the Exists condition in MSBuild for the csproj to include the project reference when its there and exclude the package reference if its there:
<Project Sdk="Microsoft.NET.Sdk">
...
<ItemGroup>
<PackageReference Condition="!Exists('[path-to-project].csproj')" Include="[package-id]" Version="[pacakage-version].*" />
</ItemGroup>
...
<ItemGroup>
<ProjectReference Condition="Exists('[path-to-project].csproj')" Include="[path-to-project].csproj" />
</ItemGroup>
...
</Project>

Compiling .NET Core VS17

For the last couple of days, I've been trying to compile my .NET Core console application, and upload it to a VPS running "Windows Server 2012 R2". The reason I am using .NET Core is because this is needed for the library - Discord.Net 1.0.
The first thing I tried was simply taking my release DLL file and data, with the following file structure:
LeveledStudios.deps
LeveledStudios.dll
LeveledStudios.pdb
LeveledStudios.runtimeconfig.dev
LeveledStdios.runtimeconfig
This worked fine for execution on the PC I developed it on, however I then went into my server, ran "dotnet LeveledStudios.dll", and was faced with the error
Error: assembly specified in the dependencies manifest was not found -- package: 'discord.net.commands', version '1.0.0-rc-00546', path: 'lib/netstandard1.3/Discord.Net.Commands.dll`
Noticing this fitted the structure of the .nuget folder on my development PC. I copied it across and faced the same issue, and tried to copy it into the same folder as leveledstudios.dll, only to run into some .dll's which refused to work. This also included missing system DLL files, like System.Net.Http, etc.
I did some googling, and saw information about self contained .NET Core applications. This sounds perfect because clearly my problem was that it was not compiling with all my additional libraries. I was a little confused because I did not have a project.json file, as mentioned in all the documentation I read on it.
However when running:
dotnet restore
dotnet build -r win10-x64
I get a host of errors, suggesting none of the system libraries are compiled:
Errors
The contents of LeveledStudios.csproj:
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="1.0.0-rc-00546" />
<PackageReference Include="Discord.Net.Commands" Version="1.0.0-rc-00546" />
<PackageReference Include="Discord.Net.Core" Version="1.0.0-rc-00546" />
<PackageReference Include="Discord.Net.WebSocket" Version="1.0.0-rc-00546" />
<PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.2-beta2" />
</ItemGroup>
</Project>
How can I fix this?
...self contained .NET Core applications [sound] perfect... however... I get a host of errors, suggesting none of the system libraries are compiled.
To resolve the errors that happened when you built as a self-contained application, add this one line to your *.csproj file:
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
Restore will then bring down the runtime, allowing you to build and publish as a self-contained application.
dotnet restore
dotnet build -r win10-x64
dotnet publish -c release -r win10-x64
The final *.csproj file looks like this:
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
<PropertyGroup>
<OutputType>exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Discord.Net" Version="1.0.0-rc-00546" />
<PackageReference Include="Discord.Net.Commands" Version="1.0.0-rc-00546" />
<PackageReference Include="Discord.Net.Core" Version="1.0.0-rc-00546" />
<PackageReference Include="Discord.Net.WebSocket" Version="1.0.0-rc-00546" />
<PackageReference Include="Microsoft.NETCore.App" Version="1.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.2-beta2" />
</ItemGroup>
</Project>
That's how to publish as a self-contained application, which is one alternative to resolve your initial problem.
The other alternative (which Ryan suggested and which you said did not work) is to use a framework-dependent application, in which case you do not need the RuntimeIdentifiers property. You can then run the same commands but without specifying the runtime.
dotnet restore
dotnet build
dotnet publish
The publish files, which will include the Discord dependencies, will be here:
bin\Debug\netcoreapp1.0\publish

Categories

Resources