Harris

Harris

  • NA
  • 4
  • 0

Remoting Interfaces by using Assemblies

Oct 11 2009 7:15 AM
Hello guys,

I am trying to create a remoting application to which I don't want the client to know anything about the implementation but just the interfaces. On the server side, it will load the implementation dll by using Assembly. The problem with this code is that when I run the application, the client will successfully get the remote object however when I try to call the method from it (also defined inside the interfaces), an exception is thrown (i.e. FileNotFoundException). I am not sure what to do, can you guys help me with this.?

Interfaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
    public interface ICustomer
    {
        void setName(string name);
        string getName();
    }
}


Implementation

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;

namespace Implementation
{
    public class Customer : MarshalByRefObject, ICustomer
    {
        string name;

        public Customer()
        {
            name = "harris";
        }

        public void setName(string name)
        {
            this.name = name;
        }

        public string getName()
        {
            return name;
        }
    }
}


Server

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Reflection;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Reflection.Assembly newAssembly = System.Reflection.Assembly.LoadFrom(@"C:\Documents and Settings\BP FANTONI\Desktop\RemotingApp3\Implementation\bin\Debug\Implementation.dll");
            Object o = newAssembly.CreateInstance("Implementation.Customer");
           
            //Assembly a = Assembly.LoadFrom("C:\\Documents and Settings\\BP FANTONI\\Desktop\\RemotingApp3\\Implementation\\bin\\Debug\\Implementation.dll");
            //Module m = newAssembly.GetModule("Implementation.dll");
            Type t = o.GetType();

            System.Runtime.Remoting.Channels.Tcp.TcpServerChannel channel = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(8080);
            ChannelServices.RegisterChannel(channel, false);
            RemotingConfiguration.RegisterWellKnownServiceType(t, "customer", WellKnownObjectMode.Singleton);
            //RemotingConfiguration.ApplicationName = "remoteserver";
            //RemotingConfiguration.RegisterActivatedServiceType(t);

            Console.WriteLine("Press enter to exit.");
            Console.Read();
        }
    }
}


Client

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Channels;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Runtime.Remoting.Channels.Tcp.TcpClientChannel channel = new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel();
            ChannelServices.RegisterChannel(channel, false);
            ICustomer cust = (ICustomer)Activator.GetObject(typeof(ICustomer), "tcp://localhost:8080/customer");
            //ICustomer cust = (ICustomer)Activator.CreateInstance(typeof(ICustomer), null, new object[] {new UrlAttribute("tcp://localhost:8080")} );
            RemotingConfiguration.RegisterActivatedClientType(typeof(ICustomer), "tcp://localhost:8080/remoteserver");
            //ICustomer cust = n
            Console.WriteLine("Name: " + cust.getName());
            Console.Read();
        }
    }
}

Answers (2)