Unable to resolve assemblies that use Portable Class Libraries - .net-4.0

I'm having a problem with assembly resolution on an end-user machine and I believe it's related to using Portable Class Libraries....
I have a .NET 4.0 application that was originally written in Visual Studio 2010. Recently we upgraded to Visual Studio 2012 and we've created a few projects that are Portable Class Libraries. I don't believe we need these features now, but we're also building a Windows 8 Store application that might benefit from these libraries.
When I compile my project, what exactly does the portable library feature do? I expect that it allows me to run it on different frameworks without modification or recompiling.
When I look at the library in reflector dotPeek it shows the Platform attribute as:
.NETPortable,Version=v4.0,Profile=Profile5
And the references seem 2.0-ish:
mscorlib, Version=2.0.5.0
System, Version=2.0.5.0
System.Runtime.Serialization, Version=2.0.5.0
When I run the application on this end-user's machine, I see an error in the log file:
Could not load file or assembly, 'System.Core, Version=2.0.5.0...'
Googling System.Core 2.0.5.0 seems to refer to SilverLight -- which appears to be one of the targeted frameworks.
This machine does not have Visual Studio installed, but has .NET 4.0 (4.0.3 update)
Is there something I should be doing differently to compile, something I should investigate in my dependencies or something I should be looking to install on the end-user machine? What does the 2.0.5.0 refer to?

For .NET 4, you need an update (KB2468871) for Portable Class Libraries to work. From the KB Article:
Feature 5
Changes to the support portable libraries. These changes include API
updates and binder modifications. This update enables the CLR to bind
successfully to portable libraries so that a single DLL can run on the
.NET Framework 4, on Silverlight, on Xbox, or on the Windows Phone.
This update adds public Silverlight APIs to the .NET Framework 4 in
the same location. The API signatures will remain consistent across
the platform. All modifications are 100 percent compatible and will
not break any existing code.
Also see the "Deploying A .NET Framework App" section of the MSDN Portable Class Library Documentation.
EDIT: Actually, if the machine has .NET 4.0.3 installed as you mention, that should be sufficient. Can you double-check to make sure that it is actually installed?

Related

C++/CLI Support in .Net Core

Our project structure is like,
native.dll :- This contains pure native code written in c\c++.
This native.dll exposes some functions using *def file.
Wrapper Library(wrapper.dll compiled with .Net framework v4.0) :-
In order to use functionality of native.dll, a Wrapper lib(wrapper.dll)
is written in C++\CLI using :clr\oldsyntax. This wrapper has all
code of Interoperability and Marshalling.
Application(Console App v4.0) directly uses wrapper.dll to use functionality provided
by native.dll.
Now this project needs to run in .Net Core. This means we will have an
.Net Core application that will reference wrapper.dll that in turn will refer
native.dll.
I know this will not directly work. But the issue is whether .Net Core(CoreCLR) supports
C++\CLI (clr\oldsyntax) runtime environment ?
If no, what can be the possible solutions to this application work ?
whether .Net Core(CoreCLR) supports C++\CLI (clr\oldsyntax) runtime environment ?
As far as I know there is no plan to support C++/CLI with .NET Core.
If no, what can be the possible solutions to this application work ?
You can (should) provide a C API. Mono e. g. supports P/Invoke and .NET Core also supports P/Invoke (see also this Stack overflow question and this DllMap related ticket).
Update (2022-09-02): This answer is from 2016. See the other answers (e.g., this) for what is possible with recent .Net Core versions.
Officially announced eventually...
(next wish... support linux # .Net 5 ^^)
https://devblogs.microsoft.com/cppblog/the-future-of-cpp-cli-and-dotnet-core-3/
C++/CLI will have full IDE support for targeting .NET Core 3.1 and higher. This support will include projects, IntelliSense, and mixed-mode debugging (IJW) on Windows. We don’t currently have plans for C++/CLI for targeting macOS or Linux. Additionally, compiling with “/clr:pure” and “/clr:safe” won’t be supported for .NET Core.
The first public previews for C++/CLI are right around the corner. Visual Studio 2019 16.4 Preview 1 includes an updated compiler with “/clr:netcore”
Updat: From replied of origin url:
"We are still working on the IDE and MSBuild integration, so I can’t share a sample project quite yet. Once it’s available, likely with 16.4 Preview 2 or 3"
(16.4 Preview1 cannot create C++/CLI with .NetCore project.)
191015
16.4 Preview2 Released.
I'v tried asp.net core 3.1 with c++/CLI dll, it works.
(need set plateform to x64 both asp.net core and c++/CLI dll)
.net Core team will only commit (now?) to supporting C++/CLI for Windows only.
The intention was to deliver it for .net Core 3.0. While I haven't found explicit mention of it yet in the release notes, C++/CLI support was a prerequisite for delivering WPF (windows-only), which is now supported in .net Core 3.0.
Support mixed-mode assemblies on Windows - #18013
This issue (#18013) will track progress toward supporting loading and running
mixed-mode assemblies on CoreCLR. The main goal is to provide support
for WPF and other existing C++/CLI code on .NET Core. Some of the work
will be dependent on updates to the MSVC compiler.
The github issue (#659) mentioned above by #Tomas-Kubes, Will CoreCLR support C++/CLI crossplat? - #659, is about cross-platform C++/CLI.
BTW, I am getting compiler warnings on "clr\oldsyntax" with VS2017/.net-4.7. So this compiler flag is already deprecated.
UPDATE: This isn't coming till .Net Core 3.1
Another potential solution (though obviously quite a difficult task) if you want to stick with C++ (i.e. expose an OO interface to .NET) might be to have a look at CppSharp from the mono project. It is able to expose native C++ code through an automatically generated C# wrapper. It supports Windows, Linux as well as OSX. However, I don't know if the generated code can be compiled to a .NET standard target (didn't try to). I can only suppose it would because the generated code does not use any fancy API (it is basically interop and marshalling code); and, by the way, it is also possible to customize the generation process (although, once again, probably not an easy task).
For those who are looking at this for general .Net Core stuff without specific clr parameters (as this is a high result on google) Microsoft have written a guide on how to port C++/CLI to .Net Core:
https://learn.microsoft.com/en-us/dotnet/core/porting/cpp-cli
Port a C++/CLI project
To port a C++/CLI project to .NET Core, make the following changes to the .vcxproj file. These migration steps differ from the steps needed for other project types because C++/CLI projects don't use SDK-style project files.
Replace <CLRSupport>true</CLRSupport> properties with <CLRSupport>NetCore</CLRSupport>. This property is often in configuration-specific property groups, so you may need to replace it in multiple places.
Replace <TargetFrameworkVersion> properties with <TargetFramework>netcoreapp3.1</TargetFramework>.
Remove any .NET Framework references (like <Reference Include="System" />). .NET Core SDK assemblies are automatically referenced when using <CLRSupport>NetCore</CLRSupport>.
Update API usage in .cpp files, as necessary, to remove APIs unavailable to .NET Core. Because C++/CLI projects tend to be fairly thin interop layers, there are often not many changes needed. You can use the .NET Portability Analyzer to identify unsupported .NET APIs used by C++/CLI binaries just as with purely managed binaries.
Build without MSBuild
It's also possible to build C++/CLI projects without using MSBuild. Follow these steps to build a C++/CLI project for .NET Core directly with cl.exe and link.exe:
When compiling, pass -clr:netcore to cl.exe.
Reference necessary .NET Core reference assemblies.
When linking, provide the .NET Core app host directory as a LibPath (so that ijwhost.lib can be found).
Copy ijwhost.dll (from the .NET Core app host directory) to the project's output directory.
Make sure a runtimeconfig.json file exists for the first component of the application that will run managed code. If the application has a managed entry point, a runtime.config file will be created and copied automatically. If the application has a native entry point, though, you need to create a runtimeconfig.json file for the first C++/CLI library to use the .NET Core runtime.
There are some more nuances but these are the actual steps to port

Creating a .NET application without the entire framework?

I am in the process of creating a lightweight application in vb.NET, and was wondering if it is possible to create an application that uses some of the dll's within the .NET 2.0 framework, without actually using the framework itself.
For instance, can I take the dlls from the .NET directory that the app will reference (and only those dlls that it references), put them into a folder inside the app's directory, and then just change the reference path to that new location? That way, when the app is installed on pc's without .net framework installed, the dlls can just be zipped and copied over to the newly installed app directory.
This may all sound a bit confusing, just let me know if you need any more info.
It is quite unlikely that you'll be able to do that.
There is this thing called .net framework client profile for creating lightweight .net apps that do not need the whole framework but you still need to have that installed.
I would recommend using something like Delphi if you need a standalone exe that does not depend on anything.
There is concept of Client Profile in .NET 3.5 and .NET 4.
That is subset of .NET, smallest necessary fraction of .NET that client must have in order to be able to run .NET applications targeting client profile.
There is more detailed overview of the features you may use when targeting client profile.
Unless you move to .Net 4 or above, you need full framework on the client computers. Having said that, which OS are you targeting? Newer MS OS often come with .Net framework installed already.

Is there a portable class library profile which supports System.Xml.Linq and System.Windows for .NET 4.0

I've been working with the Portable Class Libaries using the Visual Studio 11 Beta release.
I've had quite a lot of success getting WP7, Silverlight and .Net 4.5 working together within "Profile104".
However, I've not found a way to get .Net 4 supported within the same or similar profile. In particular, I'm looking for a profile where System.Xml.Linq and System.Windows.Input.ICommand are available?
Is there any way to do this? Either by using a standard profile, or by creating my own profile and then adding type forwarding?
Thanks
Stuart
No, ICommand (as well as INotifyCollectionChanged and ObservableCollection) is not available in a portable library that targets a version of .NET before 4.0. We had to make some changes in .NET to support it (mainly adding System.Windows.dll with type forwards), which aren't available on earlier versions.
I believe System.Xml.Linq is available for portable libraries on .NET 4.0.3 and above.

Confusion over system libariries that ship with mono

I've installed mono 2.10 along with monodevelop and have used monodelveop to code some apps under ubuntu. Several of these apps needed to access to the JavaScriptSerializer, which is part of the System.Web.Script.Serialization namespace.
In order to be able to instantiate and use a JavaScriptSerializer, I had to add a reference to my app in monodevelop - but I see there appear to be two packages that I can chose from: one named "system.web.extensions" and the other named "mono".
If I use the first package (system.web.extensions) I get a runtime error when using the JavaSerializer. I tried using the "mono" reference instead and it works fine.
So far so good. But now, I want to run this same app under windows and I'm getting a "cannot load assembly" error for System.Web.Exceptions.
I'm confused at this point: Why are there two packages shipped with mono/monodevelop for some of these assemblies, and which one(s) should I be using in order to be cross-platform?
Thanks!
Michael
Why are there two packages shipped with mono/monodevelop for some of
these assemblies?
Mono assemblies are kinds of internal assemblies: they provide additional features, not included in the .NET Framework. The System assemblies rely sometimes on these (i.e. Mono.Web for ASP.NET Web services).
Which one(s) should I be using in order to be cross-platform?
Always use the System ones. The Mono assemblies are not available under .NET Framework (unless you ship them manually).
Mono 2.10 provides two versions of the System.Web.Extensions assembly: 1.0.61025.0 and 4.0.0.0. Be sure to use the 4.0 version.
If the problem persists, could you post your code? (for me, no problem using the JavaScriptSerializer)

Using undocumented native code APIs to install items into the GAC

I'm working on a project whose setup uses the APIs documented in Microsoft Knowledge Base article KB317540 to install and uninstall assemblies into the GAC. The KB article states:
SUMMARY
The native code application
programming interfaces (APIs) that
allow you to interact with the Global
Assembly Cache (GAC) are not
documented in the .NET Framework
Software Development Kit (SDK)
documentation.
MORE INFORMATION
CAUTION: Do not use
these APIs in your application to
perform assembly binds or to test for
the presence of assemblies or other
run time, development, or design-time
operations. Only administrative tools
and setup programs must use these
APIs. If you use the GAC, this
directly exposes your application to
assembly binding fragility or may
cause your application to work
improperly on future versions of the
.NET Framework.
The GAC stores assemblies that are
shared across all applications on a
computer. The actual storage location
and structure of the GAC is not
documented and is subject to change in
future versions of the .NET Framework
and the Microsoft Windows operating
system.
The only supported method to access
assemblies in the GAC is through the
APIs that are documented in this
article.
Most applications do not have to use
these APIs because the assembly
binding is performed automatically by
the common language runtime. Only
custom setup programs or management
tools must use these APIs. Microsoft
Windows Installer has native support
for installing assemblies to the GAC.
For more information about assemblies
and the GAC, see the .NET Framework
SDK.
Use the GAC API in the following
scenarios: When you install an
assembly to the GAC. When you remove
an assembly from the GAC. When you
export an assembly from the GAC. When
you enumerate assemblies that are
available in the GAC. NOTE:
CoInitialize(Ex) must be called before
you use any of the functions and
interfaces that are described in this
specification.
What are the pros and cons of this technique? Are these APIs safe to use? Anyone out there using them? If so, why?
Why not just create a Windows Installer package? See Demystifying the .NET Global Assembly Cache. Quoting:
Developers of Windows Installer
packages can install assemblies to the
GAC using Microsoft Windows Installer
2.0. This is the preferred way for installing such shared assemblies and
should be the only way shared
assemblies are installed on non
development machines.
It says, "Only administrative tools and setup programs must use these APIs."
There are tools (e.g. command-line tools) to work with the GAC. Why not invoke these executables, instead of using these APIs directly?