Using VS Code to program C# against custom .DLL with Intellisense - c#

In Visual Studio, I am able to add a custom reference to my C# project:
Solution Explorer -> Dependencies -> Add Project Reference -> ...RevitAPI.dll
After which the code editor supports auto-completion, syntax highlighting, etc. (Intellisense).
Is there a way to accomplish this in VS code? I am referencing the assembly like this in my .csproj-file:
<ItemGroup>
<Reference Include="RevitAPI">
<HintPath>C:\Program Files\Autodesk\Revit 2022\RevitAPI.dll</HintPath>
</Reference>
</ItemGroup>
Also in relation to this assembly reference, I am experiencing an issue when building my project. After using dotnet build, all the referenced assemblies are also included in the Debug folder, not just the .dll-file itself. There is an option --no-dependencies to dotnet build, but this didn't work for me.
Is there any way to exclude the referenced assemblies from my build?

Related

Visual Studio project reference that isn't version specific at runtime

We have a multi-project solution. The references between projects are done as Project References rather than Assembly Reference (as one would expect). This works fine for our deployment, but creates a runtime dependency that is version specific. The trouble is that we would like to start creating hot fix installers that only update the specific dlls that changed. Updating all dlls is not an option for our current customer situation.
The 'Specific Version' property on project references is disabled and i'm having trouble finding out a workaround other than switching to Assembly References and using Choose blocks in the csproj to switch between debug/release bins based on build config.
Is there another alternative to allow any version to be used at runtime?
MAINTENANCE FREE
The approach we went with was to set the Assembly version to be a fixed number and only update the File version when we build.(we used to keep both in sync with each other). We went with this approach since it was maintenance free and let us keep our references by Project.
The assembly version is what .net uses to find specific versions of a dependent dll. File version is what will display if you view property details on a file via windows explorer.
If you want to be able to hotfix any dll then you'll need to update all of your Assembly References to be versionSpecific=false and set all of your projects to have a fixed assembly version. If you only want to be able to hotfix specific project dlls then you need only fix the assembly version on those projects. The referencing projects could then keep whatever assembly version scheme you want.
Assembly version is set in ProjectFolder/Properties/AssemblyInfo.cs. We've now fixed ours to be 1.0.0.0 and only increment the file version when building.
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.2.3.45678")]
REQUIRES MAINTENANCE
If you are unable to set a fixed Assembly version then another approach can be to use a File reference. The trouble here is that the path your project dlls will vary based on your active build configuration (debug vs release). To get around this you can make your reference be conditional based on the config.
The major downside being that you'll need to maintain the build sequence manually. Also, if you add a new project then you'll need to remember to use these dynamic references again.
<Choose>
<When Condition="'$(Configuration)'=='Release'">
<ItemGroup>
<Reference Include="Your.AssemblyName">
<HintPath>..\Your.AssemblyName\bin\x86\release\Your.AssemblyName.dll</HintPath>
</Reference>
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<Reference Include="Your.AssemblyName">
<HintPath>..\Your.AssemblyName\bin\x86\debug\Your.AssemblyName.dll</HintPath>
</Reference>
</ItemGroup>
</Otherwise>
</Choose>
You can also leave Debug as being a project reference if so desired, this would allow you to see what build sequence visual studio automatically generates due to the project references. You would then be able to mimic that sequence for your release config.

Replace DLL refs with Project refs for project dependencies in Visual Studio C# solution

Is it possible to programmatically replace DLL refs with Project refs for project dependencies in Visual Studio C#/VB.NET solution?
BACKGROUND:
I'm working with some legacy code where dependencies for each project are mostly referenced as compiled DLLs instead of including project reference from corresponding project in solution or even worse - referenced straight from GAC!
Right now I have to manually remove each DLL reference and replace it with project reference from VS UI for each solution out of dozens projects.
Editing the project/solution XML .csproj/.sln files is not straightforward due to GUIDs:
<!--typical DLL reference-->
<ItemGroup>
<Reference Include="MyDLL, Version=2.0.1.0, Culture=neutral, PublicKeyToken=1b6d1e0267e1acba, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>...\MyDLL.dll</HintPath>
</Reference>
</ItemGroup>
<!--typical Project reference-->
<ItemGroup>
<ProjectReference Include="..\MyDLL\MyDLL.csproj">
<Project>{3cc27830-3d6b-4071-85e5-5a4006f11142}</Project>
<Name>MyDLL</Name>
</ProjectReference>
</ItemGroup>
Use this plug-in .. It might help on your problem
https://visualstudiogallery.msdn.microsoft.com/056617a4-da39-4d7b-8ecf-933009d9b721
It has description below
Switches references from file to projects (and vice versa) references
when adding projects. References are reverted when the project is
removed.
usage:
You are developing project A which has a file reference to assembly
"b.dll". At some point you need to make changes to "b.dll" . So you
add project "B" to the solution. The ReferenceSwitcher will detect
that project B produces an assembly called "b.dll" and ask if you want
to switch all references from "b.dll" to project B.
Then at some point later, you no longer need project B in your
solution, so you remove project B. The reference switcher will
detected that references to B used to point to "b.dll" and ask you if
you would like to update them.
More info here:
http://markkemper1.blogspot.com/2011/09/project-to-file-reference-switcher-for.html
Edit:
there are lots of plug-in available to solve your purpose check them out
https://visualstudiogallery.msdn.microsoft.com/197d94f6-6276-471d-853d-a5a322ccb08c
OR search them all
https://visualstudiogallery.msdn.microsoft.com/site/search?f%5B0%5D.Type=SearchText&f%5B0%5D.Value=reference&pageIndex=2

Visual Studio project platform dependent references

I have a WP project for which I use a runtime module from a separate project.
If I reference the runtime module project from the main project, the platform/configuration (e.g.: x86/Debug vs ARM/Release) is handled automagically by visual studio at build time.
Now, I would like to remove the project dependency and only reference the binaries from the main project in such a way that when I chose a specific platform/configuration the correct reference will be used to build.
For example if I build for ARM/Release it should use the binaries from ./lib/ARM/Release/MyLibrary.winmd and if I build for x86/Debug it should use the binaries from ./lib/x86/Debug/MyLibrary.winmd.
I tried multiple ways but still could not find a solution that works both for Visual Studio and msbuild.
I actually have it working making the hint path use Platsform and Configuration variables.
<Reference Include="MyLibrary, Version=255.255.255.255, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\$(Platform)\$(Configuration)\MyLibrary\MyLibrary.winmd</HintPath>
</Reference>
You might be able to use some macros dependent on your build selection in VS.
Example the two macros found within the linker are as follows:
$(ProcessorArchitecture) which for my example = x86
$(ProcessorArchitectureAsPlatform) which for me = Win32
and depending on the configuration you selected it will build in either Debug / Release.
Similar to what Pinco said.

NuGet - Dependee project referencing different DLL according to selected build configuration

I'm completely new to NuGet and just researching/proof-of-concept'ing it at the moment:
Is it possible to package a NuGet package in such a way that the dependee project (the one that has the dependency) references a different DLL according to the project configuration build?
Example:
Dependee-Debug.DLL -> References Dependency-Debug.DLL
Dependee-Release.DLL -> References Dependecy-Release.DLL
Similarly, I'd need to repeat this behaviour for 32-bit/64-bit discrimination. If this is possible, is there a tutorial that explains how, anywhere? I can't find any mention of this functionality.
You should be able to reference different assemblies based on the current build configuration by using either a PowerShell script or using custom MSBuild targets file. Note that using an MSBuild targets file will work cross platform in MonoDevelop and Xamarin Studio where as a PowerShell script will not.
NuGet allows you to include an MSBuild targets file so you can change what happens at build time. In the MSBuild targets file you can have the references and make them conditional based on the current build configuration.
In the build directory of your NuGet package you add an MSBuild .targets file with the same name as your NuGet package id. You can also have different .targets files for a particular target framework (e.g. Net40) by having it under a Net40 subdirectory if you need to.
build\MyPackageId.targets
Then in the MSBuild .targets file you can do something simple such as have the references conditionally added.
<ItemGroup Condition=" '$(Platform)' == 'x86' ">
<Reference Include="MyAssembly">
<HintPath>x86\MyAssembly.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition=" '$(Platform)' == 'x64' ">
<Reference Include="MyAssembly">
<HintPath>x64\MyAssembly.dll</HintPath>
</Reference>
</ItemGroup>
It is possible to run PowerShell scripts during package install to edit the CSPROJ file to conditionally reference different dependencies.
Check this question for a couple of useful links.

How to reference different version of dll with MSBuild

I have a web application project which utilises a set of 3rd party dll's. The issue is that the dev/staging environment is 32bit, but production is 64bit. As such, we have to re-reference and build the solution each time we want to deploy. I am now wanting to automate this, but unsure how to go about it with MSBuild?
All other dll's are the same, just the 3 3rd party dll's.
EDIT
I have made some headway, however am coming up with some runtime assembly issues.
I have 3 dll files, 1.dll, 2.dll, 3.dll. The file version is 5.1 for each. For the 64 bit dlls, the names are exactly the same, just difference file versions. What I have done, is renamed each one to 1.v5.dll, 1.v6.dll etc. In my project files, I am then referencing each dll as follows:
<Reference Include="1.v5.dll" Condition="'$(Platform)'=='x86'">
<SpecificVersion>False</SpecificVersion>
<HintPath>bin\1.v5.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="1.v6.dll" Condition="'$(Platform)'=='x64'">
<SpecificVersion>False</SpecificVersion>
<HintPath>bin\1.v6.dll</HintPath>
<Private>False</Private>
</Reference>
This works in Visual Studio IDE, and my solution compiles file, however when I go to run the website, I get the following error...
"Could not load file or assembly '1.v5' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.
Any thoughts on how to approach this?
You can create conditional references in the project file like this:
<Reference Include="32bit.dll" Condition="'$(Platform)'=='x86'"/>
<Reference Include="64bit.dll" Condition="'$(Platform)'=='x64'"/>
To use this inside VS, you have to create two solution platforms: one for the x86 target and one for the x64 target. Depending on the active platform one of the dlls will be selected, no need for re-referencing.
To automate this using msbuild, create a new project file that builds the other project file a number of times, each time for a different platform/configuration/...:
<Target Name="BuildAll">
<MSBuild Targets="myproject.proj" Properties="Platform=x86;Configuration=Debug"/>
<MSBuild Targets="myproject.proj" Properties="Platform=x64;Configuration=Debug"/>
<MSBuild Targets="myproject.proj" Properties="Platform=x64;Configuration=Release"/>
</Target>
Have a look at the MSBuild task reference for aditional options like building in parallel.
This is what I have figured out, and seems to work no problems.
I have created 2 solution platforms, x86 and x64. I have created a new folder in my solution directory called "References", and created n x86 and x64 folder:
\References\x86\
\References\x64\
The 3 dll's for each are then placed in their respective directories.
In each project's file, I have then added the following references:
<Reference Include="{Reference1}" Condition="'$(Platform)'=='x86'">
<HintPath>..\References\dlls\x86\{Reference1}.dll</HintPath>
</Reference>
<Reference Include="{Reference2}" Condition="'$(Platform)'=='x64'">
<HintPath>..\References\dlls\x64\{Reference2}.dll</HintPath>
</Reference>
Now, when I develop within the IDE, I am working the the relevant dll specific to my needs.
I have then just added a post-build event which copies the dll based on the $(Platform) variable into the bin directory.

Categories