作了一个最简单的.net Remoting的例子:1、远程对象:
建立类库项目:RemoteObject 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"]);
Console.WriteLine(app.Add(1,2));
Console.ReadLine();
}
}
}建立配置文件:app.config
<configuration>
<appSettings>
<add key="ServiceURL" value="tcp://localhost:9999/RemoteObject.MyObject"/>
</appSettings>
</configuration>4、测试
在最后编译的时候会发现编译报错:
1、找不到app.Add()
2、找不到RemoteObject
这是因为客户端RemoteClient没有添加RemoteObject的引用,编译器并不知道远程对象存在哪些成员所以报错,添加引用以后vs.net会在客户端也保存一个dll,可能大家会问这样如果对远程对象的修改是不是会很麻烦?其实不麻烦,对项目编译一次vs.net会重新复制dll。
然后直接运行客户端会出现“目标主机拒绝”的异常,也说明了通道没有打开
运行服务端再运行客户端出现“找不到程序集RemoteObject”!回头想想可以发现我们并在服务端对RemoteObject添加引用,编译的时候通过是因为这个时候并没有用到远程对象,大家可能不理解运行服务端的时候也通过?这是因为没有这个时候还没有激活远程对象。理所当然,对服务端要添加引用远程对象,毕竟我们的对象是要靠远程承载的。
现在先运行RemoteServer,然后运行RemoteClient,结果在RemoteClient中出现如下的错误提示:
---------------------------未处理的“System.IO.FileNotFoundException”类型的异常出现在 mscorlib.dll 中。其他信息: 找不到文件或程序集名称“RemoteObject”,或找不到它的一个依赖项。-------------------------------------对应的代码是: Console.WriteLine(app.Add(1,2));为什么?请高手指教
建立类库项目:RemoteObject 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"]);
Console.WriteLine(app.Add(1,2));
Console.ReadLine();
}
}
}建立配置文件:app.config
<configuration>
<appSettings>
<add key="ServiceURL" value="tcp://localhost:9999/RemoteObject.MyObject"/>
</appSettings>
</configuration>4、测试
在最后编译的时候会发现编译报错:
1、找不到app.Add()
2、找不到RemoteObject
这是因为客户端RemoteClient没有添加RemoteObject的引用,编译器并不知道远程对象存在哪些成员所以报错,添加引用以后vs.net会在客户端也保存一个dll,可能大家会问这样如果对远程对象的修改是不是会很麻烦?其实不麻烦,对项目编译一次vs.net会重新复制dll。
然后直接运行客户端会出现“目标主机拒绝”的异常,也说明了通道没有打开
运行服务端再运行客户端出现“找不到程序集RemoteObject”!回头想想可以发现我们并在服务端对RemoteObject添加引用,编译的时候通过是因为这个时候并没有用到远程对象,大家可能不理解运行服务端的时候也通过?这是因为没有这个时候还没有激活远程对象。理所当然,对服务端要添加引用远程对象,毕竟我们的对象是要靠远程承载的。
现在先运行RemoteServer,然后运行RemoteClient,结果在RemoteClient中出现如下的错误提示:
---------------------------未处理的“System.IO.FileNotFoundException”类型的异常出现在 mscorlib.dll 中。其他信息: 找不到文件或程序集名称“RemoteObject”,或找不到它的一个依赖项。-------------------------------------对应的代码是: Console.WriteLine(app.Add(1,2));为什么?请高手指教
解决方案 »
- 找不到服务器
- 谈谈你做过的项目?
- 点按钮,弹出一个窗口(window.open())?
- 如何更改 .NET Framework 版本,我现在的版本是1.1.4322.573如何改成1.1.4322.2300
- 关于数据访问层(DA)的效率问题,UP 有分!
- 跪求最好的解决方案
- 导出导入excel,求高手指点,在不做完就要被抄啦,请高手帮忙
- 请问一个很简单的问题,做了一个查询功能,如果没有查到记录,怎样做才能提示:没有满足条件的记录!的这样的信息呢?很菜的问题!
- xml的问题(当场给100分,在线等待)
- 使用Server.Tansfer的问题
- ASP.NET 未被授权访问所请求的资源 问题
- 跪求,请教高手了,急急急急急急!
{
class MyServer
{
[STAThread]
static void Main(string[] args)
{
RemotingConfiguration.Configure("RemoteServer.exe.config");//你这里面调用的配置文件是RemoteServer.exe.config,而你下面的配置文件名字是:app.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>
我用了你的方法,把RemoteServer.exe.config改成app.config可是出现如下的错误:-------------------------
未处理的“System.Runtime.Remoting.RemotingException”类型的异常出现在 mscorlib.dll 中。其他信息: 因发生异常 System.IO.FileNotFoundException: 系统找不到指定的文件。
at System.IConfigHelper.Run(IConfigHandler factory, String fileName)
at System.ConfigTreeParser.Parse(String fileName, String configPath) 而无法成功读取 .Config 文件 App.config。---------------------------------没改之前还没有抱错,这是为什么啊
RemotingConfiguration.Configure("RemoteServer.exe.config");
是对的
为什么会抱错那?