My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
GettingStartedCSharp  
Getting started with the C# implementation.
Featured
Updated May 16, 2010 by niallr...@gmail.com

Creating the service definition

An interface can optionally be used to define the service. It can then be used by both the server and the client.

public interface ISampleService
{
    int GetSquare(int x);
    string GetDate();
}

Creating a RPC server

First the service should be implemented:

public class SampleService : ISampleService
{
    public int GetSquare(int x)
    {
        return x*x;
    }

    public string GetDate()
    {
        return DateTime.Now.ToLongDateString();
    }
}

Then the server can be created to handle requests to this service:

//create the server
var controller = new RpcController();
server = new RpcServer(controller);

//register the service with the server. We must specify the interface explicitly since we did not use attributes
server.RegisterService<ISampleService>(new SampleService());

//create and start the channel which will receive requests
var channel = new NetworkStreamRpcChannel(controller, networkStream);
channel.Start();

Creating a RPC client

First we create the client which will be used to send the requests:

//create the client
var controller = new RpcController();
client = new RpcClient(controller);

//create and start the channel which will receive requests
var channel = new NetworkStreamRpcChannel(controller, networkStream);
channel.Start();

Now a proxy can be created to make calls to a particular service:

ISampleService service = client.GetProxy<ISampleService>();

//now calls can be made, they will block until a result is received
int x = service.GetSquare(10);
string date = service.GetDate();
Comment by thomasha...@gmail.com, Apr 13, 2014

what are the requirements for the network stream ?? as far as i can read the code in "Creating a Server" its the intention to open an port and the server starts listening to it, waiting for incomming calls, right ??

i get an IOException: i cant do this with not-connected sockets:

socket = new System.Net.Sockets.Socket(serverEndPoint.Address.AddressFamily?, SocketType?.Stream, ProtocolType?.Tcp);
socket.Bind(serverEndPoint); socket.Listen(1024 64);

NetworkStream? networkStream = new NetworkStream?(socket, true);


Sign in to add a comment
Powered by Google Project Hosting