2012-12-04 33 views
1

我是.NET Remoting和C#的新手。我需要一个客户端/服务器应用程序,并希望通过.NET Remoting来处理这个问题。我已经为远程对象EchoServer类写了一个类库,并带有一些测试方法。.NET Remoting:在RegisterWellKnownServiceType上的“Value NULL”异常

我添加到Visual Studio中的服务器项目中的类库。程序集“System.Runtime.Remoting”我也添加了。

以下是我的服务器代码:

 using System; 
     using System.Collections.Generic; 
     using System.ComponentModel; 
     using System.Data; 
     using System.Drawing; 
     using System.Linq; 
     using System.Text; 
     using System.Windows.Forms; 
     using System.Runtime.Remoting; 
     using System.Runtime.Remoting.Channels; 
     using System.Runtime.Remoting.Channels.Tcp; 
     using Remoting; //Namespace Lib 

     namespace Server 
     { 
     public partial class Server : Form 
{ 
    public Server() 
    { 
     InitializeComponent(); 

     TcpChannel serverChannel = null; 

     try 
     { 
      serverChannel = new TcpChannel(9998); 
      lvStatus.Items.Add("Server is listening on port 8089..."); 

      string strIn = ""; 

      ChannelServices.RegisterChannel(serverChannel, true); 

      RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("Remoting.EchoServer, remoting_dll"), "Echo", WellKnownObjectMode.SingleCall); 
     } 
     catch (Exception ex) 
     { 
      ChannelServices.UnregisterChannel(serverChannel); 
      MessageBox.Show(ex.Message.ToString()); 
     } 
    } 
} 

}

如果我启动服务器时,我会得到一个异常:

的值不能为NULL 参数名称:类型

我试过一些教程的其他代码,但我会得到相同的excetion,如果远程对象的类是im,作为一个类库有特色,或者直接在我的项目中作为一个类。

回答

0

你可以发布Remoting的实现吗? 我的事情,你的错误是下一个:

“Remoting.EchoServer,remoting_dll”

所以,你应该正确使用Type.GetType。的工作代码

实施例:

static void Main(string[] args) 
{ 
    Server(); 
} 

static void Server() 
{ 
    Console.WriteLine("Server started..."); 
    var httpChannel = new HttpChannel(9998); 
    ChannelServices.RegisterChannel(httpChannel); 
    RemotingConfiguration.RegisterWellKnownServiceType(Type.GetType("Server.Program+SomeClass"), "SomeClass", WellKnownObjectMode.SingleCall); 
    Console.WriteLine("Press ENTER to quit"); 
    Console.ReadLine(); 
} 

public interface ISomeInterface 
{ 
    string GetString(); 
} 

public class SomeClass : MarshalByRefObject, ISomeInterface 
{ 
    public string GetString() 
    { 
     const string tempString = "ServerString"; 
     Console.WriteLine("Server string is sended: {0}", tempString); 
     return tempString; 
    } 
}