作了一个最简单的.net Remoting的例子:1、远程对象: 
建立类库项目:RemoteObject using System; namespace RemoteObject 

    public class MyObject:MarshalByRefObject 
    { 
        public int Add(int a,int b) 
        { 
            return a+b; 
        } 
    } 
} 2、服务端
建立控制台项目:RemoteServerusing System;
using System.Runtime.Remoting;namespace RemoteServer
{
    class MyServer
    {
        [STAThread]
        static void Main(string[] args)
        {
            RemotingConfiguration.Configure("RemoteServer.exe.config");
            Console.ReadLine();
        }
    }
}建立配置文件:app.config
<configuration>
    <system.runtime.remoting>
        <application name="RemoteServer">
            <service>
                <wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="RemoteObject.MyObject"
                    mode="Singleton" />
            </service>
            <channels>
                <channel ref="tcp" port="9999"/>
            </channels>
        </application>
    </system.runtime.remoting>
</configuration>3、客户端:
建立控制台项目:RemoteClientusing System;namespace RemoteClient
{
    class MyClient
    {
        [STAThread]
        static void Main(string[] args)
        {
            RemoteObject.MyObject app = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject),System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]);
            Console.WriteLine(app.Add(1,2));
            Console.ReadLine();
        }
    }
}建立配置文件:app.config
<configuration>
 <appSettings>
 <add key="ServiceURL" value="tcp://localhost:9999/RemoteObject.MyObject"/>
 </appSettings>
</configuration>4、测试
在最后编译的时候会发现编译报错:
1、找不到app.Add()
2、找不到RemoteObject
这是因为客户端RemoteClient没有添加RemoteObject的引用,编译器并不知道远程对象存在哪些成员所以报错,添加引用以后vs.net会在客户端也保存一个dll,可能大家会问这样如果对远程对象的修改是不是会很麻烦?其实不麻烦,对项目编译一次vs.net会重新复制dll。
然后直接运行客户端会出现“目标主机拒绝”的异常,也说明了通道没有打开
运行服务端再运行客户端出现“找不到程序集RemoteObject”!回头想想可以发现我们并在服务端对RemoteObject添加引用,编译的时候通过是因为这个时候并没有用到远程对象,大家可能不理解运行服务端的时候也通过?这是因为没有这个时候还没有激活远程对象。理所当然,对服务端要添加引用远程对象,毕竟我们的对象是要靠远程承载的。
现在先运行RemoteServer,然后运行RemoteClient,结果在RemoteClient中出现如下的错误提示:
---------------------------未处理的“System.IO.FileNotFoundException”类型的异常出现在 mscorlib.dll 中。其他信息: 找不到文件或程序集名称“RemoteObject”,或找不到它的一个依赖项。-------------------------------------对应的代码是:   Console.WriteLine(app.Add(1,2));为什么?请高手指教

解决方案 »

  1.   

    客户端服务端是不是都对RemoteObject项目添加了引用
      

  2.   

    namespace RemoteServer
    {
        class MyServer
        {
            [STAThread]
            static void Main(string[] args)
            {
                RemotingConfiguration.Configure("RemoteServer.exe.config");//你这里面调用的配置文件是RemoteServer.exe.config,而你下面的配置文件名字是:app.config.请用同一个名字
                Console.ReadLine();
            }
        }
    }建立配置文件:app.config
    <configuration>
        <system.runtime.remoting>
            <application name="RemoteServer">
                <service>
                    <wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="RemoteObject.MyObject"
                        mode="Singleton" />
                </service>
                <channels>
                    <channel ref="tcp" port="9999"/>
                </channels>
            </application>
        </system.runtime.remoting>
    </configuration>
      

  3.   

    to 楼上:
    我用了你的方法,把RemoteServer.exe.config改成app.config可是出现如下的错误:-------------------------
    未处理的“System.Runtime.Remoting.RemotingException”类型的异常出现在 mscorlib.dll 中。其他信息: 因发生异常 System.IO.FileNotFoundException: 系统找不到指定的文件。
       at System.IConfigHelper.Run(IConfigHandler factory, String fileName)
       at System.ConfigTreeParser.Parse(String fileName, String configPath) 而无法成功读取 .Config 文件 App.config。---------------------------------没改之前还没有抱错,这是为什么啊
      

  4.   

    System.IO.FileNotFoundException”类型的异常出现在 mscorlib.dll 中。——配置文件路径问题。
      

  5.   

    为什么RemoteServer.exe.config不抱错,而app.config反而抱错,到底哪里出了问题?
      

  6.   

    vs.net会自动把app.config拷贝到允许目录并且改名,所以
    RemotingConfiguration.Configure("RemoteServer.exe.config");
    是对的
      

  7.   

    谢谢LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^) 那么你看看我第一楼贴出的代码,
    为什么会抱错那?