Adjust runtime identifier of .NET Interactive Notebook - c#

I have a legacy assembly that is still on .NET Framework 4.8 and references System.Windows.Forms.dll (for non-UI reasons, actually) and I would like to use it inside a .NET Interactive Notebook through also existing code that loads this assembly using Assembly.LoadFrom.
However, if I do that, I end up getting an exception Could not load file or assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.. So in my opinion, essentially the runtime is not looking into the .NET 4 GAC, because it is not a .NET 4 CLR and then expecting the assembly to be located in the same folder.
In applications that are under control, we had some good success simply changing the target framework moniker to net6.0-windows in that case, but recompile obviously is not an option for interactive notebooks.
I have seen that there is a runtimeconfig.json in the Nuget cache folder for the entry DLL:
{
"runtimeOptions": {
"tfm": "net6.0",
"frameworks": [
{
"name": "Microsoft.NETCore.App",
"version": "6.0.0"
},
{
"name": "Microsoft.AspNetCore.App",
"version": "6.0.0"
}
],
"configProperties": {
"System.GC.Server": true,
"System.Reflection.Metadata.MetadataUpdater.IsSupported": false,
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
However, changing the tfm setting there did not seem to work, either. What am I doing wrong?

I forgot to paste the solution here in case anybody runs into the same problem. I actually just copied the assembly from the GAC into the application folder and then, .NET Core was able to find the assembly.

Related

How do I auto-restore NuGet packages for my .NET6 custom control when an application references it?

How do I auto-restore NuGet packages for my .NET6 custom control when a .NET6 application references the custom control DLL itself?
Is there any instruction I can give to my assembly to auto-restore NuGet packages when necessary? Otherwise, when you build the app you get this error:
error MC1000: Unknown build error, 'Could not find assembly
'System.Drawing.Common, Version=6.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. Either explicitly load this assembly
using a method such as LoadFromAssemblyPath() or use a
MetadataAssemblyResolver that returns a valid assembly. Line 14
Position 97.'
The assembly *.json file is like the following:
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v6.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v6.0": {
"Manufacturer.ProductName/1.0.0": {
"dependencies": {
"Microsoft.CSharp": "4.7.0",
"System.Buffers": "4.5.1",
"System.Configuration.ConfigurationManager": "6.0.0",
"System.Data.DataSetExtensions": "4.5.0",
"System.Management": "6.0.0",
"System.Memory": "4.5.4",
"System.Numerics.Vectors": "4.5.0",
"System.Runtime.CompilerServices.Unsafe": "6.0.0",
"System.ServiceModel.Primitives": "4.9.0",
"System.Text.Encoding.CodePages": "6.0.0"
},
"runtime": {
"Manufacturer.ProductName.dll": {}
}
},
"Microsoft.Bcl.AsyncInterfaces/5.0.0": {
"runtime": {
"lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll": {
"assemblyVersion": "5.0.0.0",
"fileVersion": "5.0.20.51904"
}
}
},
"Microsoft.CSharp/4.7.0": {},
"Microsoft.Extensions.ObjectPool/5.0.10": {
"runtime": {
"lib/net5.0/Microsoft.Extensions.ObjectPool.dll": {
"assemblyVersion": "5.0.0.0",
"fileVersion": "5.0.1021.41022"
}
}
},
"Microsoft.Win32.SystemEvents/6.0.0": {
"runtime": {
"lib/net6.0/Microsoft.Win32.SystemEvents.dll": {
"assemblyVersion": "6.0.0.0",
"fileVersion": "6.0.21.52210"
}
},
"runtimeTargets": {
"runtimes/win/lib/net6.0/Microsoft.Win32.SystemEvents.dll": {
"rid": "win",
"assetType": "runtime",
"assemblyVersion": "6.0.0.0",
"fileVersion": "6.0.21.52210"
}
}
},
...
You don’t need to restore Nuget packages for your custom control when an application reference it. Firstly, you should know what is a DLL and how it works. For more information about dll you can refer to:DDL
Here’s an example I test in VS to explain dll file.
1 When you reference a nuget package(Newtonsoft.Json) in a project A ,it will install this package to your global-packages folder that has default path and you can override the folder path.global-packages
2 when you build project A, it will generate a .dll file in your Debug/Release folder that can be referenced by other projects later.
At the same time, the Nuget packages will be generated as dll in the same folder.
3 when you reference .dll file from project A in project B ,you can right click project B and select “And Referenc”.After adding .dll file, you will see reference under Dependencies in project B.You can use function or method in project A by adding .dll file into Dependencies without restoring Nuget Packages.
When a program or a DLL uses a DLL function in another DLL, a dependency is created. The program is no longer self-contained, and the program may experience problems if the dependency is broken.From your error information, it cannot find the System.Drawing.Common when building app.
Here’re suggestions you can check:
1 check the build folder in your your project (.NET6 custom control) and make sure whether the package.dll exists.
2 check your application project’s dependencies list and make sure the dll file is referenced successfully.
3 check your dll file and make sure Whether it is removed from the computer.
4 check your dll file and make sure whether it is overwritten with an earlier version or upgrated.
At lats, you can try to build a new dll file and add it to reference again.

Azure Queue Storage WebJob with .NET Core

I've got 2 projects which are all .NET core. An infrastructure project which defines the schema, models, commands, queries etc. and an API which consists of endpoints/controllers to work on the data. The entire setup resides on Azure with the database being an Azure SQL.
The system is for analytics, so the entire setup is based around Read Only API's and what not. However, I obviously need to insert data into the database. The data comes from software out of my environment, so my idea was to create a WebJob which would receive the payload on an Azure Queue, use NewtonSoft JSON to populate my models and insert it into the database.
If I created a normal Azure WebJob it would use a .NET version which was not compatible with .NET Core, so I instead setup my project as here. In order to set my web job up, i needed to configure it:
public static void Main(string[] args)
{
var config = new JobHostConfiguration
{
DashboardConnectionString = "",
StorageConnectionString = ""
};
var host = new JobHost(config);
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
In order to use RunAndBlock(); as well as the configuration I need to reference mscorlib, which I do in my project.json:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.Azure.WebJobs": "1.1.2",
"Microsoft.Azure.WebJobs.Core": "1.1.2",
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.3"
},
"Microsoft.NETCore.Portable.Compatibility": "1.0.1",
"Microsoft.WindowsAzure.ConfigurationManager": "3.2.3",
"WindowsAzure.Storage": "8.0.1"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dnxcore50",
"net451"
]
}
},
"publishOptions": {
"include": [
"run.cmd"
]
},
"runtimes": {
"win10-x64": {}
}
}
Reading here regarding the "Microsoft.NETCore.Portable.Compatibility" package, it says:
As such, when using this package you may encounter errors like
error CS0012: The type 'WebRequest' is defined in an assembly that is
not referenced. You must add a reference to assembly
'System.Net.Requests, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a'.
Which is exactly what I get,
Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The located assembly's manifest definition does not match the assembly reference.
I am, however, not entirely sure how to fix this. My dependency foo is not super strong, and I'm not sure if I'm even able to get Azure Queue storage + webjobs to play nicely with .NET core
So how do I fix it, if it is fixable?
According to your scenario, I tried to build the NETCore console application for my WebJob and I could encounter the same error as you provided. Then I found some similar issue and issue about WebJob SDK in NETCore, and as David Ebbo stated as follows:
To clarify, Azure WebJobs that use .NET Core work fine. It's specifically the use of the WebJobs SDK that is not yet supported.
Considering your scenario, I assumed that you need to build normal WebJobs and the related library (models,queries,etc.) instead of NETCore, in order to use WebJobs SDK. Or you need to re-design your scenario.
In case somebody is still looking for a solution, this is supported in the recent version
Install-Package Microsoft.Azure.WebJobs -Version 3.0.0-beta4
Please note that this package is still in beta. You can check for the latest updates at https://www.nuget.org/packages/Microsoft.Azure.WebJobs/3.0.0-beta4

FileNotFoundException: Could not load

I'm working on a .net Core project, using the .net Core framework, I wanted to use DropNet package with .net Core so I added a reference to net452 framework inside my project.json:
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6",
"portable-net45+win8",
"net452"
]
}
},
Now when I try to create a DropNet client object in my controller class I've got an error:
Error CS0012 The type 'IWebProxy' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'. Test3..NETCoreApp,Version=v1.0
Ok in order to fix that error I've installed microsoft.netcore.portable.compatibility package, and the error is gone, the thing is that when I do that and execute the project and try to get access to the View that I'm returning from the controller action where I'm using DropNet I get a FileNotFoundException :
FileNotFoundException: Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
I'm having that problem only with that view, this is my Action Method:
public IActionResult Test()
{
DropNetClient client = new DropNetClient("gsfgsgsfg438", "6ysgsgs234cf", userToken: "w5gdfgsdfg3434d4", userSecret: "239usfgsghsf3434wyqo");
return View();
}
The problem is that DropNetClient line, if a comment that line, the view is showed, but if that line is active in there, the FileNotFoundException is thrown...
Please, don't abuse the import statement. It is there to override nuget validation when restoring packages, to install libraries which are compatible (PCL which target .NET 4.5 and Windows 8/8.1+) but do not yet have the netstandard1.6 target.
Don't EVER use it to force import net452 libraries into a .NET Core application. It won't work!!! .NET 4.5.2 libraries aren't compatible with .NET Core!!!
You only have two options:
Don't use any libraries which do not support netstandard, netcoreapp or portable-net45+win8!!!
If you need some libraries that only run with net452 and there is no replacement, then target net452 instead of netcoreapp1.0

The type 'DateTime' is defined in an assembly that is not referenced?

Created a new Class Library (Package) project
Changed the project.json to look like this (added project dependencies):
project.json
{
"version": "1.0.0-*",
"description": "WMI.ECM.PCMS.Services.Acomba Class Library",
"authors": [ "WILL" ],
"tags": [ "" ],
"projectUrl": "",
"licenseUrl": "",
"dependencies": {
"Acomba.SDK": "2.0.0",
"WMI.ECM.Inventory.Models": "",
"WMI.ECM.Inventory.Services": "",
"WMI.ECM.Inventory.Services.Contracts": ""
},
"frameworks": {
"dnx451": { },
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Runtime": "4.0.21-beta-23516",
"System.Threading": "4.0.11-beta-23516"
}
}
}
}
Compiled the solution, and I get this error:
The type 'DateTime' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PunlicKeyToken=b77a5c561934e089'.
Yet, I do not have this problem with my other projects which also target DNX451 and DNXCORE50. Also shall I mention that the mscorlib.dll is actually referenced in the project in question per this screenshot: Referenced assemblies.
As a result, I did some searches which end up clueless.
I tried to add dependencies to the dnx451{} in my project.json like so:
"dnx451": {
"dependencies": {
"Microsoft.CSharp": "",
"System.Runtime": ""
...
}
}
Because I remember having read such a solution somewhere, and it didn't work.
Most of the answers are for an older version of a web project which solve it by adding a reference in the web.config. Well, Class Library (Package) has no web.config.
The type ‘System.Object’ is defined in an assembly that is not reference (MVC + PCL issue)
CS0012: The type 'System.Web.Routing.RouteValueDictionary' is defined in an assembly that is not referenced
I also downloaded and installed this patch:
Microsoft .NET Framework 4.5.2 Developer Pack, which changed nothing.
And another one which I can barely understand what is discussed because they are talking about the facades for each and every version of the framework or something like it.
The type 'System.Object' is defined in an assembly that is not referenced
It looks like a common problem although I can't find any helpful solution to the actual problem.
Currently using
Visual Studio Community 2015 - Version 14.0.25123.00 Update 2
.NET Framework 4.6.01055
ASP.NET MVC 6 Installed
SideWaffle for Xunit DNX Unit Test Project Template
Visual C# 2015
UPDATE
It appears to be caused by the COM library which I imported using this tool from the VS CMD line: Tlbimp.exe (Type Library Importer).
In short, the AcoSDK.dll is a COM library which I imported using the above-mentioned tool to create a .NET assembly with all the COM types resolved as .NET types.
Then, I used NuGet.exe to create a package which I published over my private feed.
And then, I referenced the package in my project as "Acomba.SDK": "2.0.0" in my project.json.
Whenever I use a DateTime property from this package, the build error occurs. For example, this causes the error to occur:
public DateTime CreatedAt { get { return p.PrTimeModified; } }
And this doesn't:
public DateTime CreatedAt { get { return new DateTime(); } }
The COM type for a .NET DateTime is Date. However, it's supposed to be seen as a DateTime following the steps in the examples provided on the tlbimp.exe command line.
It is only when I added a dot to the property that I saw that the DateTime was not supported in DNXCORE50. Except that it is for native DateTime such as 'new DateTime()' (see screenshot here).
Any clue as to how to workaround this?
As per this issue reported on GitHub: Accessing mscorelib in DNX Core 5.0 #989, it seems that the Core CLR doesn't support well the COM types that refer to mscorlib.dll.
Besides, there's a package available to workaround this issue which is:
Microsoft.NETCore.Portable.Compatibility.
In short, all it does is a remap of the types referencing to mscorlib.dll such as COM objects to the newer releases of System.Runtime and other libraries, which in turn refers to its corresponding DLL for the right version or so.
So the solution is to add this package in the project.json as follows:
project.json
"frameworks": {
"dnx451": { },
"dnxcore50": {
"dependencies": {
"Microsoft.CSharp": "4.0.1-beta-23516",
"Microsoft.NETCore.Portable.Compatibility": "1.0.1-beta-23516",
"System.Collections": "4.0.11-beta-23516",
"System.Linq": "4.0.1-beta-23516",
"System.Runtime": "4.0.21-beta-23516",
"System.Threading" : "4.0.11-beta-23516"
}
}
}
For greater details about this package, and to know more about the issue, I strongly suggest to follow the links provided as they cover a more in-depth information.
In the end, it solved the problem! =)

In a DNX project, how can I target for Xamarin Android?

TL;DR
I'm writing a Xamarin.Android app and want to reference the NuGet package of a library of mine that is a dotnet-targeted DNX class library. When I do this, the compiler spits out the warning
The type 'DateTime' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, ...
and the IDE complains with messages like
Argument type 'System.DateTime [mscorlib, Version=2.0.5.0, Culture ...]' is
not assignable to parameter type 'System.DateTime [mscorlib, Version=4.0.0.0, ...]'
although the builds succeed and the code works. How can I change that in the library / the project.json?
Long story
At work we're currently porting our projects to project.json-type ones (Package, DNX, whatever the name is). At the moment, we're running the beta 7 Visual Studio integration and, in general, everything works out just fine.
I now want to reuse one of our model libraries within an ASP.NET 5 and a Xamarin.Android project (from a NuGet feed).
Since we had absolutely no luck with class libraries targeting .NET 4.5.1 in the Xamarin project I migrated the model library to a DNX project targeting net45, dnx45 and dotnet (in favor of dnxcore50, as described here), the frameworks part in the project.json being
"frameworks": {
"net45": {
"frameworkAssemblies": {
"mscorlib": "4.0.0.0",
"System.Xml": "4.0.0.0",
"System.Collections.Concurrent": "4.0.0.0"
}
},
"dnx45": {
"frameworkAssemblies": {
"mscorlib": "4.0.0.0",
"System.Xml": "4.0.0.0",
"System.Collections.Concurrent": "4.0.0.0"
}
},
"dotnet": {
"dependencies": {
"System.Collections": "4.0.0",
"System.Linq": "4.0.0",
"System.Runtime": "4.0.0",
"System.Reflection": "4.0.0",
"System.Runtime.Extensions": "4.0.0",
"System.Threading": "4.0.0",
"System.Text.RegularExpressions": "4.0.0",
"System.Text.Encoding": "4.0.0",
"System.Collections.Concurrent": "4.0.0"
}
}
},
Although this article suggests using net45 as a target for monoandroid51 projects, the dotnet library is instead referenced by the Android project whenever I add the NuGet package to it.
The package.json then contains
<package id="My.Awesome.Lib" version="1.2.3-foo9" targetFramework="monoandroid51" />
and the .csproj there has
<Reference Include="My.Awesome.Lib, Version=1.2.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\My.Awesome.Lib.1.2.3-foo9\lib\dotnet\My.Awesome.Lib.dll</HintPath>
<Private>True</Private>
</Reference>
This works out so far unless I have version numbers higher than 4.0.0 in the dependencies part and basically combusts when I do, however the following
However when I build the project, I'll get the compiler warning
1>C:\Program Files (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1706,3):
warning MSB3277: Found conflicts between different versions of the
same dependent assembly that could not be resolved. These reference
conflicts are listed in the build log when log verbosity is set to detailed.
right after the library reference. Within Visual Studio, whenever I pass a value from the Android project to one of the library types, I'll get the red squiggly lines with a message stating
The type 'DateTime' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, ...
as well as
Argument type 'System.DateTime [mscorlib, Version=2.0.5.0, Culture ...]' is
not assignable to parameter type 'System.DateTime [mscorlib, Version=4.0.0.0, ...]'
which makes perfect sense, since the Xamarin BCL is labeled as runtime version v2.0.50727, whereas my library is v4.0.30319.
I'm now wondering if I need to target some PCL something or if there is something else I'm missing.
This is not YET supported. To support this scenario, nuget will need to also be updated to take in charge the new monikers that are being thought about to simplify the references.
In the future, you would just target netstandard1.4 instead of dnxcore50 and you would have everything running and compatible with Xamarin.
.NET Standard Platform
This is where things will be going. To simplify the madness of targeting multiple platforms. This is where I got my netstandard* moniker and of course, until it hits release... this is still subject to change.

Categories

Resources