Using net5.0 in T4 templates in Rider - t4

I'm a little confused about compatibilities between netstandard, netframework, netX and how mono fits into the picture...
I understand that Rider's T4 engine runs on mono, but does that mean I can't use net5.0 assemblies in my T4 templates?
Currently, I have a net5.0 project, referencing net5.0 nuget assemblies.
In my T4 templates, I'm referencing the assembly DLLs in bin/Debug/net5.0 with `<# assembly name="...">
When I run the T4 templates in-proc in a net5.0 Console Application via Mono.TextTemplating.TemplateGenerator.TemplateGenerator, then the templates work.
However, if I right-click a template in the Solution explorer and select 'Run Template', I get a list of errors that core System.* libs are missing, e.g.
Generate.tt(21, 25): [CS0012] The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Is this a bug in Rider?

Due to implementation details, Rider compiles T4 files targeting a version of .NET Framework, that's why it has problems with files referencing net5.0 assemblies. As far as I understand what I see in Mono.TextTemplating, they target the runtime they are launched in instead. To solve your problem I'd suggest trying Mono.TextTemplating as a command-line tool - it might be able to compile files with net5.0 references. To integrate that external tool into Rider, you can go to File > Settings > Tools > Custom Tools, disable Bundled T4 Template Executor and add a custom tool for Mono.TextTemplating CLT.
To answer your question: yes, it probably can be called a bug

Related

Why is Visual Studio 2022 building my project in Release build when settings are set for Debug build?

I am trying to debug an assembly in a web project. The web project has a reference to the assembly. I have the code for the assembly. I have set the assembly project whose target framework is .Net Standatd 2.0 to Debug build as shown below.
I copied the generated .dll and .pdb files to the \bin\debug\net50 folder of the web project (targets .NET 5.0). During debugging the web project, I noticed in the modules window that Visual Studio loaded the assembly as Optimized and Skipped loading symbols. The dll and .pdb files have the same timestamps. I am not sure why the assembly's symbols are not being loaded.
Then I used this Assembly Information tool and it showed the dll as Release and optimized. So that explains why VS is loading it as such?
My question is why is it being built as Release and optimized?
My goal is getting VS to load it where I can set breakpoints and single step through the assembly code.
There was an Optimize property set to true in the csproj file. Changed it to false.

Nuspec: can I have an assembly as a dependency but instruct visual studio to NOT reference it?

I'm developing a set of assemblies which contain classes that, using a dependency injection framework, are supposed to be instantiated only by an "InstanceProvider" class --basically that's my gateway to SimpleInjector's GetInstance(). Then I'm packaging these assemblies as a nuget package.
My goal is to enforce that a developer does
var myDuck = InstanceProvider.GetInstance<IDuck>();
and doesn't do
var myDuck = new Duck();
For this, I have to avoid referencing some of the assemblies, the ones that contain the concrete implementations. I still need them to be there though.
So for now, I have them as dependencies in my nuget package. I'm looking for a way to keep them there, but when a developer uses the package for her project, some of the assemblies should not be directly referenced in their visual studio project.
Is this even possible?
Nuspec: can I have an assembly as a dependency but instruct visual studio to NOT reference it?
Since you do not want to those assemblies directly referenced in their visual studio project when you uses the package for her project, only keep them there. You can set those assemblies in the content files or tools files, like:
<file src="\*.dll" target="content\" />
<file src="\*.dll" target="Tools\" />
Check Creating the .nuspec file for some more details.
With this way, those assemblies included in the nuget package, but those assemblies would not directly referenced to the project when you use the nuget package.
Hope this helps.
General answer you can, but you should not bother about it till you really need to develop "plugins" enabled system. This turns debugging more complicated.
But continue to devide interface and realization - it is not related to dynamic loading, but helps you to devide code on layers.
There is a lot of DI tools that can this e.g. Unity. The techinque they use: you configure container in xml file that not require referencing. Then container search for assmeblies and load them.

Mixed targets in VSTS build

We have an aspnet-core application which consists of several .net standard libraries, two aspnet-core mvc applications and, exceptionally, a .net framework console application.
These all build successfully, both in solution in Visual Studio, and also individually using msbuild/dotnet build commands.
We are now trying to implement continuous integration/build with VSTS.
We have found that the aspnet-core web applications build successfully, but the .net framework console application fails with multiple error messages like the following:
2017-12-24T07:20:26.4090447Z C:\Program
Files\dotnet\sdk\2.0.3\Microsoft.Common.CurrentVersion.targets(1988,5):
warning MSB3245: Could not resolve this reference. Could not locate the
assembly "Xxx.Core, Version=1.1.0.0, Culture=neutral,
processorArchitecture=MSIL". Check to make sure the assembly exists on disk.
If this reference is required by your code, you may get compilation errors.
[d:\a\1\s\WinApps\ListBuilder\ListBuilder.csproj]
C:\Program Files\dotnet\sdk\2.0.3\Microsoft.Common.CurrentVersion.targets(1988,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "Microsoft.EntityFrameworkCore, Version=2.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [d:\a\1\s\WinApps\TelemListBuilder\TelemListBuilder.csproj]
The console application follows the same folder structure as the web applications but seems unable to find any of the referenced packages.
Our Agent queue is Hosted VS2017.
We have two VSTS tasks: Restore and Build from the asp.net core build template.
If necessary we would be happy if we could exclude the console app from the build.
Since there are general .net framework project and .net core project and you want to build them together, you need to use Visual Studio Build task.
So, remove .Net Core Restore and .Net Core build tasks and add NuGet Tool Installer (4.3.0), Nuget restore and Visual Studio Build tasks (Visual Studio Version: Latest or Visual Studio 2017)
If you want to generate the deployment package during the build, you can specify MSBuild arguments like this:
/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)"

Visual Studio 2015 C++ solution/project setup for interchangeable dependency use case

Since Visual Studio 2010 the option to specify C++ project dependencies in the solution is deprecated (What does the “Link Library Dependency” linker option actually do in Visual Studio 2010?).
However hard-linking a library dependency in the project file is not always convenient. I would like to keep the dependency interchangeable.
For example I have a project defining unittests for a set of libraries.
I want to execute the unittests for different versions of the library (each defined in its own vcxproj).
Pre-VS-2010 one solution file could be created for each library dependency, requiring only a single project file for the unittests.
Now the "preferred" way to do it would be to duplicate the unittest project files for each library dependency, which is undesired.
The .lib output of the dependency could be manually added to the "Additional Dependencies" in the linker options, but this feels like a hack.
What is the best practice for modeling these kind of dependencies in Visual Studio 2015?

How to reference assembly from GAC?

I have installed the strong named assembly TestReflection into the GAC (I am using .NET 4.0 and VS 2010).
Different versions of the TestReflection DLL are in GAC of .NET 4.0 (C:\WINDOWS\Microsoft.NET\assembly\GAC_32\TestReflection\), however, the assembly does not appear in the "Project" -> "Add reference" box of VS 2010.
How can I refer to my assembly deployed in GAC at design time from another project?
This page says that:
You cannot add references from the Global Assembly Cache (GAC), as it is strictly part of the run-time environment.
Referring to this statement, I would like to know how to make your project's DLL shared assembly for other consumers if it's the requirement?
The dll's shown in the .Net tab of the "Add references" dialog are not actually the ones registered in the GAC. They are found by searching a few paths on your filesystem.
The paths being searched are located by Visual Studio by looking up the following registry entries:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NetFramework\{Version}\AssemblyFoldersEx\
There should be some keys added there already, so if you want your own dll to show up on the .Net tab, you can add it to one of the folders defined there. You could also add a new registry key pointing to a custom folder, which would only contain your own dll's.
The GAC is only meant for loading assemblies at runtime after your application has been deployed, so I don't think you should use it while developing. When you deploy your app, make sure to set "Copy local" to false on your reference so the dll won't be copied to the bin folder, and then install it into the GAC and it will be loaded from there instead.
Another simple option would be to manually edit the project file as XML in visual studio (You will have to unload the project first), and simply add node <Reference Include="<name of dll>" /> in MSBuild project file. After reloading the project, VS will pick up the reference without problem.
If you want to add Global Assembly Cache references to your VS2010 project, there is an extension you can use: Muse.VSExtensions.
It has some quirks but does a decent job. Check it out...
The answer is the Reference Paths in the property windows, you have to set it with the GAC path
Please see my post here:

Resources