regasm netstandard DLL - com-interop

I have a C# project that I'd like to use as a .NET Standard 2.0 library for other .NET assemblies and as a COM component for native code. I could create the COM object just fine when I targeted the full .NET Framework, but when I created a new project targeting .NET Standard, I got this error when I tried register it:
regasm /codebase /tlb MyLib.dll
RegAsm : error RA0000 : Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
How can I build and register a DLL that is compatible with both .NET Standard and COM?

Ensure that .NET 4.7.1 is installed. Even then, since Visual Studio doesn't copy NuGet packages to the output directory, expect to get a similar "Could not load file or assembly" error for one of those.
If you can live with a single project but two DLLs, this is an alternative: In your .csproj, remove the TargetFramework line and replace it with multiple targets, according to what your code needs, for example:
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
Then use the net461 DLL for use by COM and the netstandard2.0 for use by .NET.

Related

How to configure Visual Studio Setup Package to deploy class library to GAC that has additional libraries in deployment folder

We're using Visual Studio 2022 to create and deploy a .NET Framework 4.7.2 Class Library.
The installation of the Class Library into the GAC is being achieved by adding the "Global Assembly Cache Folder" to the "File System on Target Machine" configuration within the setup package.
This appears to work and the library is successfully installed in the GAC after installation. However, an application referencing the DLL throws an exception for a DLL that is referenced by our class library
System.IO.FileNotFoundException: Could not load file or assembly 'System.Text.Json, Version=7.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified
The referenced DLL is in the deployment folder and if the application is run from that folder it executes without throwing the exception.
Is there a way in the setup project to ensure the DLL that is installed in the assembly cache uses the deployment folder copy of the DLL or uses that folder as the default directory? Or is there another way this should be achieved?
We've considered putting the library (and referenced libraries) in the 3rd party application folder but this is overwritten every time that application is upgraded which will be reasonably frequently. This would require re-deploying the DLL every time this happens on every machine.

Package .NET framework DLL and use it in .NET standard project

I have a vendor provided DLL which is based on .NET Framework. I do not have access to code and I only have the DLL. I have packaged this DLL into a nuget package and now I am trying to refer this into a .NET standard project. But I am getting warning as - Package 'MyPackage 0.0.1' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project..
If I refer the DLL directly into a .NET standard project it doesn't show this warning. But using it after creating a nuget package shows that.
I also tried creating a .NET standard lib project, added DLL reference and then created a nuget package but still it was showing same warning.
Is there another way of doing it? Vendor doesn't provide DLLs targeting other frameworks and I have to use it after packaging it into Nuget because of requirements.
In my side, I test the situation as you described. Create a net standard lib project called test, then use this node to include into the package.
1) create a net standard lib nuget project called test and only add this node under test.csproj file:
<ItemGroup>
<None Include="xxx\xxx\xxx.dll"(the whole path of the net framework dll) Pack="true" PackagePath="lib\$(TargetFramework)"></None>
</ItemGroup>
2) right-click on the net standard test project-->click Pack button and you can get the new version of the nuget package.
Before you install the nuget package into another main project, you should first clean nuget caches or delete all files under C:\Users\xxx(current user)\.nuget\packages.
And I did not face the issue in my side with all the above steps.

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)"

Wrong FSharp.Core version looked for in a Xamarin.Forms project

In a Xamarin.Forms project I am getting an error on Windows 8.1 and UWP:
Could not load file or assembly 'FSharp.Core, Version=3.259.3.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.
The actual version of F# used throughout the solution is 3.7.4.0:
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile>
<TargetProfile>netcore</TargetProfile>
<TargetFSharpCoreVersion>3.7.4.0</TargetFSharpCoreVersion>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
There is an F# PCL project and C# platform-specific projects. The error occurs only when using F# libraries from Nuget in the PCL. These packages tend to use:
<TargetFSharpCoreVersion>3.259.3.1</TargetFSharpCoreVersion>
I have tried the following, but they do not change the error:
Putting an app.config file with a binding redirect to 3.7.4.0 in the F# project and/or the build directory
Installing the FSharp.Core Nuget package into all projects
Very grateful for any help.
Edit: in the original question there was an incompatibility between the profile (111) and the intended TargetFSharpCoreVersion (3.7.4.0) which was causing additional issues.

I can't Add dll to References in Visual Studio 2010

I work on WinApp in Visual Studio 2010 (With C# Language)
I want to add one dll (not system dll) to Reference.
but when I open Add Reference Window And Browse dll see under error message:
Could not load file or assembly 'W2D_D2.dll' or one of its
dependencies. The module was expected to contain an assembly manifest.
this file may not be a managed assembly.
I even change the Target Platform from ".net framework 4 client profile" to ".net framework 4" but not difference.
How Can Add This dll To My Project?
Looks like this DLL is not a managed assembly. You can check this very quickly by trying to open it with ILSpy or Reflector.
If this is the case, you'll have to pinvoke or use com to use this dll.

Resources