WindowsService+.Net Remoting 实现分布式应用系统

  一直以来,公司做分布系统都是采用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();
    }
}

最后说明一点:客户端采用引用接口,而不是直接引用远端对象,是考虑到低耦合的需要:这样我们在服务端改动方法的实现,客户端不能察觉,也不用更新引用。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值