.NET Remoting 應用實例

前言web

項目中運用到.NET Remoting ,前段時間也看了下.NET Remoting的相關資料,感受本身應該動手寫個實例來梳理下對.NET Remoting認識和理解,不足的地方請你們指正。app

簡單介紹,使用Visual Studio 2010 ,在.NET Framework 4.0框架下,開發的ASP.NET web 應用程序。使用IIS 7.5。框架

基本構思ide

 結合本身再項目中的運用,構建以下解決方案。測試

  • Buseniess:業務邏輯層
  • MyInterface:接口,相似WCF中的契約
  • NetRemotingWeb:表現層
  • RemotingClient:客戶端
  • RemotingServer:服務端

基本原理:spa

 

 

實現過程3d

 1.服務端代理

 在web.config 進行配置信道和屬性code

  <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指向MyServerorm

小白在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的能夠發私信給我。

相關文章
相關標籤/搜索