none
.Net Remote IpcChannel and events RRS feed

  • Frage

  • Hello,

    I want to fire an event, the server get the event but the client dosn't get the event.Why?

    I Wrote the follwing Code.

    Server:

            static void Main(string[] args)
            {
                IpcChannel channel = new IpcChannel("localhost:32600");
                ChannelServices.RegisterChannel(channel,true);
                GethLib.serverMethods.critmsg += new GethLib.serverMethods.critmsgHandler(serverMethods_critmsg);
                Console.WriteLine("The name of the channel is {0}.",
                    channel.ChannelName);
    
                Console.WriteLine("The priority of the channel is {0}.",
                    channel.ChannelPriority);
    
                System.Runtime.Remoting.Channels.ChannelDataStore channelData =
                            (System.Runtime.Remoting.Channels.ChannelDataStore)
                channel.ChannelData;
                foreach (string uri in channelData.ChannelUris)
                {
                    Console.WriteLine("The channel URI is {0}.", uri);
                }
                
    
                System.Runtime.Remoting.RemotingConfiguration.
                    RegisterWellKnownServiceType(
                        typeof(GethLib.serverMethods), "serverMethods",
                        System.Runtime.Remoting.WellKnownObjectMode.Singleton);
               
                string[] urls = channel.GetUrlsForUri("serverMethods");
                if (urls.Length > 0)
                {
                    string objectUrl = urls[0];
                    string objectUri;
                    string channelUri = channel.Parse(objectUrl, out objectUri);
                    Console.WriteLine("The object URI is {0}.", objectUri);
                    Console.WriteLine("The channel URI is {0}.", channelUri);
                    Console.WriteLine("The object URL is {0}.", objectUrl);
                }
                
                Console.WriteLine("Press ENTER to exit the server.");
                Console.ReadLine();
                Console.WriteLine("The server is exiting.");
            }
    
            static void serverMethods_critmsg(string CritMsg)
            {
                Console.WriteLine(CritMsg);
            }
        }

    Servermethods:

        public class serverMethods:MarshalByRefObject
        {
            private int msg;
            private string xCritmsg = "Kritischer Wert erreicht!";
    
            public delegate void critmsgHandler(string CritMsg);
            public static event critmsgHandler critmsg = null;
            public int Msg
            {
                get 
                {
                    if (msg > 5)
                    {
                        if (critmsg != null)
                        {
                            critmsg(xCritmsg);
                        }
                    }
                    return (msg++); 
                }
            }
        }

    Client:

            static void Main(string[] args)
            {
                IpcClientChannel clientChannel = new IpcClientChannel();
                ChannelServices.RegisterChannel(clientChannel,true);
    
                RemotingConfiguration.RegisterWellKnownClientType(typeof(GethLib.serverMethods), "ipc://localhost:32600/serverMethods");
                GethLib.serverMethods sm = null;
    
                for (int i = 0; i < 10; i++)
                {
                    sm = new GethLib.serverMethods();
                    Console.WriteLine(sm.Msg);
                }
                GethLib.serverMethods.critmsg += new GethLib.serverMethods.critmsgHandler(serverMethods_critmsg);
            }
    
            static void serverMethods_critmsg(string CritMsg)
            {
                Console.WriteLine(CritMsg);
            }

    I hope you can help me

    Thank you

    Freitag, 15. Februar 2013 20:29

Alle Antworten

  • Hello cissoid,

    Thank you for your question but this is a German speaking  forum so please post in German.

    Regarding your question there are multiple problems:

    1. In the client you register to the event after your code basically finishes it's run so nothing is going to happen.

    2. The second is that by registering to that event while the server is already registered will also not work.

    My recommendation is to read more about events while remoting and if they are a good idea or not:

    http://blogs.msdn.com/b/dotnetremoting/archive/2006/04/07/571020.aspx

    http://aviadezra.blogspot.co.uk/2008/06/code-snippets-remoting-client-server_19.html

    Thank you,

    Ionut

    Dienstag, 19. Februar 2013 16:52
    Moderator
  • Hi,

    ja ich wollte nicht in englisch posten, aber es stand überall ales in englisch nach dem posten dann in

    deutsch...egal.

    Also das Hauptproblem welches ich sehe ist wohl IcpChannel. Ich hatte schon gelesen

    dass es damit kompliziert werden könnte von daher solle man lieber TCP nutzen.

    Mir ist noch nicht klar warum, hat das einen speziellen Grund?

    Danke

    Mittwoch, 20. Februar 2013 20:45