using System; namespace RemoteObject{ public class MyObject : MarshalByRefObject { public int Add(int a, int b) { return a + b; } }} 2、服务端建立控制台项目:RemoteServerusing System;using System.Runtime.Remoting; namespace RemoteServer{ class MyServer { [STAThread] static void Main(string[] args) { RemotingConfiguration.Configure("RemoteServer.exe.config"); Console.ReadLine(); } }}建立配置文件:app.config<configuration> <system.runtime.remoting> <application name="RemoteServer"> <service> <wellknown type="RemoteObject.MyObject,RemoteObject" objectUri="RemoteObject.MyObject" mode="Singleton" /> </service> <channels> <channel ref="tcp" port="9999"/> </channels> </application> </system.runtime.remoting></configuration> 3、客户端:建立控制台项目:RemoteClientusing System; namespace RemoteClient{ class MyClient { [STAThread] static void Main(string[] args) { RemoteObject.MyObject app = (RemoteObject.MyObject)Activator.GetObject(typeof(RemoteObject.MyObject), System.Configuration.ConfigurationSettings.AppSettings["ServiceURL"]); // System.Configuration.AppSettingsReader["ServiceURL"]); //if (app == null) { int num = app.Add(12, 1); Console.WriteLine(num); Console.ReadLine(); } } }}建立配置文件:app.config<configuration> <appSettings> <add key="ServiceURL" value="tcp://localhost:9999/RemoteObject.MyObject"/> </appSettings></configuration>这个是我在网上看到的代码,为什么我在运行的时候“客户端”提示:
找不到服务
我是把客户端和服务器端分开写的,同时启动两个程序
找不到服务
我是把客户端和服务器端分开写的,同时启动两个程序
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货