Windowsservice +. Net remoting for Distributed Application System

Source: Internet
Author: User
Tags oracleconnection

For a long time, the company has implemented distribution systems using Web Services (simple and easy to operate ). However, we basically use internal systems. Considering the execution efficiency, should we consider using the. NET remoting solution ~
The advantage of web service is that the HTTP protocol can penetrate the firewall. In addition, XML Information Transmission and soap are used to achieve platform independence. Remoting is incomparable.
As you can see from the name of. Net remoting, this solution can only be implemented on the. NET platform. However, it also has its own advantages: it can transmit information through TCP/HTTP/ICP Protocol. in this case, if a LAN is used for distributed programming, using the TCP protocol will greatly improve the execution efficiency.
In this example, if the TCP protocol is used, the. NET remoting distributed application system will be used:
1. Implement the remoting service for Windows service.
2. Use interfaces to implement low coupling of programs.

In addition, to implement remoting, three preparations are required:
1) the remote object, that is, the real service content. The Data Access Service is used as an example.
2) server: host a remote object (publish a remote object ). Windows Service is used to publish remote objects.
3) client: the remote object released by the server is called here.
Read the Code to make it clearer:
1) create a remote object (you can create a new class library file)
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. Data;
Using system. Data. oracleclient; // note that this reference must be added first.

Namespace dal
{
Public class mydal: marshalbyrefobject, Dal. imydal // note that
{
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 .";
}
}
}
You may notice that this class inherits marshalbyrefobject (which allows remote access) and implements the interface Dal. imydal (note that you can add references to imydal)
Therefore, all our methods here must be declared in imydal.

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

2) create windows servive and publish this remote object (create a new Windows service solution). Pay attention to the reference to Dal and idal.
In the onstart method of service1:
Protected override void onstart (string [] ARGs)
{
// Todo: The program that joins the dynamic server here.

Tcpchannel TCP = new tcpchannel (9999 );

Channelservices. registerchannel (TCP, false );

// Register a well-known object
Remotingconfiguration. registerwellknownservicetype (typeof (DAL. mydal ),
"Mydal", wellknownobjectmode. singlecall );

}

Note: after the service is created, you can install and start the service and set the service to automatically start.
3) client implementation (create a website remotingtest). Pay attention to adding a reference to the interface.
HTML:
Create
<Div>
<Asp: button id = "button1" runat = "server" text = "button" onclick = "button#click"/>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Asp: gridview id = "gridview1" runat = "server">
</ASP: gridview>
</Div>
Used for testing
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)
{
// Create a remote object from a remote address
Dal. imydal cdal = (DAL. imydal) activator. GetObject (typeof (DAL. imydal ),
"TCP: // localhost: 9999/mydal ");

// Call the method of the Remote Object
Textbox1.text = cdal. getstr ();
}
Protected void button#click (Object sender, eventargs E)
{
// Create a remote object from a remote address
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 ();
}
}

Note: The client uses the reference interface instead of directly referencing the remote object, which takes into account the need for low coupling. In this way, the client cannot detect the implementation of the modification method on the server and does not need to update the reference.

Related Article

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.