Remote小Demo

时间:2023-12-05 10:35:14

Demo基于http://www.cnblogs.com/zhili/p/NETRemoting.html

Remote小Demo

RemotingObj

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Metadata;
using System.Text;
using System.Threading.Tasks; namespace RemotingServer
{
public class RemotingObject:MarshalByRefObject
{
public int TestTcpAdd(int first,int second)
{
return first + second;
} public int TestHttpMinus(int first,int second)
{
return first - second;
} [SoapMethod(XmlNamespace = "RemotingServer", SoapAction = "RemotingServer#TestMultiIpc")]
public int TestMultiIpc(int first, int second)
{
return first*second;
}
}
}

  

RemotingServer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting.Channels.Ipc;
using System.Runtime.Remoting.Channels.Tcp;
using System.Text;
using System.Threading.Tasks; namespace RemotingServer
{
class Program
{
static void Main(string[] args)
{
TcpChannel tcpChannel = new TcpChannel(2046);
HttpChannel httpChannel = new HttpChannel(9001);
IpcChannel ipcChannel = new IpcChannel("IpcTest"); ChannelServices.RegisterChannel(tcpChannel,false);
ChannelServices.RegisterChannel(httpChannel,false);
ChannelServices.RegisterChannel(ipcChannel,false); Console.WriteLine("The name of the Tcp Channel:{0}",tcpChannel.ChannelName);
Console.WriteLine("The priority of the Tcp Channel:{0}",tcpChannel.ChannelPriority); Console.WriteLine("The name of the Http Channel:{0}",httpChannel.ChannelName);
Console.WriteLine("The priority of the Http Channel:{0}",httpChannel.ChannelPriority); Console.WriteLine("The name of IPC Channel :{0}",ipcChannel.ChannelName);
Console.WriteLine("The priority of Ipc Channel:{0}",ipcChannel.ChannelPriority); RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemotingServer.RemotingObject), "RemotingObject",WellKnownObjectMode.Singleton);
Console.ReadKey();
}
}
}

  

RemotingClinet

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RemotingServer; namespace RemotingClient
{
class Program
{
static void Main(string[] args)
{
RemotingObject proxyTcp =
Activator.GetObject(typeof (RemotingObject), "tcp://localhost:2046/RemotingObject") as RemotingObject;
if (proxyTcp == null)
{
Console.WriteLine("连接Tcp服务失败!");
return; } RemotingObject proxyHttp =
Activator.GetObject(typeof(RemotingObject), "http://localhost:9001/RemotingObject") as RemotingObject;
if (proxyHttp==null)
{
Console.WriteLine("连接Http服务失败!");
return;
} RemotingObject proxyIpc =
Activator.GetObject(typeof(RemotingObject), "ipc://IpcTest/RemotingObject") as RemotingObject;
if (proxyIpc == null)
{
Console.WriteLine("连接IPC失败!");
return;
} // 输出信息
Console.WriteLine("This call object by TcpChannel, 100 + 200 = {0}", proxyTcp.TestTcpAdd(100, 200));
Console.WriteLine("This call object by HttpChannel, 100 - 200 = {0}", proxyHttp.TestHttpMinus(100, 200));
Console.WriteLine("This call object by IpcChannel, 100 * 200 = {0}", proxyIpc.TestMultiIpc(100, 200));
Console.WriteLine("Press any key to exit!");
Console.ReadLine();
}
}
}

  

备注:

Remoting在HttpChannel下抛“指定的 SOAPAction 无效

解决方法:

1:将Server和Client所在的程序集修改成相同的程序集名。

http://blog.csdn.net/lilin8905/article/details/6232640

2:在需要使用httpChannel的方法上面加上SoapMethod标签

[SoapMethod(XmlNamespace = "命名空间", SoapAction = "命名空间#方法名")]

http://blog.csdn.net/sloder/article/details/8694560

Xmind

练习代码