Click here to Skip to main content
15,884,986 members
Articles / Programming Languages / C#

Grid Computing Using C# Script and .NET Remoting

Rate me:
Please Sign up or sign in to vote.
5.00/5 (20 votes)
10 Apr 2010CPOL6 min read 75.1K   2K   59  
Using the C# script engine inside a network using .NET Remoting.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Messaging;
using ClientServer;

namespace ClientTest
{
    class Program
    {
        static IRemoteObject remObj;
        static void Main(string[] args)
        {
            ChannelServices.RegisterChannel(new HttpChannel(9834), false);
            CallbackSink sink = new CallbackSink();
            sink.OnHostToClient += new ReturnEvent(sink_OnHostToClient);
            remObj = (IRemoteObject)Activator.GetObject(typeof(IRemoteObject), "http://10.0.0.129:8086/Execute");
            if (remObj == null)
            {
                Console.WriteLine("could not locate server");
                return;
            }
            try
            {
                remObj.RegisterCallback(new ReturnEvent(sink.HandleToClient));
            }
            catch (Exception ex)
            {
            }
            string scriptCode = @"using System;          
                                  using System.Windows.Forms;        
                                  public class TestClass                      
                                  {                                      
                                     static public void Main(string[] args)
                                     {
                                        if(args.Length > 0)
                                        {
                                            MessageBox.Show(args[0]);
                                            Console.WriteLine(args[0]);
                                        }
                                        else
                                        {
                                            MessageBox.Show(String.Empty);
                                            Console.WriteLine(String.Empty);
                                        }
                                     }                                 
                                  }";
            remObj.ExecuteCode(scriptCode, "*.Main", new string[] { "Helloooooooo" });
            //remObj.ExecuteCode(scriptCode, "*.Main", null, false);
            Console.WriteLine("Data had been sent...");
            Console.ReadLine();
        }
        static bool firstTime = true;
        static void sink_OnHostToClient(ReturnEventArgs e)
        {
            if (e.CodeException != null)
                Console.WriteLine("damn it!!! code exception.");
            if(e.ExecuterException != null)
                Console.WriteLine("damn it!!! executer exception.");
            else if (e.CodeException == null && e.ExecuterException == null)
            {
                Console.WriteLine("Hurraaaaaaaaaa!!!");
                if (firstTime)
                {
                    ExecuteByHash(e.HashCode);
                    firstTime = false;
                }
            }
        }

        private static void ExecuteByHash(string p)
        {
            remObj.ExecuteAlreadyExecuted(p, "*.Main", new string[] { "Helloooooooo" });
        }

    }

    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I'm an undergraduate student of IT.
I'm a C# programmer and have some experience in robotics, image processing, telephony systems, etc.

Comments and Discussions