Remote小Demo
2015-05-21 21:00 Yang-Onion 阅读(217) 评论(0) 编辑 收藏 举报Demo基于http://www.cnblogs.com/zhili/p/NETRemoting.html
RemotingObj
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ASP.NET Core 模型验证消息的本地化新姿势
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· 官方的 MCP C# SDK:csharp-sdk
· 一款 .NET 开源、功能强大的远程连接管理工具,支持 RDP、VNC、SSH 等多种主流协议!
· 提示词工程师自白:我如何用一个技巧解放自己的生产力
· 一文搞懂MCP协议与Function Call的区别
· 如何不购买域名在云服务器上搭建HTTPS服务