.NetCore 2 Using .Net Framework assemblies - winforms

Good day.
I have created a .NetCore2 webapp and would like some guidance on how you get
windows.forms working on it. (VS2017)
This is the error I get basically:
Dot Net Core and Azure Storage: Could not load file or assembly System, Version=4.0.0.0
Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.
The project builds but the moment I call the api it fails to detect the dll. (In code it detects the dll fine)
I have added the dll to the project, also followed the instructions of adding it to your project.json. (in my case: package.json)
"frameworks": {
"netcoreapp1.0": {
"imports": [
"net46",
"net40"
]
}
},
Also added the required references to my csproj folder.
<PropertyGroup>
<TargetFrameworks>net452;netstandard1.3</TargetFrameworks>
</PropertyGroup>
How to use System.Windows.Forms in .NET Core class library
"frameworks": {
"netstandard1.6": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
},
"net40": {
"frameworkAssemblies": {
"System.Windows.Forms": {}
}
}
}
I am still getting the same error.
I know it is not advised to use .Net framework in a .NetCore app.
Am I going to have to port the framework app so it works in a Core enviroment?
The reason i want to just use .NetFramework is that the app already works fine, I just have to get the screens shown to the user.
I also read somewhere that Windows.Forms is not supported with .NetCore in some versions of the framework.
If so, any links to help with this?
I am quite a noob with .NETCore so some of my questions might not make sense or are ignorant.
Thanks

Related

ABP Framework can't acces virtual files in Deploy

I'm working with the ABP Framework and I'm implementing my own mail templates.
The project was made using the abp project template. I created the template in my Application module: Acme.Bookstore.Application/Assets/Templates/MyEmailTemplate.tpl. I edited the properties of the file to be embedded.
Then I made an instance of the template definition provider as follows:
public class MyEmailTemplateDefinitionProvider : TemplateDefinitionProvider
{
public override void Define(ITemplateDefinitionContext context)
{
context.Add(
new TemplateDefinition(
MyEmailTemplates.MyEmailTemplate,
displayName: LocalizableString.Create<AccountResource>($"TextTemplate:{MyEmailTemplates.MyEmailTemplate}"),
layout: StandardEmailTemplates.Layout,
localizationResource: typeof(AccountResource)
).WithVirtualFilePath($"Assets/Templates/MyEmailTemplate.tpl", true)
);
}
}
Then I added the following lines to my BookstoreApplicationModule.cs
Configure<AbpVirtualFileSystemOptions>(options =>
{
options.FileSets.AddEmbedded<BookstoreApplicationModule>("Acme.Bookstore");
});
I did all this following this documentation: https://docs.abp.io/en/abp/5.3/Virtual-File-System
When debugging locally all works well, but when I deploy the project it can't access the template file. The error is as follows:
Volo.Abp.AbpException: Could not find a file/folder at the location: Assets/Templates/MyEmailTemplate.tpl
I already tried adding the AddEbedded call in the HttpApi.Host module but got the same result. I don't know what I'm missing.
For the record, I'm deploying in an Ubuntu server, but that shouldn't matter.
The problem was that the embedded files weren't being added to the VirtualFileSystem. It worked in develpment because the framework calls:
if (hostingEnvironment.IsDevelopment())
{
Configure<AbpVirtualFileSystemOptions>(options =>
{
... Some code ...
options.FileSets.ReplaceEmbeddedByPhysical<BookstoreApplicationModule>(
Path.Combine(hostingEnvironment.ContentRootPath,
$"..{Path.DirectorySeparatorChar}Acme.Bookstore.Application"));
});
}
So the problem didn't arise until I did a dotnet publish.
I could not figure out why it wasn't working on the Application module but moving the files to the Domain.Shared module fixed it.
I guess that there is some package or setting missing on the other module but this solution is enough for me.

New Module connected with a Host App but Route to the module pages gives me a 404 error

I'm trying to get started with module development using Abp framework, the potential of using the framework is huge with the community and the abp.commercial support if it's needed, but it's not always easy to up and running the application. Let me explain...
I have created a new module: abp new sample.module -t module. ABP CLI version 3.3.1
After that, I have added a new entity in Domain (member.cs) and using AbpHelper.GUI to auto-generate all the code.
🎉My module works correctly using Hosts/*.web.unified test project. Well done! :).🎉
NOTE: see here if you want to know how to solve a tricky issue for me at this point.
Next, I have added assemblies one by one to the Host App. I have mapped:
Module.Application --> Host.Application
Module.Application.Contracts --> Host.Application.Contracts
Module.Domain --> Host.Domain
Module.Domain.Shared --> Host.Domain.Shared
Module.EntityFrameworkCore --> Host.EntityFrameworkCore
Module.HttpApi --> Host.HttpApi
Module.HttpApi.Client --> Host.HttpApi.Client
Module.Web --> Host.Web
Finally, I added Module Dependencies and Configurations, following this post.
🎉 Restore, build, dotnet ef migrations, and *.HostApp.DbMigrator work like a charm and the database is updated based on the entities in the module. Cool! 🎉
But... when I run my Host App and click on the new module menu contributor the route doesn't work 😒 but it seems to be correct based on the page structure of the module and it worked fine using Hosts/*.web.unified test project inside the module.
I tried several times with no lucky 🤦‍♂️
Something is missing in the code that I cannot see.
Any help is really appreciated.
Because the Host application and Module are in different solutions. I was missing to include an assembly (Module.Web.Views.dll) into the Host web project.

How can I fix compilation for relative paths using StackExchange.Precompilation?

For https://github.com/StackExchange/StackExchange.Precompilation:
We have some shared views in separate library where we have installed StackExchange.Precompilation. We have to load those views along with the normal web project's views. We have the latest version of StackExchange.Precompilation installed from NuGet in both projects. I am doing the assembly loading like this:
// Register precompiled view engine
ViewEngines.Engines.Clear();
List<Assembly> viewAssemblies = new List<Assembly> { typeof(HomeController).Assembly };
viewAssemblies.AddRange(AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.ToLower().Contains(".web")));
Log.Debug().Message("Looking for views in: {0}", string.Join(", ", viewAssemblies.Select(a => a.FullName))).Write();
ViewEngines.Engines.Add(new PrecompiledViewEngine(viewAssemblies.ToArray()));
In the web project, we return views the normal way: return View("Index");
When using PrecompiledViewEngine we get an error when trying to render relative names like that:
The view 'Index' or its master was not found or no view engine supports the searched locations.
The following locations were searched:
~/util/Views/Example/Index.cshtml
~/util/Views/Shared/Index.cshtml
util is the alias of the application in IIS. We don't have any areas registered.
When I copied the PrecompiledViewEngine class from GitHub - it worked! Am I missing something that will make the version distributed through NuGet work?
Copying the code over turned out not to work. I must have been trying something else at the time that made it work for that specific case.
The problem is actually a bug in StackExchange.Precompilation. I've created an issue there: https://github.com/StackExchange/StackExchange.Precompilation/issues/12

Adding Entity Framework configuration in Visual Studio extension

Here's my solution architecture:
VM (class lib) => references SQLite.
App (WPF Desktop App) => references VM.
VSIX (Visual Studio extension) => references VM.
I have copied EF providers related stuff from app.config of VM project to both desktop app and VSIX projects. The desktop app works fine whereas the VSIX project throws the following exception:
No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite.EF6'. Make sure the provider is registered in the 'entityFramework' section of the application config file.
Is there something special I need to do in case of extension projects?
Adding the last line in the following class fixed it for me:
public class SQLiteConfiguration : DbConfiguration
{
public SQLiteConfiguration()
{
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
SetProviderServices("System.Data.SQLite", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
SetProviderServices("System.Data.SQLite.EF6", (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
}
}
Not sure why this exception was only being thrown in VSIX project, and not the desktop app.
Might help someone down the road.

MEF Parts not found for deployed app, but found in Debug mode

I checked a lot of MEF questions here but I can't imagine what my problem is. Here's what's happening:
I have a desktop WPF app that I'm deploying with AdvancedInstaller. I use .NET 4.0 and MEF to compose parts. Some parts are in the main project, so they are inside the app.exe file. Other parts are inside class libraries that reference the main project, so they are inside somename.dll files.
The problem:
While running the app from VS, both in Debug and in Release, everything is fine. Once I deploy the app, some of the dlls say that they have no parts (zero) to export.
I checked the following:
all dlls are available in the deployment and the catalog is finding the files
the export types and names are correct, after all, everything is working while in Visual Studio
when I try to add the parts from the dlls, I get that the number of parts is zero ONLY IN DEPLOYMENT.
This is the code that's not findind parts in the deployed app:
var catalog = new AggregateCatalog();
string path = Environment.CurrentDirectory.ToString();
DirectoryCatalog qualitycontrol = new DirectoryCatalog(".", "QualityControl.exe"); //this is my main assembly
DirectoryCatalog qualitymix;
catalog.Catalogs.Add(qualitycontrol); //this finds the parts and always works fine
if (File.Exists(path + #"\QualityMix.dll"))
{
qualitymix = new DirectoryCatalog(".", "QualityMix.dll"); //the file exists in the deployment
catalog.Catalogs.Add(qualitymix); //the "qualitymix" catalog shows more than 20 parts if run with VS, but 0 parts in deployment
}
The only thing that works (but it's very slow to start the app) is the following:
var catalog = new DirectoryCatalog(".", "*");
This has the problem that it needs to check more than 100 files present in the working directory, and I cannot deploy my plugin dlls in a different dir.
Why is it that a DirectoryCatalog looking at all files finds the parts, but a DirectoryCatalog looking at a single part does not? How can I debug this issue if it only happens in the deployed app?
---Edit: this problem is only happening with certain dlls, the files are found and for other dlls the parts are also found. I'm using the same Export/Import procedure in all dlls, but somehow some of them show no parts in deployment
Anything you can suggest will be helpfull, thank you guys!
NEW INFO!
I tried loading my dll with an AssemblyCatalog. It works in Visual Studio (Debug and Release) but when deployed I get the following errors:
1st try:
if (File.Exists(path + #"\QualityMix.dll"))
{
qualitymix = new AssemblyCatalog(Assembly.LoadFile(path + #"\QualityMix.dll")); //file is loaded and parts found in VS
catalog.Catalogs.Add(qualitymix);
}
Error: The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018).
Second try:
if (File.Exists(path + #"\QualityMix.dll"))
{
var name = AssemblyName.GetAssemblyName(path + #"\QualityMix.dll");
qualitymix = new AssemblyCatalog(Assembly.Load(name));
catalog.Catalogs.Add(qualitymix);
}
Error: Could not load file or assembly 'QualityMix.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I've looked for questions about these errors but nothing has been helpful so far. All projects are built for All CPUs, and the references look ok (this dll uses the same references as other projects that are loading ok).
Edit 2:
I tried the suggestion by #SuryaBhaskar to use LoadFrom instead of Load
if (File.Exists(path + #"\QualityMix.dll"))
{
qualitymix = new AssemblyCatalog(Assembly.LoadFrom(path + #"\QualityMix.dll"));
catalog.Catalogs.Add(qualitymix);
}
But I get the same error: Could not load file or assembly 'QualityMix.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.
I managed to solve this issue by moving the code of the problematic dll to a new project. This solved the issue, somehow... though the reason remains a mistery to me.
Use LoadFrom instead of LoadFile or Load.If you use Load it will have conflicts with other assemblies on current AppDomain

Resources