.NET Remoting 应用实例

前言

项目中运用到.NET Remoting ,前段时间也看了下.NET Remoting的相关资料,感觉自己应该动手写个实例来梳理下对.NET Remoting认识和理解,不足的地方请大家指正。

简单介绍,使用Visual Studio 2010 ,在.NET Framework 4.0框架下,开发的ASP.NET web 应用程序。使用IIS 7.5。

基本构思

 结合自己再项目中的运用,构建如下解决方案。

  • Buseniess:业务逻辑层
  • MyInterface:接口,类似WCF中的契约
  • NetRemotingWeb:表现层
  • RemotingClient:客户端
  • RemotingServer:服务端

基本原理:

 

实现过程

 1.服务端

 在web.config 进行配置信道和属性

  <system.runtime.remoting>
    <application>
      <service>
        <wellknown mode="Singleton" type="RemotingServer.MyServer, RemotingServer" objectUri="MyServer.rem" />
      </service>
      <channels>
        <channel ref="http">
          <serverProviders>
            <formatter ref="binary" typeFilterLevel="Full" />
          </serverProviders>
          <clientProviders>
            <formatter ref="binary" />
          </clientProviders>
        </channel>
      </channels>
    </application>
    <customErrors mode="off" />
  </system.runtime.remoting>

objectUri指向MyServer

小白在MyServer里就实现一个简单的方法

    public class MyServer : MarshalByRefObject,IMyInterface
    {
        public string sayHello(string name)
        {
            return "你好:" + name;
        }
    }

要实现远程调用,必须继承MarshalByRefObject,同时要暴露一个接口在IMyInterface中。

2.客户端

需要引用下面的命名空间

using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Lifetime;
using System.Collections.Specialized;

服务端地址

        const string OBJECT_URL = "MyServer.rem";
        const string REMOTING_URL = "http://127.0.0.1:8039/";

这里其实可以在config文件中配置,这里小白就直接写在程序里了。

定义信道和实例化代理

            if (ChannelServices.GetChannel("DataProClient") == null)
            {
                ListDictionary channelProperties = new ListDictionary();
                channelProperties.Add("port", 0);
                channelProperties.Add("name", "DataProClient");
                channelProperties.Add("timeout", -1);
                channelProperties.Add("proxyName", "");

                BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
                provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
                HttpChannel channel = new HttpChannel(channelProperties,
                    new BinaryClientFormatterSinkProvider(),
                    provider
                    );

                ChannelServices.RegisterChannel(channel, false);
            }
            client_server = (MyInterface.IMyInterface)RemotingServices.Connect(typeof(MyInterface.IMyInterface), strUri);

strUri=REMOTING_URL+OBJECT_URL;

这里,检查下信道是否为空很有必要,不判断可能造成“该通道已被占用”导致信道创建不成功。

调用服务端的方法

        public string say(string name)
        {
            string word = client_server.sayHello(name);
            return word;
        }

3.业务逻辑层

实例化客户端调用其方法

        public string sayHello(string name)
        {
            Client client = new Client();
            string s = client.say(name);
            return s;
        }

4.web 应用程序中调用业务逻辑层的方法

服务端寄宿到IIS中

1. 服务端发布

2.IIs中新建站点

在w3wp进程中就可以找到remoting寄宿的进程。

运行

 

好了,测试通过。

欢迎拍砖。需要demo的可以发私信给我。

原文地址:https://www.cnblogs.com/WangJinYang/p/3501535.html