A specific implementation example of. Net remoting

Source: Internet
Author: User

A specific implementation example of. Net remoting

Here. Net remoting wants to implement a bank transfer process
Transfers the amount entered by the user from user a to user B

Database:
Use the pubs database in SQL Server to create a data table named budget
It only contains two fields: Name and money.
Create Table budget
(
Name varchar (8) Not null,
Money float
)
Go

Server:
You only need to create a common Win32 form.

The service used to start the server
This sectionCodeOnly start a remoting service port on the host server or on TCP or HTTP

Private   Void Bttnstart_click ( Object Sender, system. eventargs E)
{
Channelservices. registerchannel ( New Httpchannel ( 7608 ));
Channelservices. registerchannel ( New Tcpchannel ( 7711 ));

Remotingconfiguration. applicationname = " Transerremotingservice " ;
// If the host Program is registered on IIS, The applicationname is equivalent to the virtual directory on IIS.
Wellknownservicetypeentry wkste =   New Wellknownservicetypeentry ( Typeof (Transfermoney ), " Urlassemblycomponent " , Wellknownobjectmode. Singleton );
Remotingconfiguration. registerwellknownservicetype (wkste );
Bttnstart. Enabled = False ;
}

Private   Void Button2_click ( Object Sender, system. eventargs E)
{
Close ();
}

The following code is the most important part of the remoting service.
Transmoney is a stored procedure, which transfers money from account A to account B.

Public   Class Transfermoney: transferalbyrefobject, itransfer
{
Public   String Gettransferresult ( Double Money)
{
Try
{
Transfermoneyfromatob (money );
Return "Transfer money succeed!";
}
Catch (Exception ex)
{
Return "Transfer money failed!"+Ex. message;
}
}
Private   Static   Void Transfermoneyfromatob ( Double Money)
{
Sqlcommand sqlcom = New Sqlcommand ( " Exec transmoney " + Money. tostring () + " , 'A', 'B' " );
Databaseaccess OBJ = New Databaseaccess ();
Sqlcom. Connection = OBJ. getconnection ();
Sqlcom. Connection. open ();
Sqlcom. executenonquery ();
Sqlcom. Connection. Close ();
// Throw new exception ("operation failed when transfer money into! ");
}

}

This is the stored procedure, which can be used in the past. Create proc transmoney (
@ Money float,
@ Fromuser varchar (8 ),
@ Touser varchar (8)
)
As
Begin
Begin tran
Update budget set money = money-@ money where name = @ fromuser
If @ Error <> 0
Begin
Rollback tran
Raiserror ('the bank teller machine went wrong when I gave the money! :( ', 16, 1) with log -- Record log
Return-1
End
Update budget set money = money + @ money where name = @ touser
If @ Error <> 0
Begin
Rollback tran
Raiserror (a bank teller machine error occurred while collecting money! :( ', 16, 1) with log -- Record log
Return-1
End
Commit tran
Return 1
End

Note that the "databaseaccess" class must be added to the project. The Code is as follows:

Using system;
Using system. Data. sqlclient;

Namespace remotingconsole
{
/// < Summary >
/// The databaseaccess sets up the connection to the data repository.
/// </ Summary >
/// < Remarks >
/// Author: Ansel du; arkstars@hotmail.com; Mar. 2005
/// </ Remarks >
Public class databaseaccess
{
 
Private Static string sqlconnectionstring = "Server = 61.129.112.20; database = pubs; user id = sa; Password = ";
Private sqlconnection;

/// < Summary >
/// Public class constructor.
/// </ Summary >
Protected internal databaseaccess ()
{
Sqlconnection = new sqlconnection (databaseaccess. sqlconnectionstring );
}

/// < Summary >
/// The getconnection method sets up the database connection
/// </ Summary >
/// < Returns > The (closed) SQL connection object </ Returns >
Protected internal sqlconnection getconnection () // The subclass of the group body is also visible.
{
Return sqlconnection;
}
}
}

However, you may notice that the remoting Service Code contains an interface "itransfer"
Public ClassTransfermoney: transferalbyrefobject, itransfer
...
Yes, I intentionally set up an interface to allow the client to easily and securely call the services of the host server. This interface requires you to create a project separately to complete it:

Using system;


Namespace assemblycomponent
{

/// < Summary >
/// Define an Interface
/// </ Summary >
Public interface itransfer
{
String gettransferresult (double money );
}
}

After you have created this interface, you can generate a DLL-type plug-in the form of assembly (assembly ).
In this case, remoting is required for reference first, and the server needs to implement this interface. The specific implementation code above has been written. You can check it and see it. I believe everyone will be smarter than me!
 
Then generate the service on the server. At this time, an EXE combination file is generated. You only need the Assembly file and the executable combination file. Then you can start a server!
OK! Next we will write a client to call the service of this server.

This is the client interface, right? At this time, you only need to enter the IP address of the host server and the amount of funds to transfer, select a transmission channel TCP channel or HTTP channel to complete the operation.

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. remoting;
Using system. runtime. remoting. channels;
Using system. runtime. remoting. channels. HTTP;

Using assemblycomponent;

Namespace remotingclient
{

.......
Private void button#click (Object sender, system. eventargs E)
{
String MSG;
Try
{
Double atemp = convert. todouble (tbmoney. Text );
}
Catch (exception Exx)
{
MSG = "the input is not a numeric type->" + Exx. message;
}

Try
{
// Obtain the proxy class instance through activator. GetObject
If (rbthttp. Checked = true) // HTTP Channel
{
Itransfer proxyobj = (itransfer) activator. GetObject (typeof (itransfer), "http: //" + tbhostserver. Text + ": 7608/transerremotingservice/urlassemblycomponent ");
Double atemp = convert. todouble (tbmoney. Text );
MSG = proxyobj. gettransferresult (convert. todouble (atemp ));
}
Else // TCP Channel
{
Itransfer proxyobj = (itransfer) activator. GetObject (typeof (itransfer), "TCP: //" + tbhostserver. Text + ": 7711/transerremotingservice/urlassemblycomponent ");
Double atemp = convert. todouble (tbmoney. Text );
MSG = proxyobj. gettransferresult (convert. todouble (atemp ));
}
// MSG = proxyobj. A (2 );

}
Catch (exception ex)
{
MSG = "Transfer failed->" + ex. message;
}
MessageBox. Show (MSG );
}

Private void form1_load (Object sender, system. eventargs E)
{
Rbttcp. Checked = true;
Rbthttp. Checked = false;

}

Private void button2_click (Object sender, system. eventargs E)
{
Close ();
}

Private void rbttcp_checkedchanged (Object sender, system. eventargs E)
{
Rbthttp. Checked =! Rbttcp. checked;
}

Private void rbthttp_checkedchanged (Object sender, system. eventargs E)
{
Rbttcp. Checked =! Rbthttp. checked;
}
}
}

OK. It seems that nothing is missing. Let's take a look. Isn't it easy?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.