Asp Net Core 3.0 GenerateAssemblyInfo = False is breaking my UserSecrets - asp.net-core-3.0

For some very odd reason when I attempt to add a GenerateAssemblyInfo flag to my local branch all of my user secrets fail to be replaced in my appsettings.json.
Does anyone happen to have any idea why this may be happening?
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UserSecretsId>aspnet-SomeStuff</UserSecretsId>
</PropertyGroup>

I ended up having to add the following line to my AssemblyInfo.cs and it worked like expected.
[assembly: UserSecretsId("aspnet-SomeStuff")]

Related

MSBuild .NET Core 3.1 application - Set PublishDir value in csproj

I struggle to set the PublishDir value in the new .net core 3.1 csproj format.
In the old csproj format I was able to set the value like this:
<PropertyGroup>
<PublishDir>..\test\path</PublishDir>
I was even able to use a Condition attribute in the PublishDir element to use different values depending on the build configuration.
Now with the new csproj I tried the same, but the PublishDir is still set to something like:
bin/Release/netcoreapp3.1/win-x64/app.publish.
Where does this directory come from?
Only when I add the parameter via msbuild call with /p:PublishDir="..\test\path" the application gets published in the appropriate directory.
Is there anything else I need to take into consideration? Do I need to set the value after a specific target, like PrepareForPublish?
This is the solution I had mentioned in the comment before:
<Target Name="CustomPublishTarget" BeforeTargets="PrepareForPublish">
<ItemGroup>
<PublishDir Include="$(PublishDir)" />
</ItemGroup>
<PropertyGroup>
<PublishDir>..\test\path</PublishDir>
</PropertyGroup>
</Target>

How to show message box on .net core Console application?

I'm developing .net core console application. I want to alert to user when want to exit application. Like below;
MessageBox.Show("Contiue or not", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.No)
Application.Exit();
But I can't add System.Windows.Forms referance to the my project. I'm getting this error.
Error CS0234 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)
Is that possible to show Message Box on .net core Console Application?
In order to use Windows Forms you need to modify .csproj:
set UseWindowsForms to true
append -windows to TargetFramework (e.g. net6.0-windows)
ConsoleApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Program.cs:
System.Console.WriteLine("Hello, Console!");
System.Windows.Forms.MessageBox.Show("Hello, Popup!");
Result:
Notes:
I checked this solution on .NET Core 3.1, .NET 5 and .NET 6 on Windows x64.
If you don't want console window to be shown, set OutputType in .csproj to WinExe instead of Exe.
Windows Forms works only on Windows, so as this solution.
On .NET Core 3.1 there is no requirement to change target framework to Windows, however it is still impossible to publish executable for Linux OS.
If you need cross-platform solution that shows popups on Windows and only use console on Linux – you can create custom build configuration and use preprocessor directives as in the example below.
Cross-platform ConsoleApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Configurations>Debug;Release;WindowsDebug;WindowsRelease</Configurations>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'WindowsDebug' Or '$(Configuration)' == 'WindowsRelease'">
<DefineConstants>WindowsRuntime</DefineConstants>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Cross-platform Program.cs
System.Console.WriteLine("Hello, Console!");
#if WindowsRuntime
System.Windows.Forms.MessageBox.Show("Hello, Popup!");
#endif
Some console apps need this line added to the project as well...
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
Adding this to the example in Daniil's post would give you this...
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<!--Insert DisableWinExeOutputInference here -->
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
</PropertyGroup>
</Project>

.net core build produces localization folders

I have a web asp.net solution that is using .net core 2.0. I am building it using the command:
dotnet publish MySolution.sln --configuration release --output d:\test_output
But when I check the output folder, I'm seeing a lot of localization folders, as you can see in the image bellow:
Is there a way to publish the code without generating these folders?
For the projects using ASP.NET Core 3.1, add this line to your *.csproj file:
<PropertyGroup>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
</PropertyGroup>
The source of the answer in this post: Disable Dll Culture Folders on Compile.
The solution provided by #Igor.K worked for my API project, but for the ASP.NET Core MVC website in my solution, I had to make a minor change.
Try adding the line below to your .csproj file.
<PropertyGroup>
<ResourceLanguages>en</ResourceLanguages>
</PropertyGroup>
You can edit this file by right-clicking your project and selecting "Unload Project". Then, when you right-click again you will be able to edit the .csproj file. Make sure you reload the project when you're finished though.
So, if SatelliteResourceLanguages doesn't solve your problem, ResourceLanguages might do the trick.
[in net 5.0] All above solutions didn't work for me.
Out of despair I added:
<PropertyGroup>
<SatelliteResourceLanguages>en-US;en</SatelliteResourceLanguages>
</PropertyGroup>
and it worked, absolutely no idea why
On the .csproj file, you look for "Microsoft.VisualStudio.Web.CodeGeneration.Design" Package reference and add the property ExcludeAssets="All"
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" ExcludeAssets="All" />
Here is the reference: Disable Dll Culture Folders on Compile
Neither the SateliteResourceLangauges nor the ResourceLangauges solutions worked for me. In my case the files were being generated by the following nuget:
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" ExcludeAssets="All" />
Affixing ExcludeAssets="All" to it as shown above resolved the issue.

Convert .Net Core to .Net Framework

I have a .Net Core project web project, and for various reasons want to convert it to a .Net Framework project.
Is there an easy way to do this, or do I have to start again and import the code from the previous projects
I have loaded core project to the VS 2017 RC Community and open *.csproj in text editor.
Just delete teg
<RuntimeFrameworkVersion>
and replace
<TargetFramework>netcoreapp1.1</TargetFramework>
to
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
And after all in project properties set to any another framework and reset back (VS reload and repair *.csproj file).
This worked for me in VS2017:
Start with .net core web project template.
Edit *.csproj so it looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.RazorPages" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
</ItemGroup>
</Project>
Save and close.
Try running project.
The PackReferences is just the NuGet files, and you can add them through the GUI if the versions are different from mine above.
There's lots of similar answers here, but I didn't see one that was quite what I ended up doing, so I'd like to leave this here just in case someone else is in the same shoes.
Just to be clear, my project was a console program. So, if you're trying to use this answer for something else, your mileage may vary.
In your .csproj file, inside of the <PropertyGroup></PropertyGroup> tag, modify <TargetFramework> to reflect the following:
<TargetFramework>net461</TargetFramework>
Now, in this example, I was using v4.6.1. I can only assume that you'll plug in your version behind the word "net", without the periods. Good luck!
None of the answers here worked for me. In .Net Core 2 the project.json file no longer exists. However, I did solve this problem using the following steps.
1) I removed all nuget packages from my existing project.
2) I created a separate .net core web app project, targeting .net 4.61. This was to get the default nuget packages.
3) I edited the temporary project's .csproj file, copied all the PackageReference nodes inside ItemGroup, and pasted them into my existing projects .csproj file.
4) Edited the TargetFramework node (inside PropertyGroup) from "netstandard2" to "net461"
I had a few package changes to track down and resolve, but otherwise I was able to run.
In my version of Visual Studio 2017 (15.6.2) after 'Unloading the Project', right-clicking and selecting 'Edit <your project file>, I had to:
Add the node:
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
Delete the nodes:
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion Condition=" '$(TargetPlatformVersion)' == '' ">10.0.16299.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
There are several steps that you need to do, in order to achieve this.
Firstly right click on the .csproj file and add the following
<TargetFrameworks>netstandard2.0;netcoreapp2.0;net35;</TargetFrameworks>
<RuntimeIdentifiers>win7-x86;win7-x64</RuntimeIdentifiers> <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
Once you have made these changes reload the project and build it.
This will generate the .dll files and Nuget package for this
build in the Debug/Release folder of the project.
Add these .dll to the nuget and access these projects from
nuget.
Try the above steps. This should work.
My .net standard project is relatively simple with few Nuget packages. I just changed
<TargetFramework>netstandard2.0</TargetFramework>
TO
<TargetFramework>**net461**</TargetFramework> under PropertyGroup section of .csproj file and this did the job for me.. Thanks to Brandon Barkley for your answer in the comments.
add below in csproj
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net471</TargetFrameworks>
</PropertyGroup>
I had only a handful of source files. For me it worked best by
Closing Visual Studio 2022
Renaming away the solution folder
Creating a new Visual Studio solution of type "WPF App (.NET Framework)" with the original folder name and same project name
Copying all *.xaml. *.xaml.cs and *.cs from the old project to the new, not touching *.sln, *.csproj and *.config.
Project->Add Existing Item… and adding the copied items
Adding all the special references.
That rebuilt all without a complaint.

Building a ASP.NET solution from command-line?

How can I build an ASP.NET web application from the command line?
Try this in a .bat file, replace v4.0.30319 with the appropriate version:
CD C:\Windows\Microsoft.NET\Framework\v4.0.30319
msbuild "C:\inetpub\wwwroot\MyWebSite.sln"
Take a look at the devenv.exe /Build switch, you give it a solution file to build, e.g.
devenv.exe "C:\Documents and Settings\someuser\MySolution.sln" /build DEBUG
If you have more complex build requirements then look into MSBuild.
This is a round about way to do it, but you can use the MSBuild task as part of an msbuild project:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Solutions Include="*.sln" />
</ItemGroup>
<Target Name="Build" >
<MSBuild BuildInParallel="true" Projects="#(Solutions)" RebaseOutputs="true" />
</Target>
built with msbuildprojectname.proj from the command line.
This may seem like overkill, but you can then add extra stuff into the project (like restarting websites, zipping files, Virtual machine control even!, code coverage) that can help you setup and test the project.
As dumb as I might look, I have used "aspnet_compiler.exe" for compiling/deploying projects
http://social.msdn.microsoft.com/Forums/en-US/msbuild/thread/2ca80dfd-b7d7-4e09-98ff-891b1570f4db
To build a solution directly,
msbuild mysolution.sln
You'll find MSBuild in the Microsoft.NET folder in your Windows directory.
You may also want to pre-compile your Web site. Instructions for that are on MSDN here. If you use the Web Deployment project node (detailed at the bottom) then the deployment will happen automatically when you run msbuild on the solution file.

Resources