2010-02-18 33 views
0

我正在学习如何在.NET中使用Remoting。我已经构建了一个小应用程序,它可以完成基本的Hello World工作。 为RemoteObject的代码是:C#中的远程处理不工作

public class MyRemoteObjectClass:MarshalByRefObject 
    { 
     public MyRemoteObjectClass() 
     { 
      Console.WriteLine("Remote object created"); 
     } 
     //return message reply 
     public String ReplyMessage(String msg) 
     { 
      Console.WriteLine("Client : " + msg);//print given message on console 
      return "Server : Yeah! I'm here"; 
     } 
    } 

服务器类的代码是:

class MyServerClass 
    { 
     public MyServerClass() 
     { 
     } 
     static void Main(string[] args) 
     { 
      RemotingConfiguration.Configure("RemotingSettings_Server.xml",false); 
      Console.WriteLine("server activated"); 
      Console.ReadLine(); 
     } 
    } 

客户端类的源代码是:

class MyClientAppClass 
    { 
     static void Main(string[] args) 
     { 
      RemotingConfiguration.Configure("ClientSettings.xml",false); 
      Console.WriteLine("Settings read successfully"); 
      MyRemoteObjectClass remObject = (MyRemoteObjectClass)Activator.GetObject(typeof(MyRemoteObject.MyRemoteObjectClass),"http://localhost:8989/MyRemoteObjectClass",WellKnownObjectMode.Singleton); 
      if (remObject == null) 
       Console.WriteLine("cannot locate server"); 
      else 
      { String res = remObject.ReplyMessage("You there?"); Console.WriteLine(res); Console.ReadLine(); } 

     } 
    } 

服务器和客户端我的配置文件分别为:

<configuration> 
    <system.runtime.remoting> 
     <application> 
     <service> 
      <wellknown 
       mode="Singleton" 
       type="MyRemoteObject.MyRemoteObjectClass, MyRemoteObjectClass" 
       objectUri="MyRemoteObjectClass.rem" 
      /> 
     </service> 
     <channels> 
      <channel ref="http" port="8989"/> 
     </channels> 
     </application> 
    </system.runtime.remoting> 
</configuration> 

客户端:

<configuration> 
    <system.runtime.remoting> 
    <application> 
     <client> 
     <wellknown 
      type="MyRemoteObject.MyRemoteObjectClass, MyRemoteObjectClass" 
      url="http://localhost:8989/MyRemoteObjectClass.rem" 
      /> 
     </client> 
    </application> 
    </system.runtime.remoting> 
</configuration> 

我第一次建立了DLL的远程对象,复制在服务器和客户端的exe位置的DLL。然后运行服务器,然后运行客户端。服务器被实例化,但客户端程序会引发异常,如“远程处理异常:请求的服务未找到”。

请帮我解决这个问题,并且可能是清楚了解远程处理概念的好地方。

谢谢, Rakesh。

+1

远程处理非常困难,而且记录错误。跳过远程处理并直接转到WCF。 – Will 2010-06-19 16:21:38

回答

0

查看您的配置文件 - 区分大小写。例如,“objecturi”应该是 “objectUri”。

+0

嗨阿隆,我已经添加我的配置文件也是我的问题。根据你所说的话,这似乎是正确的。 – 2010-02-18 06:18:16

0

取消激活防火墙。如果它能够正常工作,请重新激活防火墙并添加适当的例外。