ExcelDna icon indicating copy to clipboard operation
ExcelDna copied to clipboard

Can you make it work with .NET 7.0 or .NET 8.0?

Open nsulikowski opened this issue 3 years ago • 34 comments

Or give me some hints to do it myself. I tried modifying the source code, but failed :-( Thanks

nsulikowski avatar Dec 15 '22 21:12 nsulikowski

There is a hard limitation that different versions of the .NET core runtime cannot be loaded into a process. This means an instance of Excel can't load an add-in targeting .NET 6 and an add-in targeting .NET 7 at the same time.

For now I've decided to only support .NET 6 with Excel-DNA, so that different add-in can continue to work together without problems.

What are the main drivers for you to go towards .NET 7?

govert avatar Dec 18 '22 13:12 govert

All the libraries i wrap with ExcelDNA have been converted to NET 7 Can’t move back.


From: Govert van Drimmelen @.> Sent: Sunday, December 18, 2022 8:45:58 AM To: Excel-DNA/ExcelDna @.> Cc: Nestor Sulikowski (B) @.>; Author @.> Subject: Re: [Excel-DNA/ExcelDna] Can you make it work with .NET 7.0? (Issue #538)

There is a hard limitation that different versions of the .NET core runtime cannot be loaded into a process. This means an instance of Excel can't load an add-in targeting .NET 6 and an add-in targeting .NET 7 at the same time.

For now I've decided to only support .NET 6 with Excel-DNA, so that different add-in can continue to work together without problems.

What are the main drivers for you to go towards .NET 7?

— Reply to this email directly, view it on GitHubhttps://github.com/Excel-DNA/ExcelDna/issues/538#issuecomment-1356801861, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAPYZ5YKNETJRCZC444QUJ3WN4IRNANCNFSM6AAAAAATAFU244. You are receiving this because you authored the thread.Message ID: @.***>

nsulikowski avatar Dec 18 '22 13:12 nsulikowski

I figured out a way to go back to NET 6 for libraries. Thanks for your response. I think there should be an ExcelDna for .Net Framework, and another one for .NET 6, 7, etc. Thanks for your great work over the years. N

nsulikowski avatar Dec 19 '22 13:12 nsulikowski

Just to be clear on the situation today. You can use the current version of Excel-DNA to make add-in that target the .NET Framework 4.6.2 and higher, and you can also make add-ins that target .NET 6. Such add-in can all run together in a single Excel process, mixing .NET Framework and .NET 6 add-ins together.

If we added support for .NET 7, then such an add-in could not run at the same time as a .NET 6 add-in, so depending on which add-in is loaded first, the other would fail to load. I'm trying to avoid having to deal with such problems by only support .NET 6 for now. Since .NET 6 is a long-term support version, that seems a reasonable solution for now. Over the coming years we would have to figure out what to do, but the concurrent loading constraint together with the yearly major .NET versions makes life hard for our use case.

govert avatar Dec 19 '22 20:12 govert

Yep, I get it. Thanks again.

nsulikowski avatar Dec 19 '22 20:12 nsulikowski

Native AOT is supported in .NET 7. Is it possible that Excel-DNA could support Native AOT deployment if targeting .NET 7?

lilkui avatar Dec 29 '22 08:12 lilkui

@lilkui Indeed, I think NativeAOT is a promising future direction for Excel-DNA, especially with the COM support being worked on for .NET 8. We're likely to do some work toward this next year - signing up as a GitHub Sponsor is one way to encourage work like this.

govert avatar Dec 29 '22 08:12 govert

Hello,

  1. Thank you very much for all the effort that you put into that project.

  2. I know the bitchyness of Excel and how it cannot load many .NET at the same time. This is basically why Microsoft didnt move their VSTO out of .NET Framework, and following that on Microsoft it doesnt seem to be a solution soon or a willing for them to migrate it.

  3. The alternative that Microsoft suggests there on their developer forums is just a bad joke, the Javascript new API.

  4. I have played with this package and it seems again wonderful how 1 or a few people can make something to work nice while Microsoft with their huge teams cannot make their own things to work like they should

  5. The package is exactly what i have been expected to be, and for that i have to again thank you very much.

  6. However i dont understand why it should be your problem as package creator if i, as a user, am an idiot and want to target multiple addins on different frameworks.

  7. Moving from .NET6 to .NET7 is after my tests a close to 20% improvement in perf in pretty much every area. Not to mention the new revolution again that will be added to .NET8 this autumn.

  8. All my projects are moved in a few days after the .NET is released even if they are very complex ones. Excel COM Addins ( VSTO ) are the only ones that i couldnt move there.

  9. So i really dont understand why a nice project like this one should be stuck to .NET6 because it might be consumed wrong by the users?

Sorry for the very long comment there, Thank you again for supporting this project, Have a nice day

CorsairRO avatar Aug 09 '23 14:08 CorsairRO

I appreciate the feedback and will try to canvas for some more opinions on what to do about .NET 7+.

My main concern is this. Some users of Excel-DNA create add-ins that are used beyond their organization, e.g. they ship their add-in as part of a product to outside customers, or the add-in (like the Excel-DNA IntelliSense extension) is used in various settings that might not carefully manage the .NET environment. If we support making add-ins with .NET 7+, any add-in targeting .NET 7+ will block all add-ins targeting .NET 6. And conversely, any add-in targeting .NET 6 will block all add-ins that target .NET 7+. This would mean the only viable runtime target for add-ins to be used in less controlled internal environments, or by outside users, would be .NET Framework 4.x. So, by allowing .NET 7 to be used, we remove the .NET 6 option from many scenarios. To me it would be frustrating for all those projects to be stuck on .NET Framework for many years to come.

Over the long term there is one source of hope. Microsoft has put some effort in the pre-compiled NativeAOT option, with quite a bit of progress in .NET 8. It might be possible to engineer Excel-DNA to target NativeAOT, and in that way make a native code add-in based on the .NET 8+ runtime. These would not clash with any other add-ins, freeing us from the runtime restriction. This is not trivial from the Excel-DNA side (where reflection, runtime compilation and COM features are used extensively) so might take a while. Also, a NativeAOT add-in would require all the libraries used to also work with AOT, which might be a problem for a few years, since it's all really new. So that seems like one plan, even if it will probably take some years to materialise.

govert avatar Aug 18 '23 21:08 govert

FWIW, and probably a decent reason to plan on switching to .NET 8, are the End of Support dates. .NET 6 is EoS Nov 2024, .NET 7 is EoS May 2024.

zgrose avatar Aug 18 '23 23:08 zgrose

I appreciate the feedback and will try to canvas for some more opinions on what to do about .NET 7+.

My main concern is this. Some users of Excel-DNA create add-ins that are used beyond their organization, e.g. they ship their add-in as part of a product to outside customers, or the add-in (like the Excel-DNA IntelliSense extension) is used in various settings that might not carefully manage the .NET environment. If we support making add-ins with .NET 7+, any add-in targeting .NET 7+ will block all add-ins targeting .NET 6. And conversely, any add-in targeting .NET 6 will block all add-ins that target .NET 7+. This would mean the only viable runtime target for add-ins to be used in less controlled internal environments, or by outside users, would be .NET Framework 4.x. So, by allowing .NET 7 to be used, we remove the .NET 6 option from many scenarios. To me it would be frustrating for all those projects to be stuck on .NET Framework for many years to come.

Over the long term there is one source of hope. Microsoft has put some effort in the pre-compiled NativeAOT option, with quite a bit of progress in .NET 8. It might be possible to engineer Excel-DNA to target NativeAOT, and in that way make a native code add-in based on the .NET 8+ runtime. These would not clash with any other add-ins, freeing us from the runtime restriction. This is not trivial from the Excel-DNA side (where reflection, runtime compilation and COM features are used extensively) so might take a while. Also, a NativeAOT add-in would require all the libraries used to also work with AOT, which might be a problem for a few years, since it's all really new. So that seems like one plan, even if it will probably take some years to materialise.

Hello,

You are talking about AOT, which would be interesting to make the standalone XLL faster, but the minimum requirement to have .Net 6 & .Net 7 & ... working together would be "self contained", not AOT, wouldn't it?

AOT or Trimming would sure improve the size of the binary and launch time, but they are not the requirement for "side by side" Excel DNA addins, aren't they?

Or myabe there is something I'm missing ?

tomap avatar Aug 27 '23 10:08 tomap

@tomap "the minimum requirement to have .Net 6 & .Net 7 & ... working together would be "self contained", not AOT" No - the limitation is that there can only be one .NET core runtime loaded into the process - this is not related to whether the runtime is "self-contained" or not. So there is no way to have both .NET 6 and .NET 7 loaded into Excel at the same time, side-by-side, in the way that .NET Framework and .NET 6 can load side-by-side.

govert avatar Aug 27 '23 12:08 govert

Hi @govert! I've made some successful tests with AOT, it seems to be working well. Check out at https://github.com/lukedays/ExcelNativeAot Thanks in advance!!

lukedays avatar Aug 30 '23 01:08 lukedays

Hi @lukedays - yes, your project exactly what I expect for an AOT approach that avoids the version issues where possible. We (actually @Sergey-Vlasov ) have done a kind of prototype of using AOT with source generators etc. for making .NET-based SQLite extensions - the repository is here: https://github.com/SQLite-DNA/SqliteDna

For Excel-DNA I reworked the marshaling code a while ago to make it easy to plug into an AOT context too.

For me, the main question for AOT is how the COM integration (both ways) will work, either using the new COM source generator and / or manual COM definitions. One possible limitation with the COM support under AOT relates to the C# dynamic / IDispatch glue, which won't be supported for .NET 8, but hopefully in some future version. See my comments here: https://github.com/dotnet/runtime/issues/66674#issuecomment-1359416267

govert avatar Aug 30 '23 10:08 govert

Thanks for the reply! Very nice work on the SQL and marshaling codes. I'll take some inspiration from it in order to advance.

Long shot here, what if we mimic/translate the C/C++ IDispatch implementation? I basically did this for the Excel C API calls - a rewrite of SDK methods in C#.

lukedays avatar Aug 30 '23 13:08 lukedays

For the COM / IDispatch stuff, the code I worry about is something like this

using Microsoft.Office.Interop.Excel;

class Program
{
    static void Main(string[] args)
    {
        Application app = new Application();
        app.Visible = true;

        Workbook wb = app.Workbooks.Add();
        wb.Sheets[1].Name = "FirstSheet"; 
        //        ^^^^ The code in the PIA for the indexed property wb.Sheets[...] returns "object"
        //             Under current compiler, with PIA embedding, this compiles and runs as 'dynamic'
        //             since that kicks in the dynamic /  IDispatch support.
    }
}

It should work fine in the .NET 2.0 style COM interop, where had to be explicit about the casts, use get_Item and set_Item etc. But that's not so pleasant.

I don't know much about how dynamic, ExpandoObject etc. work under AOT - I have yet to experiment with this.

govert avatar Aug 30 '23 13:08 govert

I'll do some tests to check if the managed Interop.Excel library works. If it doesn't, maybe we can wrap the VARIANT type just like the XLOPER.

https://learn.microsoft.com/en-US/previous-versions/office/troubleshoot/office-developer/automate-excel-from-c

lukedays avatar Aug 30 '23 13:08 lukedays

There is a whole new ComWrappers API that implements COM outside the runtime, that one would build upon. https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.comwrappers

govert avatar Aug 30 '23 13:08 govert

Interesting, I'll take a look! It's a lot of work indeed... But seems to be the way forward, since the Excel API's are being forgotten by MS (even the JS one seems abandoned).

lukedays avatar Aug 30 '23 14:08 lukedays

Hi @govert, do you have any tips to make ComWrappers work for Excel COM? I've searched on Github for examples but most of them seem to rely on DllImport of native objects that get wrapped afterwards. Calling CoCreateInstance doesn't work on NativeAOT. This is the easiest example that I found, but also relies on DllImport. https://github.com/kant2002/CoMallocNativeAot/blob/12c1df83ec09dcd095a36fc034412cb6598b3711/CoMallocNativeAot/Program.cs Thanks in advance!!

lukedays avatar Sep 04 '23 23:09 lukedays