• Remote小Demo


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

    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 

    练习代码

  • 相关阅读:
    GISer 应届生找工作历程(完结)
    c#跨窗体调用操作
    c#基础学习笔记-----------委托事件
    c#基础笔记-----------集合
    ArcEngine开发鹰眼实现问题
    Null Object模式
    c#基础------------静态类与非静态类
    GIS初学者
    c#基础学习汇总----------base和this,new和virtual
    用Python编写水仙花数
  • 原文地址:https://www.cnblogs.com/Yang2012/p/Remoting.html
Copyright © 2020-2023  润新知