Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# dll (built with Microsoft.bcl) in interop(C++ managed) project

we've a C# DLL in .net4.0 but built using using Microsoft.bcl, Microsoft.bcl.async, Microsoft.bcl.build, Microsoft.net.http. These libs got from nuget. We've reasons to not to move to .net4.5 but want to use async, await from these bcl libs. Everything works fine in C# projects, but we couldnt add this DLL in our C++ interop projects, we get this error:

We get this error when we try to add this reference to project.

Even though clr interop project is also in .net4.0 and DLL we are adding is also in .net4.0 we end up getting this error. Is there a way to resolve this?

Error in text format:
---------------------------
Microsoft Visual Studio
---------------------------
Could not add a reference to:

C:\xxx\xxx\xx\xxxHelper.dll



For one of the following reasons:

    - Targets a higher version of the .NET Framework

    - Not a .NET assembly

    - Not a registered ActiveX control
---------------------------
OK   
---------------------------

code to reproduce this issue: https://dl.dropboxusercontent.com/u/1967630/BCL_Problem/oAuth2_SDK_consumer_DLL/BCL_Problem_projects.zipx

like image 650
rplusg Avatar asked Oct 17 '22 23:10

rplusg


1 Answers

I repro, sure looks like a bug. Judging from the error message in your screenshot you are using VS2013. Very unhelpful message, it got toned down in VS2015 but still fails. Odd bug btw, seems like it treats the Microsoft.Threading.Task assembly reference that your C# project uses as a framework assembly. No prior SO questions about this issue that I know of, most programmers get this right without having to fight the machine.

You should consider doing it the way they do it to avoid this error. This only goes wrong if you use the Browse button in the Add Reference dialog. But works just fine when you use a project reference, the way most programmers prefer to configure their solution. So use File > Add > Existing Project, select your C# project. Then add a reference again but this time select the C# project from the Solution > Projects list instead of using the Browse button.

If this is undesirable for some reason then you can work around this another way. Open your .vcxproj in a text editor, Notepad is fine. Locate this element:

   <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

And modify the version number to v4.5. Now add the reference like you did before with the Browse button, no complaints this time. And go back to Notepad and change the version number back to v4.0

like image 139
Hans Passant Avatar answered Oct 21 '22 03:10

Hans Passant