Remoting 找不到请求的服务
c#高级编程书上的例子:
服务器端:
HelloServer:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using RemoteHello;
namespace HelloWerver
{
class Program
{
static void Main(string[] args)
{
TcpServerChannel channel = new TcpServerChannel(8086);
HelloServer.ShowChannelProperties(channel);
ChannelServices.RegisterChannel(channel, false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello), "Hi", WellKnownObjectMode.SingleCall);//把知名的远程对象类型注册为RemotingServices
System.Console.WriteLine("press return to exit");
System.Console.ReadLine();
//channel.ChannelData
}
}
}
RemoteHello.dll:
using System;
using System.Collections.Generic;
using System.Text;
namespace RemoteHello
{
public class Hello:System.MarshalByRefObject
{
public Hello()
{
Console.WriteLine("Constructor called");
}
~Hello()
{
Console.WriteLine("Destructor called");
}
public string Greeting(string name)
{
Console.WriteLine("Greeting called");
return "Hello," + name;
}
}
}
客户端:HelloClient2
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using RemoteHello;
namespace HelloClient2
{
class Program
{
static void Main(string[] args)
{
//object[] attrs ={ new UrlAttribute("tcp://localhost:8086/HelloServer") };
//ObjectHandle handle = Activator.CreateInstance("RemoteHello", "RemoteHello.Hello", attrs);
//if (handle == null)
//{
// Console.WriteLine("could not locate server");
// return;
//}
RemotingConfiguration.RegisterActivatedClientType(typeof(Hello), "tcp://localhost:8086/HelloServer");
Hello obj = new Hello();//(Hello)handle.Unwrap();
Console.WriteLine(obj.Greeting("Christian"));
Console.ReadLine();
}
}
}
客户端运行到 RemotingConfiguration.RegisterActivatedClientType(typeof(Hello), "tcp://localhost:8086/HelloServer");
出现异常提示:找不到指定服务。