-->

C# How to pass Linq in an interface - MarshalByRef

2019-07-25 15:44发布

问题:

This is a follow up question from here but is of totally different nature. The reason i mention it is because I wanted to provide premise.

Basically I'll have a few Datatables (or serialize classes) that will contain over a million rows and will be very hard to return from SQL.

So using an example I found I derived a TCP Channelservices utilizing MarshalByRefObject to return the top x rows. However this really isn't powerful enough for what I want to do. I'm curious out of the code below, how do I provide my interface an LINQ statement so I can run any LINQ against my DataTable.

Thanks!

Code use - create two VS solutions - start server, start client

--- Server

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Data;
using System.Linq;

class Program
{


    public static DataTable myTimetable { get; set; }
    public static string[] myVars1 = { "apples", "pears", "peaches" };
    public static string[] myVars2 = { "bears", "lions", "eagles" };
    public static string[] myVars3 = { "car", "truck", "train" };
    public static string[] myVars4 = { "bat", "ball", "glove" };
    public static string[] myVars5 = { "lawyer", "court", "state" };
    public static Random rand1 = new Random();
    public static Random rand2 = new Random();
    public static Random rand3 = new Random();

    static void Main(string[] args)
    {
        DataObjectsServer();
    }
    static void DataObjectsServer()
    {
        Console.WriteLine("Data Objects server started...");

        myTimetable = new DataTable();
        myTimetable.Columns.Add("Col1");
        myTimetable.Columns.Add("Col3");
        myTimetable.Columns.Add("Col4");
        myTimetable.Columns.Add("Col5");
        myTimetable.Columns.Add("Col2");
        myTimetable.Columns.Add("Col6");


        for (int i = 0; i < 1000000; i++)
        {

            int test1 = rand1.Next(0, 2);
            int test3 = rand2.Next(0, 2);
            int test2 = rand3.Next(0, 2);
            DataRow dt = myTimetable.NewRow();
            dt[0] = myVars3[test1];
            dt[1] = myVars1[test2];
            dt[2] = myVars2[test3];
            dt[3] = myVars3[test2];
            dt[4] = myVars5[test1];
            dt[5] = myVars4[test3];
            myTimetable.Rows.Add(dt);
        }

        TcpChannel tcpChannel = new TcpChannel(9998);
        ChannelServices.RegisterChannel(tcpChannel);

        Type commonInterfaceType = Type.GetType("DataObjects");

        RemotingConfiguration.RegisterWellKnownServiceType(commonInterfaceType,
        "DataObjectsServer", WellKnownObjectMode.SingleCall);

        System.Console.WriteLine("Press ENTER to quitnn");
        System.Console.ReadLine();

    }

}

public interface DataObjectsServerInterface
{
    DataTable GetMyDataRows(int topXRow);

    //how do I provide a linq arguement!?!?
}

public class DataObjects : MarshalByRefObject, DataObjectsServerInterface
{

    public DataTable GetMyDataRows(int topxRow)
    {
        Console.WriteLine("X Row Request: " + topxRow);

        return Program.myTimetable.AsEnumerable().Take(topxRow).CopyToDataTable();

    }
}

````client

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Data;

class MyClient
{
    public static void Main()
    {
        TcpChannel tcpChannel = new TcpChannel();
        ChannelServices.RegisterChannel(tcpChannel);

        Type requiredType = typeof(DataObjectsServerInterface);

        DataObjectsServerInterface remoteObject = (DataObjectsServerInterface)Activator.GetObject(requiredType,
        "tcp://localhost:9998/DataObjectsServer");

        DataTable dt = remoteObject.GetMyDataRows(100);

        Console.WriteLine("rows received:" + dt.Rows.Count);
        Console.ReadLine();
    }
}
public interface DataObjectsServerInterface
{
    DataTable GetMyDataRows(int topXRow);

    //how do I provide a linq arguement!?!?
}