CS-Script 3.27.0


Remoting


.NET Remoting is a Microsoft application programming interface (API) for interprocess communication. Remoting implementation is based on the client-server communication pattern and both these components (client and server) can be implemented as C# scripts.

Remoting is a complex topic, which includes communication protocols, serialization, objects life cycle and some other particular implementation aspects. Of course it is not possible to cover all of them in this document and actually it is not it's purpose. This document will just describe how to implement very specific Remoting scenario with C# scripting.

When you use Remoting your server application exposes it's interface to the client application in the one of the following ways:
  1. interface is defined in the separate class library assembly
  2. interface is defined in the server assembly
    Of course your choice of protocol and serialization will affect the way the client is interfacing the server application. In the examples below Remoting is set to use HTTP as a transport protocol.


All samples below are from the CS-Script samples library (Script Library) and they are just slightly modified verion of the  MSDN Remoting CompanyList example

Server (Samples\Remoting\server.cs)
using System;
using System.Collections.Generic;
using System.Collections;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

public class CompanyLists : MarshalByRefObject
{
    private String[] Countries = {"Spain","France","Italy"};

    public String[] getCountryList()
    {
        return Countries;
    }

    public void addCountryList(string data)
    {
        List<string> list = new List<string>();
        list.AddRange(Countries);
        list.Add(data); 
        Countries = list.ToArray();
    }
}

class Script
{
    [STAThread]
    static public void Main(string[] args)
    {
        ChannelServices.RegisterChannel(new HttpChannel(8086));
        RemotingConfiguration.ApplicationName = "MyRemotingApp";
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(CompanyLists),
                                                            "CompanyLists",
                                                            WellKnownObjectMode.Singleton);

        Console.WriteLine("Press [Enter] to exit...");
        Console.ReadLine();
    }
}

Client
(Samples\Remoting\clientConsole.cs)
//css_pare soapsuds(http://localhost:8086//MyRemotingApp/CompanyLists?WSDL, CompanyLists, -new); 
//css_ref CompanyLists.dll;
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

class Script
{
    static public void Main(string[] args)
    {
        CompanyLists cLst = (CompanyLists)Activator.GetObject(typeof(CompanyLists),
                                                              "http://localhost:8086/CompanyLists",
                                                              WellKnownObjectMode.Singleton);
        cLst.addCountryList("Australia");
    }
}

This server code contains partial implementation of the CompanyLists class. It contains string array Countries which can be accessed by Remoting clients.

The client code inserts new entry (Australia) into Countries array with call addCountryList().

The code is strightforward however there is some interesting point about this example. The system is implemented as only two scripts, even though the client and the server application may not be located on the same PC. CompanyLists is not implemented as shared script/assembly, it is merged with the server implemention. So how in such case client can know the CompanyLists type?

Interestingly MS soapsuds.exe utility is capable of producing assembly (proxy) containing the implementation  of the type exposed by the Remoting server. CS-Script can prepare such proxy assembly during a script pre-execution step, so you do not need to do it manually. This is exactly what the code below does:

//css_pare soapsuds(http://localhost:8086//MyRemotingApp/CompanyLists?WSDL, CompanyLists, -new);

And after the proxy assembly (CompanyLists.dll) is prepared it is referenced by the client script as any other assembly:
//css_ref CompanyLists.dll;

See Also

Script LibraryPre- and Post-execution scripts