实现了一个简单的remoting机制,问题出现在客户端调用远程对象的函数时,远程对象调用了自己的函数而没有调用我希望他调用的服务器端的函数,请问这是什么问题?要如何改进呢?客户端:using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text.RegularExpressions;
using RemotingService;
namespace RemotingClient
{
    class DataGetter
    {
        static public QueryServiceLib remotingObj = new QueryServiceLib();        //        public DataGetter()
        static void Main(string[] args)
        {
            try
            {
                remotingObj = (QueryServiceLib)Activator.GetObject(typeof(QueryServiceLib)
                                                 , "tcp://127.0.0.1:7777/RemotingQuery");
                if (remotingObj == null)
                    Console.WriteLine("cannot locate server!");
                else
                {
                    Console.WriteLine("Input: ");
                    string rawInput;
                    string mdInput;
                    rawInput = Console.ReadLine();                    string result = remotingObj.SearchQuery(rawInput);
                    Console.WriteLine(result);
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}
服务器端:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.IO;
using SimpleKeyValueLookup;
using System.Text.RegularExpressions;
using System.Collections;
using RemotingService;namespace RemotingServer
{
    class Program
    {        static SimpleKeyValueLookup.SimpleKeyValueLookup indexer = null;        static void Main(string[] args)
        {
            try
            {
                TcpServerChannel tcpChannel = new TcpServerChannel(7777);
                ChannelServices.RegisterChannel(tcpChannel, false);
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(QueryServiceLib)
                    , "RemotingQuery", WellKnownObjectMode.Singleton);                string indexdir = @"D:\v-zijian\adChemy\sd";
                indexer = new SimpleKeyValueLookup.SimpleKeyValueLookup(indexdir);  //Load index file into memory
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            string mdInput;
            Console.WriteLine("Please input 'exit' to end the program.");
            while (!(mdInput = Console.ReadLine()).Equals("exit", StringComparison.OrdinalIgnoreCase))
            {
            }        }
        public string SearchQuery(string query)
        {
            string result = indexer.Lookup(query);
            return result;
        }
    }
}
远程对象:using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;namespace RemotingService
{
    public class QueryServiceLib : MarshalByRefObject
    {
        public QueryServiceLib()
        {
            Console.WriteLine("QueryService started");
        }        public string SearchQuery(string query)
        {
            string result = "11";
            return result;
        }
    }
}

解决方案 »

  1.   

    Server<--dll
    Client<--dll服务器和客户端使用同一个dll,连接后,你调用的是服务器上对象的函数,如果想调用服务器程序中的方法。你可以这样
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;namespace RemotingService
    {
        public class QueryServiceLib : MarshalByRefObject
        {
            public static Func<string,string> fun=null;
            public QueryServiceLib()
            {
                Console.WriteLine("QueryService started");
            }        public string SearchQuery(string query)
            {
                if(fun!=null) return fun(query);
                else return string.Empty;
            }
        }
    }
    服务器代码using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.Remoting;
    using System.Runtime.Remoting.Channels;
    using System.Runtime.Remoting.Channels.Tcp;
    using System.IO;
    using SimpleKeyValueLookup;
    using System.Text.RegularExpressions;
    using System.Collections;
    using RemotingService;namespace RemotingServer
    {
        class Program
        {        static SimpleKeyValueLookup.SimpleKeyValueLookup indexer = null;        static void Main(string[] args)
            {
                try
                {
                    TcpServerChannel tcpChannel = new TcpServerChannel(7777);
                    ChannelServices.RegisterChannel(tcpChannel, false);
                    RemotingConfiguration.RegisterWellKnownServiceType(typeof(QueryServiceLib)
                        , "RemotingQuery", WellKnownObjectMode.Singleton);
                    QueryServiceLib.fun = SearchQuery;                string indexdir = @"D:\v-zijian\adChemy\sd";
                    indexer = new SimpleKeyValueLookup.SimpleKeyValueLookup(indexdir);  //Load index file into memory
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
                string mdInput;
                Console.WriteLine("Please input 'exit' to end the program.");
                while (!(mdInput = Console.ReadLine()).Equals("exit", StringComparison.OrdinalIgnoreCase))
                {
                }        }
            public string SearchQuery(string query)
            {
                string result = indexer.Lookup(query);
                return result;
            }
        }
    }
    其他一样。