Visual Studio- custom project templates - c#

I want to create a custom project templates in Visual Studio. As the template will have the conditional parameters, I can't use new dotnet templates (it only works in terminal, but I want to have a visual form of the template). How could I create the project template with the textfields, conditional parameters and two steps form? It will be a complex project template form and after completing the form I want to get the specific folder with text files inside (without solution project, just a folder with files).
I started creating a custom project templates, but what I get is a folder with solution project. But I want only a folder with the files (not any .csproj file).
Is it because of a tag: <ProjectType>Csharp</ProjectType> in .vstemplate?

According to your requirements, it is recommended that you use the way of generating packages with NuGet.
Proceed as follows:
Create the Content\GMSTemplate directory under the root directory GMS, and copy the template files and folders into it
The template.json file is modified to:
{
"$schema": "http://json.schemastore.org/template",
"author": "5DThinking",
"classifications": [ "WinForm" ],
"name": "5DThinking WinForm GMS v1.0",
"identity": "GMS.WinForm.1.0.Template",
"shortName": "gms1.0",
"tags": {
"language": "C#" ,
"type":"project"
},
"sourceName": "Thinking.GMS",
"preferNameDirectory": true
}
Create a GMSTemplate.nuspec file in the GMS directory with the following content:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>GMSTemplate</id>
<version>1.0.1</version>
<description>
GMS(General Management System) For WinForm
</description>
<authors>5DThinking</authors>
<packageTypes>
<packageType name="Template" />
</packageTypes>
</metadata>
</package>
Package with NuGet, execute the command nuget pack GMSTemplate.nuspec -OutputDirectory . in the GMS directory, and then generate the GMSTemplate.1.0.1.nupkg file
Create a CreateYourProject.bat file in the GMS directory, the content is as follows:
color 4
dotnet new -i GMSTemplate.1.0.1.nupkg
set /p OP=Please set your project name(for example:Baidu.Api):
md .1YourProject
cd .1YourProject
dotnet new gms1.0 -n %OP%
cd ../
echo "Create Successfully!!!! ^ please see the folder .1YourProject"
dotnet new -u GMSTemplate
echo "Delete Template Successfully"
pause
Double-click to execute the CreateYourProject.bat file, follow the prompts to enter the new project name, and you're done.

Related

MudBlazor css and js fails to load when running as Windows Service

I cannot figure out what I am doing wrong here, but I am unable to run a "Blazor Server" app with MudBlazor as a Windows Service (installed via MSIX), since loading the MudBlazor css and js fails with 404.
This is how to reproduce the problem I am facing:
Use the MudBlazor template to get started:
mkdir -p mud-test/MyApplication
dotnet new mudblazor --host server --name MyApplication -o mud-test/MyApplication
dotnet add mud-test/MyApplication/MyApplication.csproj package "Microsoft.Extensions.Hosting.WindowsServices"
dotnet new sln -n MyApp -o mud-test
dotnet sln mud-test/MyApp.sln add mud-test/MyApplication/MyApplication.csproj
Now open the solution file in VS2022. We need to make a few modifications to enable running in a Windows Service:
Open Program.cs
Replace the line var builder = WebApplication.CreateBuilder(args); with:
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
});
builder.Host.UseWindowsService();
Add a new project of the type Windows Application Packaging Project called Installer. Just leave the platform versions at the default values. Then add a reference to the MyApplication project.
Open the Package.appxmanifest file in code view, and add this:
Add this namespace reference as an extra attribute on the Package element: xmlns:desktop6="http://schemas.microsoft.com/appx/manifest/desktop/windows10/6"
Add this element under the Application element:
<Extensions>
<desktop6:Extension
Category="windows.service"
Executable="MyApplication\MyApplication.exe"
EntryPoint="Windows.FullTrustApplication">
<desktop6:Service
Name="MyApplication"
StartupType="auto"
StartAccount="localService">
</desktop6:Service>
</desktop6:Extension>
</Extensions>
Add this element under the Capabilities element: <rescap:Capability Name="packagedServices"/>
Once this is done, we are ready to build the installer:
Right-click the Installer project and select Publish -> Create App Packages...
Use the default distribution method of "Sideloading", but uncheck the Enable automatic updates
Create a new self-signed certificate, and trust it
Click Create on the Select and configure packages screen, to accept all the defaults
When the installer has been built, open a PowerShell window with administrator priviledges, and navigate to the output folder of the installer build. Then run the Install.ps1 script to install the service.
Now, open a browser and navigate to http://127.0.0.1:5000/ ... which should load the sample app, but giving 404 on both the MudBlazor.min.css and Mudblazor.min.js assets
I temporarily worked around this by hosting the MudBlazor.min.css & MudBlazor.min.js files externally, and then loading them from there instead of from the _content.
This is by no means a good solution, so I am still eager to hear if anybody knows how to make it work properly

Can't type anything on Visual Studio Code's C# output

so i was trying to run a really basic C# program on vscode, here's the code:
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
string Name;
Console.Write("Input your username:\n");
Console.Write("u/");
Name = Console.ReadLine();
Console.Write("\nSo your username is u/" + Name);
}
}
}
And naturally, it outputs:
Input your username:
u/
Except that i can't type anything next to the "u/", not even below.
it doesn't even freeze, popup an error or something like that, it just shows nothing.
and the same problem happens with C and C++. any tip?
Solutions
This problem is solved in two steps:
Adding Developer Command Prompt to Visual Studio Code
Updating "console" field in the launch.json file.
1. Adding Developer Command Prompt to Visual Studio Code
If you create C# programs with the terminal or powershell that comes with Visual Studio Code, you will be using the old toolkit. In this case, launch.json and tasks.json files aren't created in the .vscode directory when you compile the project, and you cannot use the new features of the C# programming language through the old toolset (only C# 5.0 is supported). To add a new terminal to Visual Studio Code, use the Control Shift P shortcut and enter the command "Terminal: Select Default Profile". Click on the settings icon of any default terminal on the window that opens and name the new terminal profile "Developer Command Prompt".
To check that the operation was successful, use the Control Shift P shortcut to view the new terminal listed and enter the command "Terminal: Select Default Profile".
Use the Control Shift P shortcut and enter the "Preferences: Open Setting (JSON)" command to open the setting.json file. You can view the newly added terminal information named "Developer Command Prompt" under "terminal.integrated.profiles.windows" settings. Update this field below:
"Developer Command Prompt": {
"path": [
"${env:windir}\\Sysnative\\cmd.exe",
"${env:windir}\\System32\\cmd.exe"
],
"args": [
"/K",
"C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\Common7\\Tools\\VsDevCmd.bat",
],
"icon": "terminal-cmd"
}
Open "Developer Command Prompt" and create new project:
> mkdir TestProject
> cd TestProject
> dotnet new console
To receive data input from the console while debugging, change the configuration in the launch.json file as follows:
"console": "integratedTerminal"
When you press the F5 button to debug the opened project, a panel will open to select the environment. Select ".NET 5+ and .NET Core" from this field. In this step, the .vscode directory (launch.json and task.json files) will be created.
Accept all suggestions by following the prompts at the bottom right of Visual Studio Code as the new toolset will be used for the first time on the system. At this stage, launch.json and task.json files will be created under the .vscode directory.
2. Updating "console" Field In The launch.json
To receive data input from the console while debugging, change the configuration in the launch.json file as follows:
"console": "integratedTerminal"
3. Debugging On Test Project
Press F5 to debug the project:
References
Visual Studio Code - Terminal Profiles
OmniSharp - Instructions For Setting Up The .NET Core Debugger
Set As Default The Developer Command Prompt Of VS In VS Code
Debug Console Window Cannot Accept Console.ReadLine() Input During Debugging

dotnet build cmd with zip package creates a zip of lengthy subfolders for .netcoreapp 2.1

This is my dotnet build command for my .netcoreapp 2.1 MVC website which has two class library projects(VinXP.Core.csproj, VinXP.Infrastructure.csproj) referenced to the main web project(VinXP.web.csproj).
dotnet build VinXP.sln /nologo /p:PublishProfile=Release /p:PackageLocation="E:\Publish\DRS\package" /p:OutDir="E:\Publish\DRS\out" /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /maxcpucount:1 /p:platform="Any CPU" /p:configuration="Release" /p:DesktopBuildPackageLocation="E:\Publish\DRS\package\package.zip"
This above command doesn't create a zip in E:\Publish\DRS\package as mentioned in DesktopBuildPackageLocation. Instead, it creates E:\Publish\DRS\package\VinXP.VinXPWeb.zip.
Upon unzipping this zipped file, my build is available in a very deep sub folder as
E:\Publish\DRS\package\VinXP.Web.zip\Content\E_C\Working\Projects\Git\VinXPDevelopment\src\VinXP.Web\obj\Release\netcoreapp2.1\PubTmp\Out\[build]
Why cant it doesn't create a zip file with a build without creating a lengthy subfolder as I mentioned in DesktopBuildPackageLocation="E:\Publish\DRS\package\package.zip" on dotnet build command?
Attempts:
1 : Changed the path mentioned in the keys of my dotnet build command "PackageLocation","OutDir","DesktopBuildPackageLocation" but it only changes the root folder.
2 : Looked for .net documentation on Microsoft site couldn't find it useful with my challenge faced.
This basically depends on the CLI version you are using
If 2.X Instead of donet build since you are on a Windows PC you can replace this with donet publish
I.E
dotnet publish VinXP.sln /nologo /p:PublishProfile=Release /p:PackageLocation="E:\Publish\DRS\package" /p:OutDir="E:\Publish\DRS\out" /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /maxcpucount:1 /p:platform="Any CPU" /p:configuration="Release" /p:DesktopBuildPackageLocation="E:\Publish\DRS\package\package.zip"
Ideally to stop the creation of recursive directories you need include(Update) the following on the package.json file
"publishOptions": {
"exclude": [
"bin/**",
"obj/**",
"node_modules",
"**.user",
"**.vspscc"
],
OR
1.X Instead of donet build since you are on a Windows PC you can replace this with msbuild
I.E : msbuild VinXP.sln /nologo /p:PublishProfile=Release /p:PackageLocation="E:\Publish\DRS\package" /p:OutDir="E:\Publish\DRS\out" /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /maxcpucount:1 /p:platform="Any CPU" /p:configuration="Release" /p:DesktopBuildPackageLocation="E:\Publish\DRS\package\package.zip"
Refer this link for more.
Refer to this for path related issues:
1 , 2

dotnet watch $(SolutionDir) is *Undefined*

I'm trying to use dotnet watch however my project references nuget package which uses $(SolutionDir) to copy some files in prebuild event. It kinda make sense because dotnet watch is run on project level so $(SolutionDir) doesn't exist. Is there any way to run dotnet watch for entire solution?
I created a batch script watch.bat
call SET ASPNETCORE_ENVIRONMENT=Development
call SET SolutionDir=%~dp0\\..\\..\\
call dotnet watch run
Added a script in package.json:
"scripts": { "dotnetwatch": "watch.bat" }
Now I can run it like this from CLI:
npm run dotnetwatch
or from within visual studio with the following launchsettings.json profile:
"dotnet watch": {
"executablePath": "watch",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
It's not an ideal solution but at least it's good enough for me.
As of dotnet-watch 2.0.0-preview2, it is not possible to run dotnet watch on solution files. However, you can construct a custom 'project' for the watcher that will watch multiple projects. See https://github.com/aspnet/DotNetTools/tree/rel/2.0.0-preview2/samples/dotnet-watch/WatchMultipleProjects for a complete sample of how to do this.

How to compile c# in Microsoft's new Visual Studio Code?

I have installed the preview version of Microsoft's new code editor "Visual Studio Code". It seems quite a nice tool!
The introduction mentions you can program c# with it, but the setup document does not mention how to actually compile c# files.
You can define "mono" as a type in the "launch.json" file, but that does not do anything yet. Pressing F5 results in: "make sure to select a configuration from the launch dropdown"...
Also, intellisense is not working for c#? How do you set the path to any included frameworks?
Launch.json:
"configurations": [
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Cars.exe",
// Type of configuration. Possible values: "node", "mono".
"type": "mono",
// Workspace relative or absolute path to the program.
"program": "cars.exe",
},
{
"type": "mono",
}
Since no one else said it, the short-cut to compile (build) a C# app in Visual Studio Code (VSCode) is SHIFT+CTRL+B.
If you want to see the build errors (because they don't pop-up by default), the shortcut is SHIFT+CTRL+M.
(I know this question was asking for more than just the build shortcut. But I wanted to answer the question in the title, which wasn't directly answered by other answers/comments.)
Intellisense does work for C# 6, and it's great.
For running console apps you should set up some additional tools:
ASP.NET 5; in Powershell: &{$Branch='dev';iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.ps1'))}
Node.js including package manager npm.
The rest of required tools including Yeoman yo: npm install -g yo grunt-cli generator-aspnet bower
You should also invoke .NET Version Manager: c:\Users\Username\.dnx\bin\dnvm.cmd upgrade -u
Then you can use yo as wizard for Console Application: yo aspnet Choose name and project type. After that go to created folder cd ./MyNewConsoleApp/ and run dnu restore
To execute your program just type >run in Command Palette (Ctrl+Shift+P), or execute dnx . run in shell from the directory of your project.
SHIFT+CTRL+B should work
However sometimes an issue can happen in a locked down non-adminstrator evironment:
If you open an existing C# application from the folder you should have a .sln (solution file) etc..
Commonly you can get these message in VS Code
Downloading package 'OmniSharp (.NET 4.6 / x64)' (19343 KB) .................... Done!
Downloading package '.NET Core Debugger (Windows / x64)' (39827 KB) .................... Done!
Installing package 'OmniSharp (.NET 4.6 / x64)'
Installing package '.NET Core Debugger (Windows / x64)'
Finished
Failed to spawn 'dotnet --info' //this is a possible issue
To which then you will be asked to install .NET CLI tools
If impossible to get SDK installed with no admin privilege - then use other solution.
Install the extension "Code Runner". Check if you can compile your program with csc (ex.: csc hello.cs). The command csc is shipped with Mono. Then add this to your VS Code user settings:
"code-runner.executorMap": {
"csharp": "echo '# calling mono\n' && cd $dir && csc /nologo $fileName && mono $dir$fileNameWithoutExt.exe",
// "csharp": "echo '# calling dotnet run\n' && dotnet run"
}
Open your C# file and use the execution key of Code Runner.
Edit: also added dotnet run, so you can choose how you want to execute your program: with Mono, or with dotnet. If you choose dotnet, then first create the project (dotnet new console, dotnet restore).
To Run a C# Project in VS Code Terminal
Install CodeRunner Extension in your VS Code (Extension ID: formulahendry.code-runner)
Go to Settings and open settings.json
Type in code-runner.executorMap
Find "csharp": "scriptcs"
Replace it with this "csharp": "cd $dir && dotnet run $fileName"
Your project should Run in VS Code Terminal once you press the run button or ALT + Shift + N

Categories

Resources