Referencing a .NET 2 assembly in a project targeting .NET 4 - c#

I am currently using TeeChart Lite (the free version of teechart). I would like to use it in my WPF project however the downloadable free version is only available for .NET 2.0 therefore I get the following error:
Warning 1 The referenced assembly
"TeeChart.Lite" could not be resolved
because it has a dependency on
"System.Design, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" which
is not in the currently targeted
framework
".NETFramework,Version=v4.0,Profile=Client".
Please remove references to assemblies
not in the targeted framework or
consider retargeting your project.
Is it safe to just add the assembly System.Design, Version 4.0.0.0 even though it is not part of the targeted framework? Or is there something else I can do to get it working in .NET 4.0?

I don't have Visual Studio available to test, but it's probably one of these two things:
More likely, you just need to reference System.Design.dll, which is a standalone DLL.
Less likely: as the error message states, you are targeting the 4.0 client framework, which is the default for new projects in VS2010. Go to your project properties and reference the full 4.0 framework, which might solve the problem.
The actual solution may require combining both steps.

Related

Failed to load .NET framework 4.0 dll from .NET 6 project

I have a .NET 6 project and try to load a third-party dll, which build with .NET framework 4.0, dependencies are mscorelib, System.Configuration, System.Data, System.Drawing and System.Core, all are version 4.0.
When I run into the method, the application crashes with exception:
System.IO.FileNotFoundException: Could not load file or assembly '**dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
Seems there are some version conflict between dependent assemblies, but some .NET framework 2.0 dlls works fine. How to resolve this problem if I cannot ask the dll verdor to build a new one?
If a library uses functions for the .NET Framework, it cannot run on the .NET Core. If it doesn’t, building to the .NET Standard allows it to run on both. Either:
The library is not usable on the .NET Core, in which case your out of luck,
it has separate builds for .NET Framework and .NET Core, in which case you would need to choose the right one,
or the library developer only compiled it for the .NET Framework when it could be compiled otherwise, in which case you could try to compile it yourself.

.NET Framework 4.6.1 application has errors referencing .NET Standard 1.4 class library

I've been trying to wrap my head around the differences between the .NET Frameworks, .NET Core and .NET Standard. From what I've been reading, .NET Standard is kind of the lowest common denominator, and that libraries written using .NET Standard should be compatible with code written using the other frameworks.
But I've created a solution with a class library that uses .NETStandard 1.4 and an application that uses .NET Framework 4.6.1, and it appears the application is not able to use that library.
Wherever the application attempts to use a class from the class library, I get errors such as the following.
Error CS0012 The type 'IEnumerator<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Error CS0012 The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
Error CS1579 foreach statement cannot operate on variables of type 'HtmlMonkey.HtmlNodeCollection' because 'HtmlMonkey.HtmlNodeCollection' does not contain a public definition for 'GetEnumerator'
Can someone help me round out my understanding of these libraries such that my application can use my class library?
This is a known problem regarding compatibility of netstandard 1.4 and below with .NET 4.6.1 - 4.7 projects. It is tracked in .NET Standard GitHub repo as an issue #503 Referencing NETStandard.Library 2.0.0 in net461-net47 project and only using ns1.4 (or lower) libs doesn't work
Description of the problem and working workaround is below:
NETStandard.Library 2.0.0 package doesn't install netstandard1.x packages in net461-net47 projects. This is because we expected the support package to always be present on net461 and later, but when that support was implemented we dialed it back to only turn on when a netstandard1.5 or later library was referenced.
As a result installing a netstandard1.0-1.4 library in a net461-47 project and referencing NETStandard.Library 2.0.0 package will have missing dependencies.
One workaround is to use the NETStandard.Library 1.6.1 package instead. This still has the dependencies on the individual library packages to bring in the facades.
An alternative workaround is to set ImplicitlyExpandNETStandardFacades=true in the project file. This will enable all the facades for ns2.0 assemblies.
I don't remember the exact sequence was used to create my original projects. As mentioned in the comments, I had both Visual Studio 2017 and the new Visual Studio 2017 Preview installed on my machine. It's even possible I used different versions to create each project in my solution.
Either way, I used the Visual Studio 2017 Preview to recreate the solution, again with the class library using .NET Standard, and a WinForms application that uses .NET Framework. And it appears to be working just fine. (I did not need to install anything manually with NuGet.)
So not a very clear resolution. I originally assumed it was a more concrete issue related to which frameworks I was referencing. But perhaps I'll leave the question in case anyone gets in a similar situation with the same error messages.

Can we have a DLL built in .Net V2.0 references another built in .Net V4.5?

I need to have a DLL built in .net v2.0 to be called from SQL Server 2008 as an external DLL (CLR Assembly). This dll must be in .net v2.0 to be compatible with our SQL Server version and is only to make a bridge between SQL Server and other C# features developed in .net V4.5. So i refence another DLL built in .Net V4.5 but I can't compile it! I have the fallowing warning:
warning MSB3258: The primary reference "my_dll" could not be resolved because it has an indirect dependency on the .NET Framework assembly "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which has a higher version "4.0.0.0" than the version "2.0.0.0" in the current target framework.
Can we ignore the assembly version compability ? Or can we have another solution to invoke any function developed in .net V.4.5 from a SQL Server 2008 ?
I'm not sure I'm following your question but to answer it you just need change the project dependency to .NET 4.5. .NET frameworks can reference other framework versions but you have to consider the order of dependency. If the 2.0 is using a 4.0 library then it will fail because it doesn't have a reference of it's own to the components. When your project is compiled it's placed into a single Assembly and that assembly must have reference to all ingredients being used. However; if your projects are separate dll's which is sounds like they are then you can reference between them as long as the 4.0 members aren't exposed publicly in the assembly. However; that means the dll requires an environment with 4.0 installed in order for itself to operate.
In order to run different versions of the same assemblies you would need to add an assemblyBinding to your project configuration.
It's a bit hard to follow but look at this post and see if it helps you better understand how to achieve this.
Post
As per this answer here, it is possible to reference the .Net 4.5 assembly by exposing the components required as COM. All versions of .Net are based on COM at it's core, so it can be used as a 'lowest common denominator' channel of communication. Depending on your requirements, you may also want to look into using Named Pipes, IPC or even Web Services.

Compiling transformation: The type 'Object' is defined in an assembly that is not referenced

I'm making some changes in an asp.Net MVC5 webapp, in which I used typelite to create .ts definitions from C# classes (really handy). For some reason now I've got this error when executing the T4:
Compiling transformation: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'.
and this warning:
Compiling transformation: Assuming assembly reference 'mscorlib, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' used by 'EnvDTE' matches identity 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' of 'mscorlib', you may need to supply runtime policy Mair.Tracking.Web
I guess it's something related to the EnvDTE version used by TypeLite and the reference to mscorlib EnvDTE uses. Should I add a bindingRedirect in the web.config?
I'm using VS2015 with Update2 CTP.
I can confirm that Portable class libraries in combination with T4 templates cause this problem in Visual Studio 2015 Update 2. I wasn't able to find better solution then retargeting TypeLITE to full .NET framework.
From the version 1.8.0 TypeLITE nuget package contains a copy of the library that targets full .NET framework. If your project targets full .NET framework, the correct TypeLITE assembly is picked up during installation and everything works fine in Visual Studio 2015 Update 2. Unfortunately if your project targets PCL, you are out of luck with Visual Studio 2015 Update 2. I am looking for better solution.
Thanks to Ian Yates for opening the Connect item.
Microsoft re-released KB3151378 on May 4, 2016. I can confirm that this update addresses the issue when T4 templates are used with EDMX files. Unfortunately I do not use TypeLite, so I am unable to confirm that it addresses the OP's issue, though I would suspect it does.
From the KB page:
Issue 6:
In T4 templates, errors occur when you reference an assembly that's built for a version of the .NET Framework that differs from the desktop (full) framework. For instance, errors occur when you reference portable libraries.
It looks like this should be fixed by KB3151378.
Notes for version 14.0.25130.0:
Issue 6
In T4 templates, errors occur when you reference an assembly that's built for a version of the .NET Framework that differs from the desktop (full) framework. For instance, errors occur when you reference portable libraries.
Specifically for TypeLite, I opened my copy of it (fairly out of date but I've tweaked it to suit my needs) and stopped it being a portable library. That was done following the steps at https://stackoverflow.com/a/23135170/1053381. Now that it's not a portable library I'm able to execute my T4 template again.
I have many other T4 templates in my solution using ImmutableObjectGraph (search GitHub) and they ran fine despite it also using a portable library. I didn't dig too far into the specifics but I've learnt that mscorlib 2.0.5.0 is associated with portable libraries, etc and really we don't need TypeLite itself to be portable.
Hope this helps others.
There's also an open Connect issue at https://blogs.msdn.microsoft.com/visualstudio/2016/03/03/visual-studio-2015-update-2-rc/ since this is still a regression in VS2015 Update 2.
I had the full version of the TypeLite 1.5.1 installed (Regular WebApplication .Net Full version 4.5.1) and simple updating to 1.8.1 helped to resolve the issue. So I should say that VS 2015 Update2 RC and Update2 RTM doesn't work neither with the FULL version of the TypeLite nor with the PORTABLE (yes, version 1.5.1 already has the portable version)

Application settings error after changing target framework of project

In my application I am using user settings as explained here.
Then I realized that in VS 2010 I was using .NET 4.0 while only .NET 2.0 was sufficient.
When I changed the framework and build the project, in my code whenever I access setting now, I get the following error:
An error occurred creating the
configuration section handler for
userSettings/Vegi_Manager.Properties.Settings:
Could not load file or assembly
'System, Version=4.0.0.0,
Culture=neutral,
PublicKeyToken=b77a5c561934e089' or
one of its dependencies. The system
cannot find the file specified.
(C:\Users\AKSHAY\AppData\Local\Microsoft\Vegi-Manager.vshost.exe_Url_44035dlkzpfaaauiqsd4nh3f0l0yq0tv\1.0.0.0\user.config
line 5)
It is for unknown reasons using version 4.0.
Please suggest what should I do.
Check out your app.config maybe you still have System.Configuration.UserSettingsGroup or something of that sort of version 4 still lurking around, you will have to manually edit and get the relevant for version 2
I was having the same problem having started developing my Outlook add-in in Visual Studio 2010 targeting the .NET 4.0 framework and then deciding to change it to the 3.5 framework. It seems Visual Studio was NOT smart enough to update my app.config file which still had a reference to 4.0.
As previous posters have suggested (I'll be just a little bit more specific), by manually editing app.config to change all references from "Version=4.0.0.0" to "Version=2.0.0.0" (apparently .NET 3.5 still uses 2.0.0.0) in the <sectionGroup> element and its child elements, I was able to get the settings working again in my case.
It is likely that you have a reference to a .NET 4 assembly in your solution - if you open the "Add Reference" window in your solution and make the window a bit wider, you will see that there are columns for Version and Runtime... when you created your solutions to target .NET 4, you might have added a reference to an assembly that requires the .NET 4 runtime and now you have changed to .NET 2, you need to swap it for an assembly that only needs the .NET 2 runtime.
Of course, if you have used something form the .NET 4 assembly that didn't exist in .NET 2, you will either have to re-write to avoid using it or change your mind and do it in .NET 4 instead!
if you open your app.config, make sure all the config sections are targeting .net framework 2 not 4
if you post your app.config to me i can help more
May I add here that you need to change the app.config details / references to the previous .NET framework (in this case 4.0.0.0) to the new System refence .NET version (in this case 2.0.0.0 which is the same for .NET 3.5!)

Categories