【问题标题】:Reference to type 'MarshalByRefObject' claims it is defined in 'System.Runtime', but it could not be found对“MarshalByRefObject”类型的引用声称它是在“System.Runtime”中定义的,但找不到
【发布时间】:2021-09-08 08:02:23
【问题描述】:

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

我已经测试和调用标记为 MarshalByRefObject 的方法会抛出这个错误。如果我只是添加并调用没有该标记的方法,它将正常工作。

这是对象类的代码:

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");
        }
    }
}

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

【问题讨论】:

  • "... 调用标记为 MarshalByRefObject 的方法将引发此错误。如果我只是添加并调用没有该标记的方法,它将正常工作“这些句子没有意义。您真的了解自己要做什么,还是只是盲目地复制粘贴代码?
  • 您不能在以 .NETCore 为目标的项目中使用远程处理。
  • @IanKemp 我实际上是在尝试从我正在做的软件开发课程中重新创建代码。我正在努力澄清,因为我还在学习。

标签: c# visual-studio-2019 .net-remoting


【解决方案1】:

根据Microsoft Documentation.

.NET 5+(和 .NET Core)不支持远程处理。 .NET 远程处理是 被确定为有问题的架构。它是用来交流的 跨应用程序域,不再受支持。还, 远程处理需要运行时支持,维护成本很高。

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

【讨论】:

    猜你喜欢
    • 2018-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 1970-01-01
    • 2018-09-12
    相关资源
    最近更新 更多