0

I have an implementation of RTDServer working fine in .NET Framework 4.x and I am trying to upgrade to .NET 7 but still have problems.

This is my basic example in .NET 7, Server.cs:

using System;
using System.Runtime.InteropServices;
using com.lightstreamer.client;
using Microsoft.Office.Interop.Excel;

namespace Examples.RtdDemo
{
    [ComVisible(true)]
    [Guid("DB1797F5-7198-4411-8563-D05F4E904956")]
    [ProgId("lightstreamer.rtdnew12")]
    public class LsRtdServer : IServer
    {
        private int pi = 0;

        int IRtdServer.ServerStart(IRTDUpdateEvent CallbackObject)
        {
            return (int)pi;
        }

        object IRtdServer.ConnectData(int TopicID, ref Array Strings, ref bool GetNewValues)
        {
            pi += TopicID;

            return ""+pi;
        }

        Array IRtdServer.RefreshData(ref int TopicCount)
        {
            object[,] data = new object[1, 1];

            data[0,0] = ""+pi;

            return data;
        }

        void IRtdServer.DisconnectData(int TopicID)
        {
            // .
        }

        int IRtdServer.Heartbeat()
        {
                return 0;
        }

        void IRtdServer.ServerTerminate()
        {
            // ..
        }
    }
}

This is the interface, IServer.cs:

using System;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Excel;

[ComVisible(true)]
[Guid("BA9AC84B-C7FC-41CF-8B2F-1764EB773D4B")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IServer : IRtdServer
{

}

This is the project file:

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <TargetFramework Condition="'$(DefaultALC)' == 'True'">net8.0</TargetFramework>

    <!-- Indicate the assembly is providing a COM server -->
    <EnableComHosting>True</EnableComHosting>

    <!-- Generate a RegFree COM manifest -->
    <EnableRegFreeCom Condition="'$(RegFree)' == 'True'">True</EnableRegFreeCom>
</PropertyGroup>

<ItemGroup>
    <!-- Used in lieu of a Primary Interop Assembly (PIA) -->
    <Compile Include="../COMContract/*.cs" />
</ItemGroup>

<ItemGroup>
    <PackageReference Include="Lightstreamer.DotNetStandard.Client" Version="6.0.0-beta.2" />
    <PackageReference Include="Microsoft.Office.Interop.Excel" Version="15.0.4795.1001" />
</ItemGroup>

<ItemGroup>
    <RuntimeHostConfigurationOption Condition="'$(DefaultALC)' == 'True'" Include="System.Runtime.InteropServices.COM.LoadComponentInDefaultContext" Value="true" />
</ItemGroup>

<Target Name="ServerUsage" Condition="'$(RegFree)' != 'True'" AfterTargets="Build">
    <Message Importance="High" Text="%0a************************************%0a*** $(MSBuildProjectName) usage instructions ***%0a************************************" />
    <Message Importance="High" Text="The server must be COM registered in order to activate.%0aThe following commands must be executed from an elevated command prompt." />
    <Message Importance="High" Text="Register:%0a    regsvr32.exe &quot;$(ProjectDir)$(OutputPath)$(ComHostFileName)&quot;" />
    <Message Importance="High" Text="Unregister:%0a    regsvr32.exe /u &quot;$(ProjectDir)$(OutputPath)$(ComHostFileName)&quot;" />
</Target>

<Target Name="ServerUsage_RegFree" Condition="'$(RegFree)' == 'True'" AfterTargets="Build">
    <Message Importance="High" Text="%0a************************************%0a*** $(MSBuildProjectName) usage instructions ***%0a************************************" />
    <Message Importance="High" Text="A RegFree COM manifest has been created for the server.%0aThe manifest '@(RegFreeComManifest->'%(Filename)%(Extension)')' must be included during server deployment.%0aThe COMServer project will copy all required outputs to the COMClient output directory." />

    <ItemGroup>
        <ServerOutput Include="$(OutputPath)*.dll" />
        <ServerOutput Include="$(OutputPath)*.runtimeconfig.json" />
        <ServerOutput Include="$(OutputPath)*.deps.json" />
        <ServerOutput Include="$(OutputPath)*.manifest" />
    </ItemGroup>

    <!-- Deploy all required server outputs -->
    <Copy SourceFiles="@(ServerOutput)" DestinationFolder="../COMClient/$(OutputPath)" />
</Target>

<Target Name="Clean_RegFree" AfterTargets="Clean">
    <ItemGroup>
        <ServerOutputToDelete Include="../COMClient/$(OutputPath)COMServer.*" />
    </ItemGroup>
    <!-- Cleanup deployed server outputs -->
    <Delete Files="@(ServerOutputToDelete)" />
</Target>

Once built I have registered the comhost.dll with regsvr32.exe. The registration succeeded but from Excel any cell with a RTD formula got "N/A". Instead with a COM client application works. Any idea what is going wrong? I missing something?

  • 1
    Migrating COM objects from .NET 4 to .NET core+ is not an inplace replacement (many implicit features were lost in translation, breaking changes have appeared). No tlb is generated, to start with. You should add more information, we have no idea what your code does, how it's compiled, deployed, etc https://stackoverflow.com/help/minimal-reproducible-example – Simon Mourier May 31 '23 at 16:24
  • Thank you, Simon. I have update my question with a minimal example. – Giuseppe Corti Jun 01 '23 at 09:16
  • 1
    Works fine for me, Excel calls into the ctor and ServerStart method. PS: 1) I didn't use COM regfree, 2) make sure you compile and register for x64 and your Excel runs as x64 or you compile and register for x86 and your Excel runs as x86. – Simon Mourier Jun 04 '23 at 10:59
  • Thank you Simon, adding x86 makes the trick. – Giuseppe Corti Jun 05 '23 at 15:36

0 Answers0