简单到无法简单的.Net Remoting 程序了~
为什么运行client程序,得到的结果是Client.EXE, 而不是Server.EXE远程的类型定义RemotableType.cs:
using System;
using System.Diagnostics;
public class RemotableType : MarshalByRefObject
{
private string _internalString = "This is the RemotableType.";
public string StringMethod(){
Console.WriteLine(Process.GetCurrentProcess().ProcessName); return _internalString;
}
}服务器端Listener.cs:
using System;
using System.Runtime.Remoting;public class Listener{
public static void Main(){
RemotingConfiguration.Configure("listener.xml");
Console.WriteLine("Listening for requests. Press Enter to exit...");
Console.ReadLine();
}
}服务器端listener.xml:
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown
mode="Singleton"
type="RemotableType, RemotableType"
objectUri="RemotableType.rem"
/>
</service>
<channels>
<channel ref="http" port="8989"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>客户端Client.cs:
using System;
using System.Runtime.Remoting;public class Client{ public static void Main(){
RemotingConfiguration.Configure("client.xml");
RemotableType remoteObject = new RemotableType();
//RemotableType remoteObject=(RemotableType)Activator.GetObject(typeof(RemotableType), "http://localhost:8989/RemotableType.rem");
Console.WriteLine(remoteObject.StringMethod());
Console.ReadLine();
}
}客户端client.xml:
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
type="RemotableType, RemotableType"
url="http://localhost:8989/RemotableType.rem"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
为什么运行client程序,得到的结果是Client.EXE, 而不是Server.EXE远程的类型定义RemotableType.cs:
using System;
using System.Diagnostics;
public class RemotableType : MarshalByRefObject
{
private string _internalString = "This is the RemotableType.";
public string StringMethod(){
Console.WriteLine(Process.GetCurrentProcess().ProcessName); return _internalString;
}
}服务器端Listener.cs:
using System;
using System.Runtime.Remoting;public class Listener{
public static void Main(){
RemotingConfiguration.Configure("listener.xml");
Console.WriteLine("Listening for requests. Press Enter to exit...");
Console.ReadLine();
}
}服务器端listener.xml:
<configuration>
<system.runtime.remoting>
<application>
<service>
<wellknown
mode="Singleton"
type="RemotableType, RemotableType"
objectUri="RemotableType.rem"
/>
</service>
<channels>
<channel ref="http" port="8989"/>
</channels>
</application>
</system.runtime.remoting>
</configuration>客户端Client.cs:
using System;
using System.Runtime.Remoting;public class Client{ public static void Main(){
RemotingConfiguration.Configure("client.xml");
RemotableType remoteObject = new RemotableType();
//RemotableType remoteObject=(RemotableType)Activator.GetObject(typeof(RemotableType), "http://localhost:8989/RemotableType.rem");
Console.WriteLine(remoteObject.StringMethod());
Console.ReadLine();
}
}客户端client.xml:
<configuration>
<system.runtime.remoting>
<application>
<client>
<wellknown
type="RemotableType, RemotableType"
url="http://localhost:8989/RemotableType.rem"
/>
</client>
</application>
</system.runtime.remoting>
</configuration>
应该是Server.EXE啊
http://www.cnblogs.com/WannyJong/articles/454833.html
而在client的时候,则是 RemoteType,Client这时候你的client 用RemotableType)Activator.GetObject(typeof(RemotableType), "http://localhost:8989/RemotableType.rem"),调用的时候,他通过Soap到服务端,对于StringMethod调用,会post一个类似的消息。
<SOAP-ENV:Body>
<StringMethod xmlns="http://schemas.microsoft.com/xxxxx/client" />
</SOAP-ENV:Body>
注意这个soap 的namespace,是client,而你的server.exe 并不会处理这个soapaction的请求。
应该会告诉你个非法soapaction的错误2.两个xml都有问题。您可以这样这样该一下,应该就可以了。
<wellknown
mode="Singleton"
type="RemotableType, RemotableType"
objectUri="RemotableType.rem"
/>
这里的type,注意是逗号格开的,前面是类型,后面是程序集的名字。你的程序集分别叫client和server,两者都不可能load成功。所以配置文件事事上没有起作用。
正确的做法:
1。 把 Listener.cs, Listener.xml, RemotableType.cs copy 到一个目录。然后修改一下你的Listener.xml 改为 type="RemotableType,Server“然后编译一下, Csc /out:server.exe *.cs 然后运行server.exe2。把Client.cs,client.xml,RemotableType.cs Copy 到另外一个目录。
更改client.xml 为 type="RemotableType,Server“
然后运行。 csc /out:server.exe *.cs 这时候也编译了一个server.exe 出来。然后运行,你就看到效果了。
然后分别对Listener和Client两个项目加这个引用. 然后写XML, 其实XML一点都没错~
错在我在bin/debug下建了xml, 然后把这两个xml"添加现有项"到项目里去~ 没想到啊没想到, xml不是bin/debug/目录下的那个, .net自己去新弄了个, 放在项目根目录下~ 不巧去看了看bin/debug/下的xml,发现怎么是空的....
汗~不过还是多谢楼上几位~ 太感谢了, 不耐其烦.结
以为你建立了两个项目。如果要想用两个项目体验remoting的话,就按照我说的就行。