C# reference in .props file - c#

When I add this code directly to the .csproj file, the reference is resolved correctly:
<ItemGroup>
<Reference Include="base_csharp">
<HintPath>D:\Repositories\MDSBuild\MdsDrivers\deps\Base\install\bin\Windows\MSVC\x86\Debug\base_csharp.dll</HintPath>
</Reference>
</ItemGroup>
However, when I put the exact same code in a props file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Reference Include="base_csharp">
<HintPath>D:\Repositories\MDSBuild\MdsDrivers\deps\Base\install\bin\Windows\MSVC\x86\Debug\base_csharp.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
</Project>
And import that props file into the .csproj
<Import Project="D:\Repositories\MDSBuild\MdsDrivers\deps\Base\scripts\props\base_csharp.props" /
I get the warning that the reference could not be resolved:
warning MSB3245: Could not resolve this reference. Could not locate the assembly "base_csharp". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
The code is exactly the same and all the paths are absolute, but for some inexplicable reason it is unable to find the .dll file. What am I doing wrong?

One problem is that you have opened 2 "ItemGroup" tags and closed only one.
If your path is absolute you can give it like this:
<ItemGroup>
<Reference Include= "D:\Repositories\MDSBuild\MdsDrivers\deps\Base\install\bin\Windows\MSVC\x86\Debug\base_csharp.dll" />
</ItemGroup>

Related

GeneratePackageOnBuild NU5118 resources.dll

in my solutions there are two libraries:
The library I want to ship as a nuget package (let's call it EntryLibrary)
The library, where I store the translations using RESX files (Translations)
The translations library csproj looks like (nothing fancy):
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<Compile Update="Strings.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Strings.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Strings.resx">
<Generator>PublicResXFileCodeGenerator</Generator>
<LastGenOutput>Strings.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
The EntryLibrary csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>9.0</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Translations\Translations.csproj" PrivateAssets="all" />
</ItemGroup>
</Project>
For someone like me, who is not very familiar with the nuspec things, I just like the ease of <GeneratePackageOnBuild>, however building this shows the following warning:
C:\Program Files\dotnet\sdk\6.0.401\Sdks\NuGet.Build.Tasks.Pack\build\NuGet.Build.Tasks.Pack.targets(221,5): warning NU5118: File '<Path-to-EntryLibrary>\bin\Debug\netstandard2.0\pl\Translations.resources.dll' is not added because the package already contains file 'lib\netstandard2.0\Translations.resources.dll'
And it won't ship any localized Translations.resources.dll. It only works if all of these are directly part of the EntryLibrary, but I seperated them by intention into another library.
Is there any way to get this working without a dedicated nuspec configuration?
UPDATE
I made a demo solution, which can be found on GitHub

Can't properly add .dll reference in VS Code

I'm trying to add and use shell32.dll to my console project in VS Code. I put it to root folder of project, ..\bin and ..\bin\Debug. I made dotnet restore. Code in my .csproj file is:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Reference Include="Shell32">
<HintPath>shell32.dll</HintPath>
<SpecificVersion>False</SpecificVersion>
</Reference>
</ItemGroup>
</Project>
But when I add using Shell32; to Program.cs it is giving error "The type or namespace name 'Shell32' could not be found". Please advise what am I doing wrong?
You the COM reference to add Microsoft Shell Controls and Automation instead of referencing the actuall Shell32.dll.
Sample XML:
<ItemGroup>
<COMReference Include="Shell32.dll">
<Guid>50a7e9b0-70ef-11d1-b75a-00a0c90564fe</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<WrapperTool>tlbimp</WrapperTool>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
</COMReference>
</ItemGroup>

Prevent content from being copied

I'm using a custom targets file to prevent superfluous copying when running MSBuild on C# projects in a build server. Originally I started with this answer:
<ItemDefinitionGroup>
<Reference>
<Private>False</Private>
</Reference>
<ProjectReference>
<Private>False</Private>
</ProjectReference>
</ItemDefinitionGroup>
This works great for references, but I wasn't able to find any equivalent version to files marked with Content or None tags.
This is what I've come up with, and unfortunately it doesn't work:
<ItemDefinitionGroup>
<Content>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</Content>
<None>
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemDefinitionGroup>
Example: I'd like my_resource.dll to never copy to the output directory. This is how it's defined within the project file:
<ItemGroup>
<Content Include="..\..\..\my_resource.dll">
<Link>my_resource.dll</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
What am I missing? Thanks!

Alea Gpu runtime error not finding a file or dependecy in a Rhino/GrassHopper project

I have a runtime problem with Alea Gpu which doesn't find all related files (dll's I guess). Being new to .NET I have a problem figuring out the correct paths when it doesn't work out of the box, so if anyone can give me some hints based on the paths I present below, it would be gold.
Background
I installed the Alea Gpu packages for VS2017 using Nuget, and my project code compiles and creates the desired GrassHopper component for Rhino3d. This component also shows up properly in RT, but fails with the following error message when entering the the "solver" method containing Alea Gpu calls.
Error message
The error message (my translation from Swedish):
"1. Solution exception:Cannot find the File or the Assembly Alea, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ba52afc3c2e933d6 or one of its dependencies. Cannot find the file."
Perhaps not the most relevant, but here's the failing component with the error message (in Swedish):
The calling .dll (.gha)
The code that runs this so called "GrassHopper definition" (a .gha file which essentially is a .dll) is located in the following folder structure in a VS project where also the Alea package structure is Shown.
The calling GrassHopper (.gha) file:
C:\Workbench\VS\RILGH_Development\RILGH_CSUtils\RILGH_CSUtils\bin\x64\Debug\RILGH_CSUtils.gha
Alea.targets
The content of the file "...\packages\Alea.3.0.3\Alea.targets is as follows :
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<AleaPackageDir Condition="$(AleaPackageDir) == '' Or $(AleaPackageDir) == '*Undefined*'">$(MSBuildThisFileDirectory)..\</AleaPackageDir>
<AleaResourceDir Condition="$(AleaResourceDir) == '' Or $(AleaResourceDir) == '*Undefined*'">$(OutputPath)</AleaResourceDir>
<AleaDoExtractCTResources Condition="$(AleaDoExtractCTResources) == '' Or $(AleaDoExtractCTResources) == '*Undefined*'">true</AleaDoExtractCTResources>
</PropertyGroup>
<UsingTask
TaskName="Alea.Build.ExtractCTResourcesTask"
AssemblyFile="$(AleaPackageDir)tools\Alea.Build.dll"/>
<Target AfterTargets="AfterBuild" Name="AleaExtractCTResources" Condition="$(AleaDoExtractCTResources) == 'true' Or $(AleaDoExtractCTResources) == 'True'">
<Alea.Build.ExtractCTResourcesTask
ResourceAssemblyDir="$(AleaPackageDir)tools\"
ResourceDir="$(AleaResourceDir)" />
</Target>
<Target AfterTargets="AfterClean" Name="AleaCUDACleanCTResources">
<RemoveDir Directories="$(AleaResourceDir)\Alea.CUDA.CT.LibDevice"/>
<RemoveDir Directories="$(AleaResourceDir)\Alea.CUDA.CT.Native.ARM.B32.Linux"/>
<RemoveDir Directories="$(AleaResourceDir)\Alea.CUDA.CT.Native.X86.B32.MacOSX"/>
<RemoveDir Directories="$(AleaResourceDir)\Alea.CUDA.CT.Native.X86.B32.Windows"/>
<RemoveDir Directories="$(AleaResourceDir)\Alea.CUDA.CT.Native.X86.B64.Linux"/>
<RemoveDir Directories="$(AleaResourceDir)\Alea.CUDA.CT.Native.X86.B64.MacOSX"/>
<RemoveDir Directories="$(AleaResourceDir)\Alea.CUDA.CT.Native.X86.B64.Windows"/>
</Target>
</Project>
.csproj settings
<ItemGroup>
<Reference Include="Alea, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ba52afc3c2e933d6, processorArchitecture=MSIL">
<HintPath>..\packages\Alea.3.0.3\lib\net45\Alea.dll</HintPath>
</Reference>
<Reference Include="Alea.IL, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ba52afc3c2e933d6, processorArchitecture=MSIL">
<HintPath>..\packages\Alea.3.0.3\lib\net45\Alea.IL.dll</HintPath>
</Reference>
<Reference Include="Alea.Parallel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ba52afc3c2e933d6, processorArchitecture=MSIL">
<HintPath>..\packages\Alea.3.0.3\lib\net45\Alea.Parallel.dll</HintPath>
</Reference>
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
<HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="RhinoCommon">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files\Rhinoceros 5 (64-bit)\System\RhinoCommon.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Grasshopper">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files\Common Files\McNeel\Rhinoceros\5.0\Plug-ins\Grasshopper (b45a29b1-4343-4035-989e-044e8580d9cf)\0.9.76.0\Grasshopper.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="GH_IO">
<SpecificVersion>False</SpecificVersion>
<HintPath>C:\Program Files\Common Files\McNeel\Rhinoceros\5.0\Plug-ins\Grasshopper (b45a29b1-4343-4035-989e-044e8580d9cf)\0.9.76.0\GH_IO.dll</HintPath>
<Private>False</Private>
</Reference>
</ItemGroup>
... last rows in .csproj :
<Import Project="..\packages\Alea.3.0.3\build\Alea.targets" Condition="Exists('..\packages\Alea.3.0.3\build\Alea.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Alea.3.0.3\build\Alea.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Alea.3.0.3\build\Alea.targets'))" />
<Error Condition="!Exists('..\packages\Fody.1.29.4\build\dotnet\Fody.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Fody.1.29.4\build\dotnet\Fody.targets'))" />
</Target>
<Import Project="..\packages\Fody.1.29.4\build\dotnet\Fody.targets" Condition="Exists('..\packages\Fody.1.29.4\build\dotnet\Fody.targets')" />
Hopefully this is enough info about paths and where to make any modifications so that the runtime files can be found, given the path to the individual calling GrassHopper file ("C:\Workbench\VS\RILGH_Development\RILGH_CSUtils\RILGH_CSUtils\bin\x64\Debug\RILGH_CSUtils.gha") and the general paths where the GrassHopper execution platform searches for related component dll's (a path which Alea seemingly doesn't care much about), "C:\Program Files\Common Files\McNeel\Rhinoceros\5.0\Plug-ins\Grasshopper (b45a29b1-4343-4035-989e-044e8580d9cf)\0.9.76.0\Components\". I have told GrassHopper to search in both these directories, and even tried dropping copies of the Alea dll's in this (the last) folder, but no go.
Q: So how does package library like Alea actually control the paths it uses in runtime based on the files and path (se above) which I can control?
Reading through the posts tagged aleagpu was not helpful to me, nor the sparse troubleshooting info on the alea documentation pages (possibly due to being newbie with .Net and VS)
Thanks in advance.
// Rolf
It seems that this is an old unsolved problem in Grasshopper.
Anyway, I probably have a solution for that, at least it worked for me.
From this discussion:
http://www.grasshopper3d.com/forum/topics/how-do-i-install-a-custom-ghx?id=2985220%3ATopic%3A168876&page=2#comments
To summarize, to solve the issue:
Copy the Alea.dll file to thisdirectory
C:\Program Files\Rhinoceros 5 (64-bit)\System\
In Rhino type the command
GrasshopperDeveloperSettings
and uncheck the option:
Memory load *.GHA assemblies using COFF byte arrays
It could probably work even without launching Rhino as Admin and with the COFF box checked, but i haven't tried yet.

Include file in solution explorer without it being a build dependency

How can I include a file in the list of files in solution explorer without including it as a dependency for compilation?
I have a .targets file that generates .cs files, similar to the examples in this answer.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<CoreCompileDependsOn>$(CoreCompileDependsOn);GenerateCode</CoreCompileDependsOn>
</PropertyGroup>
<ItemGroup>
<Sources Include="..\sources\*.txt" />
</ItemGroup>
<Target Name="GenerateCode" Inputs="#(Sources)" Outputs="#(Sources->'generated\%(Filename).cs')">
<!-- run executable that generates files -->
<ItemGroup>
<Compile Include="generated\*.cs" />
</ItemGroup>
</Target>
</Project>
This builds correctly and consecutive builds don't rebuild the project unnecessarily. The resulting .cs files are not visible in the solution explorer. The generated code also isn't found by intellisense.
If I add the files with ItemGroups in the .csproj, the generated files are visible in the solution explorer, but subsequent builds result in rebuilding the project unnecessarily. The genereated code still isn't found by intellisense.
<ItemGroup>
<Sources Include="..\sources\*.txt">
<Link>sources\%(Filename)%(Extension)</Link>
</Sources>
<!-- using None instead of Compile on the next line makes no difference -->
<Compile Include="#(Sources->'generated\%(Filename).cs')">
<Generator>MSBuild:Compile</Generator>
<Link></Link>
</Compile>
</ItemGroup>
How can I tell msbuild that the .cs files included in the project are inconsequential to the build and therefore shouldn't trigger rebuilding the entire project?
Move the code generation to BeforeCompile instead of CoreCompileDependsOn. this will keep the generation of the files from tirggering the subsequent builds.
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="BeforeCompile" DependsOnTargets="GenerateCode">
</Target>
<Target Name="GenerateCode" Inputs="#(Sources)" Outputs="#(Sources->'generated\%(Filename).cs')">
<!-- run executable that generates files -->
</Target>
</Project>
If you include all of the generated files in the .csproj, the visual studio intellisense will work.
<ItemGroup>
<Sources Include="..\sources\*.txt">
<Link>sources\%(Filename)%(Extension)</Link>
<LastGenOutput>generated\%(Filename).cs</LastGenOutput>
</Sources >
<Compile Include="#(Sources->'generated\%(Filename).cs')">
<Link></Link>
</Compile>
</ItemGroup>

Categories

Resources