对类型 'MarshalByRefObject' 的引用声称它在 'System.Runtime' 中定义,但找不到

Reference to type 'MarshalByRefObject' claims it is defined in 'System.Runtime', but it could not be found

我今天一整天都在为这个错误而苦恼。我正在尝试在我的计算机上模拟一个小型分布式服务,该服务由三个独立项目中的远程对象、服务器和客户端组成。这个想法是读取一个带有远程对象的 XML 文件。

我已经测试过,调用标记为 MarshalByRefObject 的方法将引发此错误。如果我只是添加并调用没有该标记的方法,它会正常工作。

这是对象的代码 class:

using System.Xml;
using System.Xml.XPath;

namespace XmlViewer
{
    public class XmlViewerClass : MarshalByRefObject
    {
        string xmlFilePath;

        public XmlViewerClass(string xmlFilePath)
        {
            this.xmlFilePath = xmlFilePath;

            XPathNavigator xpath = ViewXmlDoc(xmlFilePath);
        }

        public string XmlFilePath
        {
            set { value = xmlFilePath; }
            get { return xmlFilePath; }
        }

        public XPathNavigator ViewXmlDoc(string xmlFilePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlFilePath);

            XPathNavigator ret = doc.CreateNavigator();
            return ret;
        }
    }
}

这是服务器:

using System;
using System.Windows.Forms;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;

namespace MyNamespaceServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void RunServer()
        {
            try
            {
                TcpServerChannel tcp = new TcpServerChannel(1902);
                ChannelServices.RegisterChannel(tcp, true);

                RemotingConfiguration.RegisterWellKnownServiceType(
                    typeof(XmlViewer.XmlViewerClass), "XmlViewerClass",
                    WellKnownObjectMode.Singleton);

                lblStatus.Text = "Server running...";
            }
            catch (Exception ex)
            {
                lblStatus.Text = "An error has occurred... " + ex.Message;
            }
        }
    }
}

这是客户端(此时不完整),这是出现错误的地方 - 特别是行 XPathNavigator nav = p.ViewXmlDoc("music.xml");

namespace MyNamespaceClientXmlViewerFinal
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            XmlViewerClass p = new XmlViewerClass("music.xml");
            XPathNavigator nav = p.ViewXmlDoc("music.xml");
        }
    }
}

我对远程处理还很陌生,所以我完全有可能只是在某个地方犯了一些小错误。但老实说,在这一点上我很困惑!

根据Microsoft Documentation

Remoting isn't supported on .NET 5+ (and .NET Core). .NET remoting was identified as a problematic architecture. It's used for communicating across application domains, which are no longer supported. Also, remoting requires runtime support, which is expensive to maintain.

如果您想继续使用远程处理,则必须以 .NET Framework 4.8(或更早版本)为目标。或者,您可以研究 gRPC,这是一种在 .NET 5 中支持的开放式 RPC 机制。