Mike
Mike

Reputation: 19

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

I have been struggling with this error all day today. I'm trying to simulate a small distributed service on my computer, comprised of a remotable object, server, and client in three seperate projects. The idea is to read an XML file with a remotable object.

I have tested and calling methods which are marked as MarshalByRefObject will throw this error. If I just add and call the method without that tag it will work fine.

Here is the code for the object 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;
        }
    }
}

Here is the server:

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

And here is the client (incomplete at this point), which is where the error is showing up - specifically the line 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");
        }
    }
}

I am very new to remoting, so it's entirely possible I'm just making some small slip up somewhere. But honestly at this point I am stumped!

Upvotes: 0

Views: 1724

Answers (1)

David Jones
David Jones

Reputation: 3382

According to the 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.

You must target .NET Framework 4.8 (or earlier) if you want to continue using remoting. Alternatively, you could investigate gRPC, which is an open RPC mechanism with support in .NET 5.

Upvotes: 1

Related Questions