Add item to fsproj through dotnet CLI - f#

I am using the dotnet CLI (v1.0.4) to create and manage projects. I have found the documentation for and have successfully created projects using the CLI with commands such as the following:
dotnet new console -lang F# -o src/myConsoleApp
I have also figured out how to add references to other projects, DLLs, and Nuget packages.
The one thing that I can't figure out how to do is add a new compile item to the fsproj file through a CLI command. Does such a command exist? I can manually edit the fsproj file and add a new <Compile Include="..." />, but that seems too easy to make typos and other errors. I have been using Visual Studio for many years, so perhaps I am under the wrong impression that there should be another way than to manually edit the project file to add a file to a project?

Related

No Dependencies are automatically installed when adding nuget package

I'm creating a small nuget package for internal using (I created .NET standard project to use both .NET Framework or .NET Core). I created a Build Definition to build the library then push it to the Nuget Feeds of my company.
I have 2 questions:
How can I set it automatically increase the version of the my nuget package when Queue the build in tfs 2017 & Can I set some information like Author, Company...for my package?
After having the Package in the TFS Packages. I tried to add my nuget package to my project. The nuget referenced package were not automatically added to the project. I can see, it displayed "No dependencies" but in fact, I used 2 packages in my library are Newtonsoft & StackExchange.Redis. So when I run the debug, error shown "Could not find the StackExchange.Redis..."
That's strange
In my Libary project setting, I can see
There are two ways to package nuget packages. You can use "nuget pack" or "dotnet pack" (if you have some dotnet core in your solution). The difference is that dotnet pack will create a dependency section in your nuspec and you would see dependency upon installing where nuget pack will not by default unless you use an actual nuspec file. If you do create a nuspec file, you'll need to add the dependency section to you file in order to have them pop up at installation. Check here for info on the nuspec file.
For you version number it's also actually in the nuspec file in the metadata section there is a tag with the version you want. The TFS nuget pack task as a drop down you can use to send in a version number. For this task you can either use a nuspec file or the csproj directly.
You have the same options for dotnet pack using the dotnet core task
Update:
If you change your build number version pattern in the options tab for a 4 (or 3) digit pattern like $(Date:yyyy.MM.dd).$(Rev:r) e.g. 2020.03.05.xx (where xx will increment for each build in the same day and go down to 1 the next day) or 1.0.$(Rev:r) which will be 1.0.xx that will increment forever until you change the 1.0 value and then use the "Use Build Number" it will do it for you.

How to build .NET Framework ASP.NET app using dotnet

We have a mixture of ASP.NET Core and .NET Framework ASP.NET apps. We use a mixture of msbuild and dotnet to build the apps.
I'm trying to go all in on dotnet, but the build always throws an error of:
error MSB4019: The imported project "C:\Program
Files\dotnet\sdk\3.0.100-preview5-011568\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets"
was not found. Confirm that the path in the declaration is
correct, and that the file exists on disk.
Right now I'm just trying with a very simple command of dotnet msbuild foo.sln. No flags or anything being used for now.
I've tried this on multiple ASP.NET (not Core) apps and they all give the same error.
For ASP.NET Web applications, you need to compile using the following code.
C:\'Program Files (x86)'\'Microsoft Visual Studio'\[year]\[edition]\MSBuild\Current\Bin\msbuild.exe [project.csproj] /p:VisualStudioVersion=[version] /p:DeployOnBuild=true /p:PublishProfile=[profileName]
You can run in cmd or PowerShell.
Replace tags according to the version of Visual Studio installed on your machine and solution version.
For Example:
C:\'Program Files (x86)'\'Microsoft Visual Studio'\2019\Community\MSBuild\Current\Bin\msbuild.exe HelloWorld.csproj /p:VisualStudioVersion=16.0 /p:DeployOnBuild=true /p:PublishProfile=Release
We've solved the Microsoft.WebApplication.targets missing reference by adding it as a NuGet dependency, however the dotnet msbuild command still can't compile all related framework and asp.net projects.
We've also stood up our build server inside a docker container on an ubuntu image, as we were hoping to improve our infra with containerization etc.
However we've hit a wall in building all possible projects using the dotnet executable, even though it has the msbuild command built in.
Anyone had any luck with this?
This answer indicates this is not possible though
https://stackoverflow.com/a/66366638/6578823

The default XML namespace of the project must be the MSBuild XML namespace

I cloned the ASP.NET Core SignalR Repo locally, and try opening the solution from within the following environment.
IDE
Microsoft Visual Studio Enterprise 2015
Version 14.0.25431.01 Update 3
Microsoft .NET Framework
Version 4.6.01055
DOT NET CLI
λ dotnet --info
.NET Command Line Tools (1.0.0-preview2-1-003177)
Product Information:
Version: 1.0.0-preview2-1-003177
Commit SHA-1 hash: a2df9c2576
Runtime Environment:
OS Name: Windows
OS Version: 6.1.7601
OS Platform: Windows
RID: win7-x64
I end up seeing a lot of these kinds of error messages:
..\Repos\SignalR\src\Microsoft.AspNetCore.SignalR\Microsoft.AspNetCore.SignalR.csproj
: error : The default XML namespace of the project must be the
MSBuild XML namespace. If the project is authored in the MSBuild 2003
format, please add
xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the
element. If the project has been authored in the old 1.0 or
1.2 format, please convert it to MSBuild 2003 format. ..\Repos\SignalR\src\Microsoft.AspNetCore.SignalR\Microsoft.AspNetCore.SignalR.csproj
I want to know how to fix this the correct way.
The projects you are trying to open are in the new .NET Core csproj format. This means you need to use Visual Studio 2017 which supports this new format.
For a little bit of history, initially .NET Core used project.json instead of *.csproj. However, after some considerable internal deliberation at Microsoft, they decided to go back to csproj but with a much cleaner and updated format. However, this new format is only supported in VS2017.
If you want to open the projects but don't want to wait until March 7th for the official VS2017 release, you could use Visual Studio Code instead.
I ran into this issue while opening the Service Fabric GettingStartedApplication in Visual Studio 2015. The original solution was built on .NET Core in VS 2017 and I got the same error when opening in 2015.
Here are the steps I followed to resolve the issue.
Right click on (load Failed) project and edit in visual studio.
Saw the following line in the Project tag: <Project Sdk="Microsoft.NET.Sdk.Web" >
Followed the instruction shown in the error message to add xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to this tag
It should now look like:
<Project Sdk="Microsoft.NET.Sdk.Web" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
Reloading the project gave me the next error (yours may be different based on what is included in your project)
Saw that None element had an update attribute as below:
<None Update="wwwroot\**\*;Views\**\*;Areas\**\Views">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
Commented that out as below.
<!--<None Update="wwwroot\**\*;Views\**\*;Areas\**\Views">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>-->
Onto the next error: Version in Package Reference is unrecognized
Saw that Version is there in csproj xml as below (Additional PackageReference lines removed for brevity)
Stripped the Version attribute
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" />
I now get the following:
Bingo! The visual Studio One-way upgrade kicked in! Let VS do the magic!
The Project loaded but with reference lib errors.
Fixed the reference lib errors individually, by removing and replacing in NuGet to get the project working!
Hope this helps another code traveler :-D
#DavidG's answer is correct, but I would like to add that if you're building from the command line, the equivalent solution is to make sure that you're using the appropriate version of msbuild (in this particular case, it needs to be version 15).
Run msbuild -version to see which version you're using or where msbuild to check which location the environment takes the executable from and update (or point to the right location of) the tools if necessary.
Download the latest MSBuild tool from here.
If getting this error trying to build .Net Core 2.0 app on VSTS then ensure your build definition is using the Hosted VS2017 Agent queue.
I was getting the same messages while I was running just msbuild from powershell.
dotnet msbuild "./project.csproj" worked for me.
if the project is not a big ,
1- change the name of folder project
2- make a new project with the same project (before renaming)
3- add existing files from the old project to the new project (totally same , same folders , same names , ...)
4- open the the new project file (as xml ) and the old project
5- copy the new project file (xml content ) and paste it in the old project file
6- delete the old project
7- rename the old folder project to old name
I had the same problem and solved it by using dotnet instead of msbuild.

Is it possible to use monogame in VS code?

I am using an iball notebook and I don't have enough memory to install Visual Studio. I am having no problems using VS code and i am able to make and create executables of my console applications. I need to learn game development but everyone keeps saying that i need to install visual studio for it
However i did find a fourm on monogame on the topic and found that it is possible(At least on Linux) to use Monogame in VS code.
http://community.monogame.net/t/visual-studio-code-and-monogame/2371
Please Help me out.I want to know if it's really possible to compile and run a monogame app in windows.
Answer edited as Monogame released official dotnet project templates
I finally got it working.
I realized that all I needed was to create a monogame project (*.csproj) and Compile/Build it without Visual Studio. VS Code is just a feature-rich text editor and I would need other toolset for it.
MSBuild tool is used to Compile/Build monogame project and is available as a CLI. It is available without installing Visual Studio.
For C# project building, dotnet core is required. executing the script
dotnet new [Template]
creates a new Project. We need to add a template for monogame here.
As per the latest update by the Monogame Team, you can install the templates by executing
dotnet new --install "MonoGame.Templates.CSharp"
Use the script
dotnet new -h
to check out all the templates available.
Now, to generate the project, use the following
dotnet new mgwindows
On successful execution, this will generate [FolderName].csproj, Game1.cs, Program.cs and other files/folders of monogame project. Please not that this csproj is on .NET Framework (version 4.5 if I'm not wrong....) and therefore it might not work with dotnet run command. (If you're a bit stubborn, you might need to copy the Monogame installed folder(which contains, among many other files, Monogame.target file) in your dotnet installed folder.)
In other words, use msbuild to build and run the project
msbuild
If the program does not contain any compile time errors, the .exe file will be built successfully and you will get to see the the Output file path which you get to execute.
If you're working on Linux or have some other reason not to use MSBuild, you should not generate a mgwindows project. You can rather chose
dotnet new desktopgl
which works on dotnet core (i.e you can use dotnet run command to execute it).
I wrote this (Windows-only) solution in medium. It's a step-by-step of how to install and run dotnet with MonoGame in the terminal of VSCode.
You need to install:
.NET SDK 5.0
.NET Core SDK 3.1
.NET Runtime 5.0
You can run dotnet in your terminal and see if it's working.
Install MonoGame editor:
dotnet tool install --global dotnet-mgcb-editor
and
mgcb-editor --register
Install MonoGame Templates:
dotnet new --install MonoGame.Templates.CSharp
Create a new project in the chosen template:
dotnet new mgdesktopgl -o ProjectName
Enter in your project with cd ProjectName and add the MonoGame package to it:
dotnet add package MonoGame.Framework.DesktopGL --version 3.8.0.1641
And finally:
dotnet run Program.cs
There is absolutely no reason you cannot work with MonoGame from Visual Studio Code. It will not be an optimal setup since you'll lack debugging, and the setup will be difficult, but if you're okay with that then continue on.
You've already noted that you have no issues creating executable console applications. This is all you really need to be able to do. The key here is that you must build targeting .NET4+ or Mono. If you've followed tutorials that lead you to building .NET Core applications they will not work with MonoGame (at this time). If you are building .NET Core, spend some time looking into how to build Desktop CLR applications using MSBuild or Mono. If you need more information I can expand upon this. You'll also need to be sure you know how to reference other .NET assemblies from your console applications. Please do some research on how to do this before moving on.
For Windows you have the option of targeting DesktopGL (OpenGL) or WindowsDX (DirectX) versions of MonoGame. I'm partial to the DirectX versions myself. You'll need 2 things to get up and running: 1. the MonoGame assemblies, and 2. the MonoGame Pipeline Tool (this is used to compile your content into .XNB files so they may be imported into your game).
To get at MonoGame's assemblies and tools the easiest way I can think of is to install Visual Studio Community Edition and then download and install MonoGame for Visual Studio. This will bring all the tools to you. You'd then need to look at "C:\Program Files (x86)\MonoGame\v3.0\Assemblies" for the appropriate assemblies and "C:\Program Files (x86)\MSBuild\MonoGame\v3.0\Tools" for the MonoGame Pipeline Tool.
If Visual Studio will not let you install because your machines doesn't meet the requirements then you are not out of luck. The assemblies can be pulled in via nuget. Download the latest nuget.exe here: https://dist.nuget.org/index.html and then run: nuget.exe install MonoGame.Framework.WindowsDX or nuget.exe install MonoGame.Framework.DesktopGL. This will create a directory containing a lib folder that contains a net40 folder which contains the .DLL files you need. For WindowsDX I think you'll also need the DirectX runtime https://www.microsoft.com/en-us/download/details.aspx?id=34429. For OpenGL I think you'll need OpenAL (for audio) https://www.openal.org/downloads/.
Once you have the assemblies you'll need to reference them when you build your code. As you've said you're already familiar with creating and running console applications I'll assume you know how to do this. Just reference every managed .DLL you downloaded with Nuget or pulled from the Assemblies folder from the install.
To test things out, drop this into a .cs file, reference the MonoGame assemblies in your build, build it as you would a console application, and execute:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
public class Game1 : Game
{
GraphicsDeviceManager graphics;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
}
[STAThread]
static void Main()
{
using (var game = new Game1())
game.Run();
}
}
You should get a window with a cornflower blue background. If you don't, then you're not building or referencing things right, or you're missing a dependency.
The trick now is getting your hands on the Pipeline Tool, and MGCB.exe. If you were able to install MonoGame for Visual Studio, great!, these files are in the folder I references above. If not, for whatever reason the MonoGame project doesn't distribute stand alone versions of these, only with the installer. I've taken the contents of what you need and plopped it into a dummy release on GitHub here: https://github.com/srakowski/derp/releases/tag/MG. Download the Pipeline.zip file, extract it, and you should have what you need.
Create an empty Content.mgcb file and open it with Pipeline.exe. You should be able to add and build content files. You'll need to copy these files into the same directory where your .exe lives. Commonly, these are put into a Content folder, and Content.RootDirectory = "Content"; is added to the Game's constructor.
Once you get all this working you should be free and clear to create games as your heart desires. Please let me know if you have troubles and we'll work things out.
I've tried Monogame on Visual Studio and own a Windows PC. So I can safely confirm that Monogame does work on Windows. To answer your question.
However, if you want to try it without Visual Studio, then I don't think you can really get far. as there are several build in tools needed to make a decent one. And you cannot debug it either. You're working really limited if you cannot use Visual Studio.
Try to clean up your PC to make some free space. Visual Studio would really be a better choice to work with.

fsproj files in dotnet-core projects

When creating new projects with .net-core (dotnet new -l F#), the projects are created with a project.json file and no fsproj file. However, it is my understanding that an fsproj file is needed in order to get intellisense.
Are there any tools for generating an fsproj file from a project.json file?
I am interested in a solution that works both in Linux and Visual Studio as I use both equally for fsharp.
If you open project.json in Visual Studio 2015 with update 3, it will create an xproj file for the project.
I'm not aware of a way of doing this in linux.
However, as far as I know you don't need an fsproj file to get intellisense.
I found projekt useful for creating and manipulating .fsproj files. However, it will not automatically convert your project.json for you.

Resources