Remoting 找不到请求的服务

aimyray aimyray 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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");
出现异常提示:找不到指定服务。
...全文
评论 7
收藏 已收藏
分享
返回顶部
aimyray
C# 111,129 成员 642,540 内容