【问题标题】:Problem with .Net Remoting (C#).Net 远程处理问题(C#)
【发布时间】:2009-06-30 21:25:30
【问题描述】:

我正在使用 .Net Remoting 并尝试访问在服务器中修改的远程对象,这是对象定义: 这个想法是,我在服务器上创建的 MRB 对象返回给客户端,并在客户端调用 getString() 时打印出“Set On Server”。

我现在得到的是客户端上的空字符串,因此当客户端上调用 new 时,MRB 对象没有发送到客户端。 对不起所有不同的类等,但这是唯一的方法,我尽可能地修剪。

我真正想要的是运行时在客户端打印的“Set On The Server”

using System;
using System.Runtime.Remoting.Lifetime;
namespace RemoteType
{
    public class MyRemoteObject : System.MarshalByRefObject
    {
        private string sharedString;
        public string getString()
        {
            return sharedString;
        }
        public void setString(string value)
        {
        sharedString = value;
        }   
        public MyRemoteObject()
        {
            Console.WriteLine("MyRemoteObject Constructor Called");
        }

        public override object InitializeLifetimeService()
        {
            return null;
        }
        public string Hello()
        {
            return "Hello, Welcome to .Net Remoting !";
        }
    }

知道这里是服务器:

using System;
using System.Runtime.Remoting;
using RemoteType;
namespace SimpleServer
{
    class SimpleServer
    {
        public static MyRemoteObject MRB = null;
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemotingAppServer.exe.config");
            MRB = new MyRemoteObject();
            MRB.setString("Set on the server");
            Console.WriteLine(MRB.getString());
            RemotingServices.Marshal((MRB), "MyRemoteObject");
            Console.WriteLine("Press return to exit");
            Console.ReadLine();
        }
    }

还有.NET Remote Config App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application>
            <channels>
                <channel ref="tcp" port="8000" />
            </channels>
            <service>
                <wellknown mode="Singleton" 
                type="RemoteType.MyRemoteObject, RemoteType"
                objectUri="MyRemoteObject" />
            </service>
        </application>
    </system.runtime.remoting>
</configuration>

最后是客户:

using System;
using System.Runtime.Remoting;
using RemoteType;
namespace SimpleClient
{
    class SimpleClient
    {
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemoteClient.exe.config");
            MyRemoteObject robj = new MyRemoteObject();
            Console.WriteLine(robj.Hello() + " " + robj.getString());
            Console.ReadLine();
        }
    }
}

还有它的配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.runtime.remoting>
        <application name = "SimpleClient">
            <client>
                <wellknown
                type="RemoteType.MyRemoteObject,RemoteType"
                url="tcp://localhost:8000/MyRemoteObject"/>
            </client>
            <channels>
                <channel ref="tcp" port="0"/>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>

【问题讨论】:

    标签: c# .net remoting


    【解决方案1】:

    我确实测试了您的代码并且运行良好。我确实设置了三个项目,一个带有服务器,另一个带有客户端,第三个在服务器和客户端之间为远程对象共享。 在远程 app.config 中,您可以删除众所周知的条目,因为您已经通过代码对其进行编组。

    【讨论】:

    • 客户端运行时有没有看到客户端打印“Set on the server”?
    • @daniel:您是否将项目设置为同时启动服务器和客户端?如果是,也许在客户端代码的开头添加一个 Thread.Sleep 可能会有所帮助。
    • 是的,我在屏幕客户端和服务器中都看到了“在服务器上设置”。我确实有两种解决方案,一种用于服务器,一种用于客户端。我先启动服务器,然后多次启动客户端,结果都一样。
    • 在我的机器上一定有什么奇怪的东西,但我得到:\bin\Debug>RemoteClient.exe MyRemoteObject Constructor Called Hello, Welcome to .Net Remoting!
    【解决方案2】:

    您确定您的套接字会在应用程序结束后立即关闭吗?

    如果您快速测试多次,这可能会导致 tcp 通信问题

    【讨论】:

      【解决方案3】:

      防火墙是否会在您的环境中阻止 tcp 端口 8000?您可以尝试切换到 http 作为测试。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多