我创建了了一个书上的一个REMOTE应用的例子,如下:
//远程对象类定义
using System;
using System.Runtime.Serialization;namespace RemoteHello
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
[SerializableAttribute]
public class Hello
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
public Hello()
{
Console.WriteLine("Constructor called!");
} ~Hello()
{
Console.WriteLine("Destructor called!");
} public string Greeting(string name)
{
Console.WriteLine("Gretting Called!");
return "Hello,"+name;
}
}
}
//客户端
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;
using RemoteHello;namespace HelloClient
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class HelloClient
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
string test="";
static void Main(string[] args)
{
System.Runtime.Remoting.Channels.Tcp.TcpClientChannel channel=new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel();
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel);
try
{
Hello hello=(RemoteHello.Hello)Activator.GetObject(typeof(RemoteHello.Hello),"tcp://localhost:8086/Hi"); if(hello==null)
{
Console.WriteLine("Can not locate server!");
return;
}
for(int i=0;i<5;i++)
{
Console.WriteLine(hello.Greeting("LEE "+i.ToString()));
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}//服务端
using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteHello;namespace RemoteServer
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class RemoteServer
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.Runtime.Remoting.Channels.Tcp.TcpServerChannel chanel=new TcpServerChannel(8086);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chanel);
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteHello.Hello),"Hi",
System.Runtime.Remoting.WellKnownObjectMode.SingleCall); Console.WriteLine("Hit to exit!");
System.Console.ReadLine();
}
}
}//结束执行结果如下:
服务端运行以后,信道都已经建立起来了,我用命令窗口
NETSTAT -A 看了也没问题 但客户端运行到
Hello hello=(RemoteHello.Hello)Activator.GetObject(typeof(RemoteHello.Hello),"tcp://localhost:8086/Hi"); 发生异常: {"试图创建未绑定类型的代理。" },不知道是什么原因,
请高手指点!感激不胜!
//远程对象类定义
using System;
using System.Runtime.Serialization;namespace RemoteHello
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
[SerializableAttribute]
public class Hello
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
public Hello()
{
Console.WriteLine("Constructor called!");
} ~Hello()
{
Console.WriteLine("Destructor called!");
} public string Greeting(string name)
{
Console.WriteLine("Gretting Called!");
return "Hello,"+name;
}
}
}
//客户端
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Http;
using RemoteHello;namespace HelloClient
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class HelloClient
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
string test="";
static void Main(string[] args)
{
System.Runtime.Remoting.Channels.Tcp.TcpClientChannel channel=new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel();
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel);
try
{
Hello hello=(RemoteHello.Hello)Activator.GetObject(typeof(RemoteHello.Hello),"tcp://localhost:8086/Hi"); if(hello==null)
{
Console.WriteLine("Can not locate server!");
return;
}
for(int i=0;i<5;i++)
{
Console.WriteLine(hello.Greeting("LEE "+i.ToString()));
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}//服务端
using System;
using System.Runtime;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteHello;namespace RemoteServer
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class RemoteServer
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.Runtime.Remoting.Channels.Tcp.TcpServerChannel chanel=new TcpServerChannel(8086);
System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(chanel);
System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteHello.Hello),"Hi",
System.Runtime.Remoting.WellKnownObjectMode.SingleCall); Console.WriteLine("Hit to exit!");
System.Console.ReadLine();
}
}
}//结束执行结果如下:
服务端运行以后,信道都已经建立起来了,我用命令窗口
NETSTAT -A 看了也没问题 但客户端运行到
Hello hello=(RemoteHello.Hello)Activator.GetObject(typeof(RemoteHello.Hello),"tcp://localhost:8086/Hi"); 发生异常: {"试图创建未绑定类型的代理。" },不知道是什么原因,
请高手指点!感激不胜!
解决方案 »
- C# 求解 很简单的一个代码 本人刚刚接触
- 数据插入缓存 memcache键值该怎么设计
- Winform 程序完成后怎么部署打包(就是可以在客户端运行应用程序)
- 很实用的问题:SQL语句3张表连接查询,表中某个数据为double,但允许为空,但要把这条数据和低于安全库存的数据一起查出来,怎么办?
- "无法从传输连接中读取数据:远程主机强迫关闭了一个现有的连接"这个是什么错误啊?
- .net程序打包问题
- MSN聊天记录,用什么工具打开看,格式不会乱呀?
- 当改变combobox,要发生什么事件??
- 请教:多线程原理-函数临时变量、对象属性、堆栈
- 这个TreeView应该如何写比较好??
- Reflector反编译后修改源文件如何编译回去?
- C#中如何解压gzip压缩后的字符流?
楼上没错。public class Hello : System.MarshalByRefObject
{
...
}
2.服务器端配置对象如你所写的那样,不用在配置文件里进行配置,因为这样会带来,部分对象占用内存资源,不释放的问题。
3.调用Remoting对象,最好用一个RemotingProxy层来做代理\....
请楼主以后别忘了,做remoting的时候远程对象类一定要继承于这个类,否则就需要自己做序列化工作