Hi there,
I am trying to create a remote object which uses Interfaces and .Net Remoting
I am really wondering if I am on the right track or if I need to go and review everything that I am doing. Any help or updates in code would be greatly appriciated.
What I want to do is listed in four steps so my question is this:
Is what I want to do possible??
1: Create an Interface DLL with a namespace DoSomethingInterface which contains a Class IDoSomething which has a method GetString that returns a string.
Code:
using System;
namespace DoSomethingInterface
{
public interface IDoSomething
{
string GetString() ;
}
}
2: Create a seperate DLL with a namespace DoSomethingImplementation which contains a class DoSomethingImplementation which implements IDoSomething.
Code:
using System ;
using System.Runtime.Remoting ;
using System.Runtime.Serializati
on ;
using DoSomethingInterface ;
namespace DoSomethingImplementation
{
[Serializable]
public class DoSomethingImp : MarshalByRefObject, IDoSomething
{
public DoSomethingImp()
{
}
string IDoSomething.GetString()
{
return "This is a string from DoSomethingImp" ;
}
}
}
4: Create a Console/Service application called DoSomethingServer which listens for Remoting Tcp requests for the IDoSomething class.
Code:
using System;
using System.Runtime.Remoting ;
using System.Runtime.Remoting.Ch
annels ;
using System.Runtime.Remoting.Ch
annels.Tcp
;
using System.Runtime.Serializati
on ;
using DoSomethingInterface ;
namespace DoSomethingServer
{
class EnrtyPoint
{
[STAThread]
static void Main(string[] args)
{
TcpServerChannel myChannel = new TcpServerChannel(9988) ;
ChannelServices.RegisterCh
annel(myCh
annel) ;
RemotingConfiguration.Regi
sterWellKn
ownService
Type(typeo
f (IDoSomething),"IDoSomethi
ng",WellKn
ownObjectM
ode.Single
Call) ;
Console.WriteLine("Server Running") ;
Console.WriteLine("Press Any Key!") ;
Console.ReadLine() ;
}
}
}
5: Create a Client Console application which calls the DoSomethingImplementation via the DoSomethingServer and the IDoSomethingInterface
Code:
using System;
using System.Runtime.Remoting ;
using System.Runtime.Remoting.Ch
annels ;
using System.Runtime.Remoting.Ch
annels.Tcp
;
using DoSomethingInterface ;
namespace DoSomethingClient
{
class Client
{
[STAThread]
static void Main(string[] args)
{
ChannelServices.RegisterCh
annel(new TcpClientChannel()) ;
IDoSomething myDoSomething = (IDoSomething)Activator.Ge
tObject(ty
peof(IDoSo
mething),"
tcp://loca
lhost:9988
/DoSomethi
ngImp") ;
Console.WriteLine(myDoSome
thing.GetS
tring().To
String()) ;
Console.WriteLine("Press Any Key!") ;
Console.ReadLine() ;
}
}
}
Thanks for any help in advance
Start Free Trial