WindowsService+.Net Remoting 實現分布式應用系統

來源:互聯網
上載者:User

  一直以來,公司做分布系統都是採用Web Service實現的(可能考慮到簡單,易操作吧)。但是我們基本上都是內部使用的系統,考慮到執行效能,是否應該考慮採用.Net Remoting 解決方案哪~
  Web Service的優勢在於採用Http協議,可以穿透防火牆。而且其採用XML資訊傳輸,採用Soap的方式實現了平台無關性。這一點Remoting是不能比擬的。
  .net remoting從名字上就可以看出,這個解決方案只能實施在.Net平台之上。但是它也有自己的優點:可以通過Tcp/Http/Icp協議進行資訊傳輸.這樣的話,如果是在一個區域網路內進行分布式編程,採用TCP協議將大大提高執行效率。
   本例簡單的說明如果利用TCP協議,架構.net Remoting 分布式應用系統.例子中將採用:
   1. windows Service 實施Remoting 服務。
   2. 採用介面實現程式的低耦合.

另外,如果要實現Remoting,需要三部分準備工作:
   1)遠端對象,也就是真正的服務內容。這裡採用資料訪問服務為例
   2)服務端:承載遠端對象(發布遠端對象)。這裡採用windows service 來發布遠端對象.
   3)用戶端:也就是我們的應用程式,在這裡調用服務端發布的遠端對象.
看代碼,看的更清楚:
1)建立遠端對象(可以建立一個類庫檔案)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OracleClient;  //注意首先添加此引用才行

namespace DAL
{
    public class MyDal : MarshalByRefObject,DAL.IMyDal  //注意這裡
    {
        private string ConnString;
        public MyDal()
        {
            ConnString = "Data Source=oratest;uid=test;password=test";
        }

        public DataSet getDs(string sql)
        {
            using (OracleConnection conn = new OracleConnection(ConnString))
            {
                try
                {
                    conn.Open();
                    OracleDataAdapter ada = new OracleDataAdapter(sql, conn);
                    DataSet ds = new DataSet();
                    ada.Fill(ds);
                    return ds;
                }
                catch(Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
        }

        public string getStr()
        {
            return "this is something.";
        }
    }
}
你可能注意到這個class繼承MarshalByRefObject(允許此類被遠端訪問)和實現介面 DAL.IMyDal(注意添加對IMyDal的引用)
所以,我們這裡所有的方法都必須在IMyDal中聲明。

using System;namespace DAL{   public interface IMyDal    {        System.Data.DataSet getDs(string sql);        string getStr();    }}

2)建立windows servive,發布此遠端對象(建立windows service解決方案即可),注意對DAL,iDAL的引用
在Service1的OnStart方法中:
protected override void OnStart(string[] args)
        {
            // TODO: 在此加入啟動服務的程式碼。

            TcpChannel tcp = new TcpChannel(9999);

            ChannelServices.RegisterChannel(tcp, false);

            //註冊知名對象
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(DAL.MyDal),
                "myDAL", WellKnownObjectMode.SingleCall);

        }

注意,建立完了之後,就可以安裝並啟動此服務,並設定服務為自動啟動,原因不必多說.
3) 用戶端實現(建立網站RemotingTest),注意添加對介面的引用
HTML:
建立
<div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
用來測試使用
CS:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // 從遠端地址建立遠端對象
       DAL.IMyDal cDal = (DAL.IMyDal)Activator.GetObject(typeof(DAL.IMyDal),
            "tcp://localhost:9999/myDAL");

        //調用遠端對象的方法
       TextBox1.Text = cDal.getStr();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        // 從遠端地址建立遠端對象
        DAL.IMyDal cDal = (DAL.IMyDal)Activator.GetObject(typeof(DAL.IMyDal),
             "tcp://localhost:9999/myDAL");

        string sql = "select * from ivan_test";
        DataSet ds = cDal.getDs(sql);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
}

最後說明一點:用戶端採用引用介面,而不是直接引用遠端對象,是考慮到低耦合的需要:這樣我們在服務端改動方法的實現,用戶端不能察覺,也不用更新引用。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.