简体   繁体   English

.NET远程处理和服务器激活的对象

[英].NET Remoting and Server Activated Objects

what's the problem with the following code... I have this Complex class: 以下代码有什么问题...我有这个Complex类:

public class Complex : MarshalByRefObject
    {

        public double imaginary{get;set;}
        public double real{get;set;}            

        public void setReal(double re)
        {
            real = re;
        }

        public void setImaginary(double im)
        {
            imaginary = im;
        }

        public Complex(double im, double re)
        {
            imaginary = im;
            real = re;
        }    

        public void writeMembers()
        {
            Console.WriteLine(real.ToString() + imaginary.ToString());
        }
    }

Actually, there's a little more to it, but the code it's too big, and we don't use the rest of it in the context of this. 实际上,它还有更多功能,但是代码太大了,在此情况下,我们不会使用其余的代码。

Then, I implemented a server which listens for connections: 然后,我实现了一个监听连接的服务器:

HttpChannel channel = new HttpChannel(12345);                
            ChannelServices.RegisterChannel(channel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(SharedLib.Complex), "ComplexURI", WellKnownObjectMode.SingleCall);

            Console.WriteLine("Server started. Press any key to close...");
            Console.ReadKey();

            foreach (IChannel ichannel in ChannelServices.RegisteredChannels)
            {
                (ichannel as HttpChannel).StopListening(null);
                ChannelServices.UnregisterChannel(ichannel);
            }

Then, we have the client: 然后,我们有客户:

try
            {
                HttpChannel channel = new HttpChannel();
                RemotingConfiguration.Configure("Client.exe.config", false);

                Complex c1 = (Complex)Activator.GetObject(typeof(Complex), "http://localhost:12345/ComplexURI");                    


                if (RemotingServices.IsTransparentProxy(c1))
                {
                    c1.real = 4;
                    c1.imaginary = 5;    

                    c1.writeMembers();                    

                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("The proxy is not transparent");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }

Then, I run the server, which opens a console window, and I run the client. 然后,我运行服务器,这将打开一个控制台窗口,然后运行客户端。 Instead of displaying 4 and 5 on the server window, I merely get 00, a sign that the members weren't changed. 我只得到00,而不是在服务器窗口上显示4和5,这表明成员没有更改。 How do I do, so the members change? 我该怎么做,所以成员会改变吗? Thanks. 谢谢。

The problem is that you're using WellKnownObjectMode.SingleCall . 问题是您正在使用WellKnownObjectMode.SingleCall As the documentation says: 文档所述

  • SingleCall Every incoming message is serviced by a new object instance. SingleCall每个传入消息都由一个新的对象实例提供服务。
  • Singleton Every incoming message is serviced by the same object instance. 单例每个传入消息均由同一对象实例提供服务。

See also the documentation for RegisterWellKnownServiceType : 另请参见RegisterWellKnownServiceType的文档:

When the call arrives at the server, the .NET Framework extracts the URI from the message, examines the remoting tables to locate the reference for the object that matches the URI, and then instantiates the object if necessary, forwarding the method call to the object. 当调用到达服务器时,.NET Framework从消息中提取URI,检查远程处理表以找到与URI相匹配的对象的引用,然后在必要时实例化该对象,将方法调用转发给该对象。 If the object is registered as SingleCall, it is destroyed after the method call is completed. 如果对象注册为SingleCall,则在方法调用完成后将销毁该对象。 A new instance of the object is created for each method called. 将为每个调用的方法创建一个对象的新实例。

In your case, the statement c.Real = 4 is a call to the Real property setter. 在您的情况下,语句c.Real = 4是对Real属性设置器的调用。 It makes a call to the remote object, which creates a new object, sets the Real property to 4, and returns. 它调用远程对象,该对象创建一个新对象,将Real属性设置为4,然后返回。 Then when you set the imaginary property, it creates a new object, etc. 然后,当您设置imaginary属性时,它将创建一个新对象,依此类推。

If you want this to work, you'll have to use WellKnownObjectMode.Singleton . 如果您希望此方法有效,则必须使用WellKnownObjectMode.Singleton But you might want to ask yourself if you really want such a "chatty" interface. 但是您可能想问自己,是否真的想要这样的“聊天”界面。 Every time you set a property, it requires a call through the proxy to the server. 每次设置属性时,都需要通过代理调用服务器。

And, finally, you might consider abandoning Remoting altogether. 最后,您可以考虑完全放弃远程处理。 It's old technology, and has a number of shortcomings. 它是旧技术,并且有许多缺点。 If this is new development, you should be using Windows Communications Foundation (WCF). 如果这是新开发的,则应该使用Windows Communications Foundation (WCF)。 The Remoting documentation says: 远程处理文档说:

This topic is specific to a legacy technology that is retained for backward compatibility with existing applications and is not recommended for new development. 本主题特定于保留的旧技术,以与现有应用程序向后兼容,不建议用于新开发。 Distributed applications should now be developed using the Windows Communication Foundation (WCF). 现在应该使用Windows Communication Foundation(WCF)开发分布式应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM