.net Remoting的一个具体实现例子

这里.net remoting 的想实现一个银行转帐的过程
把用户输入的金额从用户A转入到用户B

数据库:
使用SQL SERVER里面的那个Pubs数据库,在这个数据库里面创建一个数据表叫Budget
只包含两个字段,Name、Money
CREATE TABLE Budget
(
   NAME         VARCHAR(8)      NOT NULL, 
   MONEY      FLOAT
)
GO

服务器端:
只需要创建一个普通的Win32窗体即可

.net Remoting的一个具体实现例子

用于启动服务器的服务
这段代码只是在宿主服务器上启动一个Remoting服务端口或者是在Tcp或者是在Http上

.net Remoting的一个具体实现例子        private void bttnStart_Click(object sender, System.EventArgs e)


下面这段代码就是Remoting服务的最重要的部分,具体实现都是在这里实施
其中“TransMoney”是一个存储过程,它做了把钱从A的帐户转到B的实际操作

.net Remoting的一个具体实现例子        public class TransferMoney:MarshalByRefObject,ITransfer

这就是那个存储过程,考过去就可以用了
.net Remoting的一个具体实现例子CREATE Proc TransMoney(
.net Remoting的一个具体实现例子@Money float,
.net Remoting的一个具体实现例子@FromUser varchar(8),
.net Remoting的一个具体实现例子@ToUser varchar(8)
.net Remoting的一个具体实现例子)
.net Remoting的一个具体实现例子as
.net Remoting的一个具体实现例子begin
.net Remoting的一个具体实现例子  begin tran
.net Remoting的一个具体实现例子  update budget set Money=Money-@Money where Name =@FromUser 
.net Remoting的一个具体实现例子  if @@Error
<>0
.net Remoting的一个具体实现例子  begin
.net Remoting的一个具体实现例子    rollback tran
.net Remoting的一个具体实现例子    RaisError ('妈的给钱时银行柜员机出错了!:( ', 16, 1) with Log  --记录日志
.net Remoting的一个具体实现例子    return -1
.net Remoting的一个具体实现例子  end
.net Remoting的一个具体实现例子  update budget set Money=Money+@Money where Name =@ToUser 
.net Remoting的一个具体实现例子  if @@Error
<>0
.net Remoting的一个具体实现例子  begin
.net Remoting的一个具体实现例子    rollback tran
.net Remoting的一个具体实现例子    RaisError ('妈的收钱时银行柜员机出错了!:( ', 16, 1) with Log  --记录日志
.net Remoting的一个具体实现例子    return -1
.net Remoting的一个具体实现例子  end
.net Remoting的一个具体实现例子  commit tran
.net Remoting的一个具体实现例子  return 1  
.net Remoting的一个具体实现例子end
.net Remoting的一个具体实现例子

这里还要注意一个类“databaseAccess”需要加入到项目中去,代码如下:

.net Remoting的一个具体实现例子using System;
.net Remoting的一个具体实现例子using System.Data.SqlClient;
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子namespace RemotingConsole
.net Remoting的一个具体实现例子{
.net Remoting的一个具体实现例子 /// 
<summary>
.net Remoting的一个具体实现例子 ///   The databaseAccess sets up the connection to the data repository.
.net Remoting的一个具体实现例子 /// 
</summary>
.net Remoting的一个具体实现例子  /// 
<remarks>
.net Remoting的一个具体实现例子  ///   Author: Ansel Du; arkstars@hotmail.com; Mar. 2005
.net Remoting的一个具体实现例子  /// 
</remarks>
.net Remoting的一个具体实现例子 public class databaseAccess 
.net Remoting的一个具体实现例子 {
.net Remoting的一个具体实现例子 
.net Remoting的一个具体实现例子       private static string sqlConnectionString = "server=61.129.112.20;database=Pubs;User ID=sa;Password=";
.net Remoting的一个具体实现例子       private SqlConnection sqlConnection;
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子     /// 
<summary>
.net Remoting的一个具体实现例子     ///   Public class constructor.
.net Remoting的一个具体实现例子     /// 
</summary>
.net Remoting的一个具体实现例子     protected internal databaseAccess() 
.net Remoting的一个具体实现例子  {
.net Remoting的一个具体实现例子   sqlConnection = new SqlConnection(databaseAccess.sqlConnectionString);
.net Remoting的一个具体实现例子  }
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子     /// 
<summary>
.net Remoting的一个具体实现例子  ///   The getConnection method sets up the database connection
.net Remoting的一个具体实现例子     /// 
</summary>
.net Remoting的一个具体实现例子  /// 
<returns>the (closed) SQL connection object</returns>
.net Remoting的一个具体实现例子  protected internal SqlConnection getConnection() //组合体内子类也可见
.net Remoting的一个具体实现例子  {
.net Remoting的一个具体实现例子      return sqlConnection;
.net Remoting的一个具体实现例子  }
.net Remoting的一个具体实现例子 }
.net Remoting的一个具体实现例子}
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子



但是大家可能注意到了在Remoting的服务那一段代码里面有一个接口"ITransfer"
public class TransferMoney:MarshalByRefObject,ITransfer
...
对,我为了让客户端容易而且安全地调用宿主服务器的服务,故意设了一个接口,这个接口需要你单独在创建一个项目来完成它:

.net Remoting的一个具体实现例子using System;
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子namespace AssemblyComponent
.net Remoting的一个具体实现例子{
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子    /// 
<summary>
.net Remoting的一个具体实现例子    /// 定义一个接口
.net Remoting的一个具体实现例子    /// 
</summary>
.net Remoting的一个具体实现例子    public interface ITransfer 
.net Remoting的一个具体实现例子    {
.net Remoting的一个具体实现例子        string getTransferResult(double money);
.net Remoting的一个具体实现例子    }
.net Remoting的一个具体实现例子}

这个接口你创建了以后,生成一个Assembly(组合体)的DLL形式的东东
这个东东首先需要Remoting进行引用,而且服务器需要实现这个接口,具体实现上面的代码已经写了,你可以对照着看看就明白了。相信大家一定比我聪明!
 
然后生成,服务器端的服务这就完成了,这时候生成了一个Exe的组合文件,用户只需要那个Assembly文件和这个可执行的组合文件。那么就可以启动一个服务器了!
Ok!下面我们编写一个客户端来调用这个服务器的服务。
.net Remoting的一个具体实现例子
这个就是客户端的界面,很简单吧?这时候用户只需输入宿主服务器的IP和要转帐的金额数,选择一个传输通道TCP通道或者HTTP通道即可完成操作。

.net Remoting的一个具体实现例子using System;
.net Remoting的一个具体实现例子using System.Drawing;
.net Remoting的一个具体实现例子using System.Collections;
.net Remoting的一个具体实现例子using System.ComponentModel;
.net Remoting的一个具体实现例子using System.Windows.Forms;
.net Remoting的一个具体实现例子using System.Data;
.net Remoting的一个具体实现例子using System.Runtime.Remoting;
.net Remoting的一个具体实现例子using System.Runtime.Remoting.Channels;
.net Remoting的一个具体实现例子using System.Runtime.Remoting.Channels.Http;
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子using AssemblyComponent;
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子namespace remotingClient
.net Remoting的一个具体实现例子{

            .......
.net Remoting的一个具体实现例子        private void button1_Click(object sender, System.EventArgs e)
.net Remoting的一个具体实现例子        {
.net Remoting的一个具体实现例子            string msg;
.net Remoting的一个具体实现例子            try
.net Remoting的一个具体实现例子            {
.net Remoting的一个具体实现例子                double atemp=Convert.ToDouble(tBMoney.Text);
.net Remoting的一个具体实现例子            }
.net Remoting的一个具体实现例子            catch(Exception exx)
.net Remoting的一个具体实现例子            {
.net Remoting的一个具体实现例子                msg="输入的并非数字类型-〉 "+ exx.Message;
.net Remoting的一个具体实现例子            }
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子            try
.net Remoting的一个具体实现例子            {.net Remoting的一个具体实现例子                                
.net Remoting的一个具体实现例子                //通过Activator.GetObject得到代理类的实例
.net Remoting的一个具体实现例子                if(rbtHttp.Checked==true)//Http信道
.net Remoting的一个具体实现例子                {
.net Remoting的一个具体实现例子                    ITransfer ProxyObj=(ITransfer) Activator.GetObject(typeof(ITransfer),"http://"+tbHostServer.Text+":7608/TranserRemotingService/URLAssemblyComponent");
.net Remoting的一个具体实现例子                    double atemp=Convert.ToDouble(tBMoney.Text);
.net Remoting的一个具体实现例子                    msg=ProxyObj.getTransferResult(Convert.ToDouble(atemp));
.net Remoting的一个具体实现例子                }
.net Remoting的一个具体实现例子                else//Tcp信道
.net Remoting的一个具体实现例子                {
.net Remoting的一个具体实现例子                    ITransfer ProxyObj=(ITransfer) Activator.GetObject(typeof(ITransfer),"tcp://"+tbHostServer.Text+":7711/TranserRemotingService/URLAssemblyComponent");
.net Remoting的一个具体实现例子                    double atemp=Convert.ToDouble(tBMoney.Text);
.net Remoting的一个具体实现例子                    msg=ProxyObj.getTransferResult(Convert.ToDouble(atemp));
.net Remoting的一个具体实现例子                }
.net Remoting的一个具体实现例子                //msg=ProxyObj.a(2);
.net Remoting的一个具体实现例子                
.net Remoting的一个具体实现例子            }
.net Remoting的一个具体实现例子            catch(Exception Ex)
.net Remoting的一个具体实现例子            {
.net Remoting的一个具体实现例子                msg="转帐失败-〉 "+ Ex.Message;                
.net Remoting的一个具体实现例子            }
.net Remoting的一个具体实现例子            MessageBox.Show(msg);            
.net Remoting的一个具体实现例子        }
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子        private void Form1_Load(object sender, System.EventArgs e)
.net Remoting的一个具体实现例子        {
.net Remoting的一个具体实现例子            rbtTcp.Checked=true;
.net Remoting的一个具体实现例子            rbtHttp.Checked=false;
.net Remoting的一个具体实现例子            
.net Remoting的一个具体实现例子        }
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子        private void button2_Click(object sender, System.EventArgs e)
.net Remoting的一个具体实现例子        {
.net Remoting的一个具体实现例子          Close();
.net Remoting的一个具体实现例子        }
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子        private void rbtTcp_CheckedChanged(object sender, System.EventArgs e)
.net Remoting的一个具体实现例子        {
.net Remoting的一个具体实现例子            rbtHttp.Checked=!rbtTcp.Checked;                        
.net Remoting的一个具体实现例子        }
.net Remoting的一个具体实现例子
.net Remoting的一个具体实现例子        private void rbtHttp_CheckedChanged(object sender, System.EventArgs e)
.net Remoting的一个具体实现例子        {
.net Remoting的一个具体实现例子            rbtTcp.Checked=!rbtHttp.Checked;    
.net Remoting的一个具体实现例子        }
.net Remoting的一个具体实现例子    }
.net Remoting的一个具体实现例子}
.net Remoting的一个具体实现例子

Ok,好像不缺少什么东西了,慢慢看吧,是不是很简单哦

相关文章:

  • 2021-07-30
  • 2022-12-23
  • 2021-11-23
  • 2021-10-27
  • 2021-06-25
  • 2022-02-07
  • 2022-12-23
猜你喜欢
  • 2021-09-26
  • 2022-12-23
  • 2022-12-23
  • 2021-12-17
  • 2021-10-16
  • 2022-12-23
相关资源
相似解决方案