Can't properly add .dll reference in VS Code - c#

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>

Related

How to make VS to copy nuget packages dll's to output directory

I'm trying to use material design in the first time on my WPF project I install the package from nuget but when I'm trying torun it it's prompt:
System.Windows.Markup.XamlParseException: 'Could not load file or
assembly 'MaterialDesignThemes.Wpf, Culture=neutral,
PublicKeyToken=df2a72020bd7962a'. The system cannot find the file
specified.'
When I'm copying the assemblies manually to the output folder it's works.
My .csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<BaseOutputPath>$(SolutionDir)\bin\</BaseOutputPath>
<OutputPath>$(SolutionDir)\bin\</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<UseCommonOutputDirectory>true</UseCommonOutputDirectory>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.6.1" />
<PackageReference Include="ShowMeTheXAML.MSBuild" Version="2.0.0" />
</ItemGroup>
</Project>
I tried to add
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
but it does nothing.
Here is my .csproj
<Project Sdk="Microsoft.NET. Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.6.1" />
</ItemGroup>
</Project>
Auto copy is fine. Your problem did not occur. Have you tried recreating a project, does the same problem occur?
If that still doesn't work, you may need to add the msbuild event to copy the file. Use xcopy.

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

Build Error with External Assembly in .NET6 Blazor Webassembly App

I'm trying to create Blazor Webassembly App with .Net6 using Visual Studio 2022(preview). And I've referenced external assembly file in that project. When I build the project, I got the following error,
The asset 'D:\Office\Testing\BlazorApp1\BlazorApp1\bin\Debug\net6.0\wwwroot_framework\ClassLibrary1.dll' can not be found at any of the searched locations 'D:\Office\Testing\BlazorApp1\BlazorApp1\bin\Debug\net6.0\wwwroot_framework\ClassLibrary1.dll' and 'ClassLibrary1'
screen shot image
Is there a solution for that ?
Project's Properties here
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ServiceWorkerAssetsManifest>service-worker-assets.js</ServiceWorkerAssetsManifest>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0-rc.1.21452.15" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.0-rc.1.21452.15" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<Reference Include="ClassLibrary1">
<HintPath>Lib\ClassLibrary1.dll</HintPath>
<EmbedInteropTypes></EmbedInteropTypes>
</Reference>
</ItemGroup>
<ItemGroup>
<ServiceWorker Include="wwwroot\service-worker.js" PublishedContent="wwwroot\service-worker.published.js" />
</ItemGroup>
</Project>
You just need to change this,
<ItemGroup>
<Reference Include="../Lib/ClassLibrary1">
<HintPath>Lib\ClassLibrary1.dll</HintPath>
<EmbedInteropTypes></EmbedInteropTypes>
</Reference>
</ItemGroup>

.NET Core 2.1 app publishes from VS2017 but not command line?

I turned OFF the TreatWarningsAsErrors, as i kept getting errors when publishing while self-contained is set to true. Now VS2017 publishes successfully, but the command line dotnet publish still reports the same errors. How can fix this?
Example of the errors i receive:
error NU1605: Detected package downgrade: System.Runtime.InteropServices from 4.3.0 to 4.1.0. Reference the package directly from the project so select a different version.
VS2017 Publish settings that actually works:
cmd line that doesn't work:
dotnet publish "c:\myproject.csproj" -f netcoreapp2.1 -c "Debug" -o "c:\users\me\dekstop\publish" --self-contained true -r win-x64
EDIT added csproj contents*
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
<Platforms>x64;x86</Platforms>
</PropertyGroup>
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<EnableDefaultCompileItems>False</EnableDefaultCompileItems>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\GlobalInfo\GlobalAssemblyInfo.cs">
<Link>Properties\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Configuration\CrossPlatformConfiguration.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Registrations\SetupModule.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Autofac">
<HintPath>..\..\packages\autofac\4.9.2\lib\netstandard2.0\Autofac.dll</HintPath>
</Reference>
<Reference Include="log4net">
<HintPath>..\..\packages\log4net\2.0.8\lib\netstandard1.3\log4net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\proj1.csproj" />
<ProjectReference Include="..\..\proj2.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="app.config">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\_Keys\Private\MyXkey.snk</AssemblyOriginatorKeyFile>
<AssemblyName>Test.Setup</AssemblyName>
<RootNamespace>Test.Setup</RootNamespace>
<DelaySign>false</DelaySign>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x86'">
<OutputPath>..\..\Bin\x86\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<OutputPath>..\..\Bin\x64\Debug\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<OutputPath>..\..\Bin\x64\Release\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x86'">
<OutputPath>..\..\Bin\x86\Release\</OutputPath>
</PropertyGroup>
What is happening differently here that allows VS2017 to publish the executable?
ProgrammerMan led me to the solution, which was to add
<NoWarn>$(NoWarn);NU1605</NoWarn>
to the csproj of each project in the solution.
Not an answer, but some more information that may eventually help towards a 'real' solution...
I believe you will find the same NU1605 errors in VS2017, but the difference is they are treated as warnings so the publish completes successfully.
After publishing you can see the warnings in the Output window - View > Output and then Show output from: Build.
I find the errors/warning are linked to the target runtime (-r) and anything
other than 'Portable' (or blank) will cause them.
I have updated to .NET Core 2.2.107 and still get them.
I also get the same with my .NET Standard 2.0 project.
I have also muted them by adding the same NoWarn suggestion.

'HttpClientHandler' does not contain a definition for 'ClientCertificates' in net461 and net47?

I'm making a NuGet package and my csproj looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.6;net461;net47</TargetFrameworks>
...
<PackageId>Package2</PackageId>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Package1\Package1.csproj" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461' OR '$(TargetFramework)'=='net47'">
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>
The project it's referencing looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup Label="Globals">
<SccProjectName>SAK</SccProjectName>
<SccProvider>SAK</SccProvider>
<SccAuxPath>SAK</SccAuxPath>
<SccLocalPath>SAK</SccLocalPath>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard1.6;net461;net47</TargetFrameworks>
<PackageId>Package1</PackageId>
...
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<NoWarn>1701;1702; CS1591</NoWarn>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.6' OR '$(TargetFramework)'=='net461'">
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461' OR '$(TargetFramework)'=='net47'">
<Reference Include="System.Net.Http" />
</ItemGroup>
</Project>
And inside the Package2 project it has the following line:
webRequestHandler.ClientCertificates.Add(certificate);
But it isn't building in net461 or net47, saying:
'HttpClientHandler' does not contain a definition for
'ClientCertificates' and no accessible extension method
'ClientCertificates' accepting a first argument of type
'HttpClientHandler' could be found (are you missing a using
directive or an assembly reference?)
Package2(net461),
Package2(net47)
But the only thing I've changed is removed this from the csproj for package2:
<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.0'">
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="2.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='netstandard1.6' OR '$(TargetFramework)'=='net461' OR '$(TargetFramework)'=='net47'">
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="1.1.2" />
</ItemGroup>
Can anyone help me understand how to fix it? I don't really want to bring in that whole NuGet package just to fix this one dependency I think I'm just missing an assembly but as far as I know it should be in System.Net.Http which I should already be referencing (and so should the other project it's already dependent on).
Looks like this property is available from version 4.7.1:
https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.clientcertificates?view=netframework-4.7.1
Note that if you select an older .NET version, you'll see a message like this:
The requested page is not available for .NET Framework 4.7.
For me the solution was to download the newest version of "System.Net.Http".
You can find it through the NuGet package manager.
At the time of writing v4.3.4 is the newest version and it contains the ClientCertificates methode.
I've swapped out the assembly reference for the NuGet package:
<ItemGroup Condition="'$(TargetFramework)'=='net47' OR '$(TargetFramework)'=='net461'">
<PackageReference Include="System.Net.Http" Version="4.3.4" />
</ItemGroup>
pretty sure this is the wrong thing to do™ as the assembly reference in .NET Framework should be sufficient and I'm probably going to suffer many conflicting reference issues anywhere I install my package, but this is the best I can come up with right now.

Categories

Resources