Hi,
I have been struggling with receiving events from a server to a client (using .NET remoting) for quite a long time now but I keep receiving the following message on client side (“Exception has been thrown by the target of an invocation.”). This message is received as the server is trying to call the event handler while the event handler exists in the client and since the client assembly is not available on the side of the server, this message comes.
Any idea?

Server Class library
-----------------------------

public delegate void MyDel();

    public class ServerClass : MarshalByRefObject
    {
        public event MyDel myEvent;

        public void StartServer (bool shouldFireEvent)
        {
            TcpServerChannel tsc;
            BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
            provider.TypeFilterLevel = TypeFilterLevel.Full;
            IDictionary di = new Hashtable();
            di["port"] = 8085;
            di["bindTo"] = "172.19.35.191";
            tsc = new TcpServerChannel(di, provider);
            ChannelServices.RegisterChannel(tsc, false);
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerClass), "myURI", WellKnownObjectMode.Singleton);

            if (shouldFireEvent)
            {
                while (true)
                {
                    myEvent();
                    Thread.Sleep(1000);
                }
            }
        }
    }

Server GUI
-----------------------------

class ServerGui
    {
        static void Main(string[] args)
        {
            ServerClass server = new ServerClass();
            server.myEvent += new MyDel(ReceivedEventFromServerHandler); //lets register to event
            server.StartServer(true);
            Console.WriteLine("server is running. press enter to quit");
            Console.ReadKey();
        }

        static void ReceivedEventFromServerHandler()
        {
            Console.WriteLine("I am the server GUI. Got a message from the server");
        }
    }

Client
--------------

class ClientClass
    {
        static void Main(string[] args)
        {
            ServerClass server = (ServerClass)Activator.GetObject(typeof(ServerClass), "tcp://172.19.35.191:8085/myURI");
            server.myEvent += new MyDel(server_myEvent); //HERE IT CRASHES
            while (true)
            {
                Thread.Sleep(1000);
            }
        }

        static void server_myEvent()
        {
            Console.WriteLine("I am the client. Got a message from the server over the network");
        }
    }

Recommended Answers

All 2 Replies

>Exception has been thrown by the target of an invocation.

First of all check for an inner exception.

>Exception has been thrown by the target of an invocation.

First of all check for an inner exception.

Inner exception is: "{"Could not load file or assembly 'ClientApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"ClientApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}" due to that the server is trying to call the event handler while the event handler exists in the client and since the client assembly is not available on the side of the server

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.