Conditional Compilation Symbols not working - visual-studio

There are plenty of reported bugs in almost every version of Visual Studio with similar or exactly the same issue. The problem is that every bug report I found has been already resolved. Additional info: My exact same code has worked a month ago. I am using .NET Core 3.1.100 and Visual Studio 16.4.2.
Project GUI
.csproj Configuration
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Demo|AnyCPU'">
<DefineConstants>DEMO</DefineConstants>
</PropertyGroup>
Code (as you can see, the DEMO symbol will not be applied).
Has anyone an idea how to hotfix this issue for my production environment?
Thanks in advance!

This is not how this should be done in ASP.NET Core. ASP.NET Core doesn't use the concept of build configurations natively, and while you can sort of use them in places like this or compiler directives, it goes against the grain of ASP.NET Core app design. ASP.NET Core apps are build once, deploy anywhere, in contrast to ASP.NET where you needed to build for each environment separately.
Instead, this should be handled via config and environments. You can even have a "Demo" environment, if you like, which would then allow you to just inject IWebHostingEnvironment and check _env.IsEnvironment("Demo"). Short of that, you should simply specify via config (JSON, environment variable, etc.) a "Demo" boolean, and then pull that out of IConfiguration.

Related

Shared Project option totally gone from the Xamarin.Forms project menu in Visual Studio 2019?

I work with Xamarin.Forms in Visual Studio since a few years, and updated my IDE recently (VS v. 16.3.7). Now I realized something is missing: the Shared Project option.
When i go through the Wizard to create a new project the question if I want to use Shared Project or PCL(or NetStandard) is now missing (see screenshot below from VS 2017)
I do know that you can still create a Shared Project and add an iOS and an Android project manually, but this is just too cumbersome. My question really is: Is there a reason why Microsoft went on to push the PCL/NetStandard above the Shared Project strategy?
I am specially confused about this decision since already back in 2016 #miguel.de.icaza declared his favorite option (for most cases) was the Shared Project strategy:
https://tirania.org/blog/archive/2016/Jan-22.html
For me, the PCL is just too cumbersome for most uses. It is like using a canon to kill a fly. It imposes too many limitations (limited API surface), forces you to jump through hoops to achieve some very basic tasks.
I have installed VS 2017 15.9.14, on this version, Portable Class Libraries (PCLs) are deprecated. You could use .NET Standard instead. On VS 2019, the default is .NET Standard.
On Microsoft document, MS recommend .NET Standard 2.0 libraries over Portable Class Libraries.
For more, please refer to the MS docs. https://learn.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/code-sharing
My question really is: Is there a reason why Microsoft went on to push the PCL above the Shared Project strategy?
For MS announcement, PCL is deprecated, you could not find that when you create the project.
You could create the Shared project by File > New > Project > Shared Project template.
For more details, you could check the link below. https://learn.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/shared-projects?tabs=windows
Updated:
Is there a reason why Microsoft went on to push the >NetStandard< above the Shared Project strategy?".
For the old version of VS 2017, .Net Standard is an option of Shared Project strategy.For the latest version of VS, MS use .Net Standard as the default for Sharing Strategy.
Is there any doc where MS states why Shared Project is now deprecated/not available in the Xamarin.Forms project wizard?
PCL is deprecated. Shared Project is still in use, in my answer, I provide the second link about how to create the shared project.
Summarizing and adding to the answer by #WendyZang-MSFT:
I started a project using the Shared Project strategy around 4 years ago, and until now i have not found any issue/problem specific to it that would make me consider moving to .Net Standard.
Recently i went into the trouble of localization, and even that was possible using Shared Project without much hassle.
As mentioned above, after i saw that the latest version of Visual Studio 2019 was not offering the possibility of creating a Xamarin.Forms App as a Shared Project i got a bit worried thinking that i should migrate my solution. But the time hast passed by, and i just realized that MS encourages the use of .Net Standard, but Shared Project remains also a valid approach.

How to get NuGet options in Visual Studio 2017 Community?

I am struggling with creating NuGet packages. I am using Visual Studio 2017 Community edition.
I have seen a couple of videos that show a "Pack" option on the menu when right-clicking the project in Solution Explorer. However, I do not have that option. Is this one of the features in the other (non-Community) versions of Visual Studio? I believe I have also seen a "create NuGet package on build" option mentioned somewhere. I cannot find that either.
I have tried various ways of using nuget, dotnet, and msbuild from the command line(s), but haven't had much success. Very frustrating.
Any help is appreciated.
If you really want to use Visual Studio, I would recommend installing an extension that helps you with that problem. For example, this one. The options people have in videos depend on the extensions they have installed. For you, it is the same.
Alternatively, just use the command-line tooling for this as explained here or for .NET Core here or here.
dotnet/msbuild pack is only available for SDK-style projects, but I believe works for all versions of Visual Studio, as well as on the command line. .NET Core introduced these SDK-style projects, which can be identified by <Project Sdk="Microsoft.NET.Sdk">. If your project (.csproj if it's a C# project) doesn't have the Sdk property or import Microsoft.NET.Sdk in either of the two other ways, then it's not an SDK style project and doesn't support packing in this way. Another obvious difference between the two styles of projects is that SDK projects are only a few lines long from the new project template and don't list files in the project, whereas old style projects are typically a full screen long, even from a new project template with only a single class file, and it does list individual files in the project. If you want to continue with this project type, you'll need to use nuget.exe pack and you'll probably want to create a .nuspec file to define some of the package metadata.
However, using SDK style projects is the future, it just takes time for all of Microsoft's existing project types to migrate. It's much simpler to use, so personally I would avoid old style projects unless you're using a project type (like ASP.NET, not ASP.NET Core) that doesn't support it.
All of this is confusing for anyone new to the .NET ecosystem. My recommendation is 1. when you install Visual Studio, when making your workload selections, make sure in the component list that .NET Core is selected, whatever the newest version of .NET Core that is available at the time of installation. When creating a new project in Visual Studio, always select the .NET Core version, or .NET Standard version of any new project template, even if you want to target the (Windows) .NET Framework, in which case you edit the .csproj and change <TargetFramework>netstandard2.0</TargetFramework> to <TargetFramework>net45</TargetFramework>, although I would recommend multi-targeting possible by adding a s to the element name and using a semi-colon separated list: <TargetFrameworks>net45;netstandard2.0</TargetFrameworks>. So, avoid the "Class Library (.NET Framework)" template, instead use "Class Library (.NET Standard)" and then change the target if you have to.
#zivkan led me down the right path. Changing my project types to .Net Core from .Net Framework made all the options I mentioned in my original post available. No extensions were needed.
My .Net Core class library project now has the Pack and Publish options available on the project's context menu. In addition, there is a another tab (Package) on the project properties page. On that page there is a "Generate NuGet package on build" option along with version, name, tags and other properties.
I have done much .Net framework development, but have been ignoring .Net Core and the newer options. I guess I need to dig in and learn about them.

.net core visual studio publish is including runtimes

I'm currently using a .net core visual studio publish, and its including the runtime dlls in the output. I do not want these files, but would like to use the hosting bundles on the server.
This is a new thing, it didn't use to include these dlls with a publish. I think it might have been triggered by a visual studio update, but I am not positive why its happening.
Here's my settings:
The Deployment Mode is not set to "Self-Contained" which I think is the setting that should force the runtime to be included.
I've done tons of googling but can't seem to find any other configuration that would be related to this. Any guidance would be greatly appreciated!
Posting an answer, just in case anyone else ends up here. I found the answer mostly because of Lex Li comment.
The most recent upgrade to visual studio / .net core seems to require you to specificity a target runtime in your Publish Profile. In order to make this work, you will be forced to upgrade your project to 2.1 or higher. Otherwise visual studio will choose portable by default, and include the runtime dlls in the publish.

Azure Functions Visual Studio 2015 intellisense

I'm playing around with Azure Functions but I've noticed VS 2015 doesn't provide intellisense for .csx files.
Any idea how to fix this?
This isn't an issue with your configuration. IntelliSense support with csx is limited and will be improved in future tooling releases (also targeting VS Code and the portal).
Follow this blog post Publishing a .NET class library as a Function App and don't look back. Full C#, (not .csx.), C# 7 (if using VS2017), and full intellisense and debugging.
Basically the approach is instead of creating a Function Project (in VS2015 only), instead create an ASP.NET project and use the Azure Function CLI to create the actual functions. Then you are not dealing with .CSX files which have poor support within the VS editor.
From the New Project dialog, choose ASP.NET Web Application (.NET Framework) with the empty site template.
Open the project properties. In the Web tab, choose Start External Program
For the program path, enter the path to func.exe for the Azure Functions CLI. If you’ve installed the Visual Studio Functions Tooling, the path will look something like C:\Users\USERNAME\AppData\Local\Azure.Functions.Cli\1.0.0-beta.93\func.exe If you’ve installed the Azure Functions CLI through NPM, the path will be something like C:\Users\USERNAME\AppData\Roaming\npm\node_modules\azure-functions-cli\bin\func.exe
For Command line arguments, set host start
For Working directory, specify the root of the web project on your machine (unfortunately, this isn’t set automatically.)
One solution is to develop your Function as a Precompiled function as explained here: https://github.com/Azure/azure-webjobs-sdk-script/wiki/Precompiled-functions
I got fed up very quickly of the limited intellisense.

Running SonarQube against an ASP.Net Core solution/project

SonarQube has an MSBuild runner but .NET Core uses dotnet.exe to compile and msbuild just wraps that. I have tried using the MSBuild runner with no success against my ASP.NET Core solution. Using SonarQube Scanner works kind of.
Any suggestions on how I can utilize SonarQube with .NET Core? The static code analysis is what I am looking for.
With .csproj now being the de-facto format of .Net Core solutions, SonarQube support of such solutions comes with the SonarQube Scanner for MSBuild v2.3 .
It seems .NET Core is switching from project.json to use .csproj and MSBuild. That may be available in .NET Core 1.1 or similar. At that time it may be possible to use MSBuild to build projects and have SonarQube pick that up.
See: https://blogs.msdn.microsoft.com/dotnet/2016/05/23/changes-to-project-json/
Whether that be a good thing or not remains to be seen I guess.
Also see this link for a possible workaround to use MSBuild to build .NET Core projects in the meantime (I have not tested it yet): https://learn.microsoft.com/en-us/dotnet/articles/core/tutorials/target-dotnetcore-with-msbuild
The .NET Core tooling is going to move from project.json to MSBuild based projects. We expect the first version of the .NET Core tools that use MSBuild to ship along with the next version of Visual Studio. However, it is possible to use MSBuild for .NET Core projects today, and this page shows how.
I was able to get .NET Core builds working from the procedure on their site, with two things I'd like to point out….
When you run the SonarQube.Scanner.MSBuild.exe command lines, you must be in the same directory as your project file, and the location directory of the end command must match the start.
When the instructions say you must modify sonar.host.url to point to your server, you must uncomment the entire section of the file before doing the modification. Otherwise you've just inserted your server into a sonar.host.url line that is dead because it is commented out. This made me spin for a while before I really looked….

Resources