Implement the G module of ten modules

Source: Internet
Author: User

First of all, I am very glad that you have paid attention to and criticized my previous article. I have understood many technical terms, such as the Framework, architecture, and modules. At the same time, it also greatly deepened my understanding of the three-tier architecture. Thank you !!

Next I will analyze the working principle and implementation method of the G module in the next ten modules. The project uses the Microsoft. NET Remoting framework to implement distributed development. For more information about the. NET Remoting mechanism, see this article in the blog. I think it is well written.

Http://www.cnblogs.com/wayfarer/archive/2004/07/30/28723.html

Because it is a specific project, in order to meet the functional reuse, maintenance convenience and security requirements, it encapsulates many common classes and interfaces. It also hides many implementation details. The code I will talk about below is extracted, saving the need to call common classes at different layers.

The required configuration files are Service. config and client. config. The former is used to configure the information of the server object to be registered, and the latter is used to provide the client with information about the remote server registration channel. The content is as follows:

Service. config

1 <configuration> 
2 <service>
3 <wellknown mode="Singleton" assemblyName="THRSEC01008P" typeName="ThreeHigh.Application.Remoting.Bussiness.EmployeeInfoDB" objectUri="MyEmployeeInfo" />
4 <wellknown mode="Singleton" assemblyName="THRSEC01108P" typeName="ThreeHigh.Application.Remoting.Bussiness.MenuRoleSetDB" objectUri="MyMenuRoleSet" />
5
6 </service>
7 </configuration>
Client. config

1 <?xml version="1.0" encoding="utf-8"?>
2 <configuration>
3 <application>
4 <channels>
5 <channel ref="HTTP" ip="172.168.1.105" port="3000" />
6 </channels>
7 </application>
8 </configuration>

Server: It is divided into two versions: Console Startup Program in the development stage and window Service program. Mainly analyzes Console programs.

This part adopts the singleton mode for development and obtains the Server Object email class ServiceConfig to be registered and activated to read the information stored in the configuration information. The Singleton mode is used to ensure that all client program requests are processed by only one instance. Of course, there are many ways to implement the singleton mode. Here we mainly use the static initialization method.

ServiceConfig. cs

Using System;
Using System. IO;
Using System. Xml;
Using System. Text;
Using System. Reflection;
Using System. Collections. Generic;
Using System. Runtime. Remoting;

Namespace DETConsoleService
{
//************************************** *******
// * Obtains information about the Service. config configuration file.
//************************************** *******
Public class ServiceConfig
{
Private static ServiceConfig _ Instance = null;

Private List <WellKnownServiceTypeEntry> _ ServiceEntryList = new List <WellKnownServiceTypeEntry> ();

Public static ServiceConfig GetInstance ()
{
If (_ Instance = null)
{
_ Instance = new ServiceConfig ();
_ Instance. Initialize ();
}

Return _ Instance;
}
//************************************** *****
// * List of objects to be registered and activated.
//************************************** *****
Public List <WellKnownServiceTypeEntry> ServiceEntryList
{
Get
{
Return _ ServiceEntryList;
}
}


//************************************
// * Read the configuration file to obtain information about the object to be registered.
//************************************
Public void Initialize ()
{
Try
{
String appRoot = Path. GetDirectoryName (Assembly. GetExecutingAssembly (). Location );
String appFileName = appRoot + Path. DirectorySeparatorChar + "Config" + Path. DirectorySeparatorChar + "Service. config ";

If (File. Exists (appFileName ))
{
XmlDocument document = new XmlDocument ();
Document. Load (appFileName );

XmlNodeList serviceNodes = document. SelectNodes ("configuration/service/wellknown ");

If (serviceNodes! = Null & serviceNodes. Count> 0)
{
Foreach (XmlNode node in serviceNodes)
{
String typeName = node. Attributes ["typeName"]. Value;
String assemblyName = node. Attributes ["assemblyName"]. Value;
String mode = node. Attributes ["mode"]. Value;
String objectUri = node. Attributes ["objectUri"]. Value;

WellKnownObjectMode wMode = WellKnownObjectMode. Singleton;

If ("SingleCall". Equals (mode ))
{
WMode = WellKnownObjectMode. SingleCall;
}
WellKnownServiceTypeEntry ServiceEntry = new WellKnownServiceTypeEntry (typeName, assemblyName, objectUri, wMode );

_ ServiceEntryList. Add (ServiceEntry );
}

}
}
}
Catch
{
// Capture error logs.
}
}

}
}

 

The main console Program. cs is used to register and activate an object and provide it to the client for calling. Remoting channels mainly include Tcp and Http. In this example, Http is used.

Program. cs

Using System. Runtime. Remoting;
Using System. Runtime. Remoting. Channels;
Using System. Runtime. Remoting. Channels. Tcp;
Using System. Runtime. Remoting. Channels. Http;

Namespace DETConsoleService
{
Class Program
{
Private static HttpChannel _ HttpChannel;

Static void Main (string [] args)
{
Try
{
_ HttpChannel = new httpChannel (Convert. ToInt32 (3000 ));
// The ServerConfig class is used to retrieve the ChannelPort in the configuration file. For ease of use, I wrote 3000 directly.
ChannelServices. RegisterChannel (_ HttpChannel, false );

List <WellKnownServiceTypeEntry> entryList = ServiceConfig. GetInstance (). ServiceEntryList;
Foreach (WellKnownServiceTypeEntry entry in entryList)
{
RemotingConfiguration. RegisterWellKnownServiceType (entry );
}

Console. WriteLine ("service started properly ");

}
Catch (Exception ex)
{
Console. WriteLine ("Startup error:" + ex. Message );
}

Console. ReadLine ();
// Logout Channel
UnRegisterChannel ();
}
// Logout Channel
Private static bool UnRegisterChannel ()
{
IChannel [] channels = ChannelServices. RegisteredChannels;
Try
{
Foreach (IChannel eachChannel in channels)
{
ChannelServices. UnregisterChannel (eachChannel );
}
Return true;
}
Catch (Exception e)
{
Console. WriteLine (e. ToString ());
Return false;
}

}
}
}

At this point, the work on the server has basically ended. The function module will be added later. You only need to add the corresponding configuration items in the service. config configuration file.

Client: The G module of each program. Module A requires the object of the lower P module. You only need to call the G module to obtain the object. The Code is as follows.

GlobalSettingInfoMediation. cs

Using System. Reflection;

Namespace ThreeHigh. Application. Remoting. Mediation
{
Public class GlobalSettingInfoMediation
{
Public GlobalSettingInfoMediation ()
{

}

/// <Summary>
/// Obtain remote object reference.
/// </Summary>
/// <Returns> </returns>
Public static IGlobalSettingInfoDB GetGlobalSettingInfoDB ()
{
Try
{
IGlobalSettingInfoDB globalSettingInfoDB = (IGlobalSettingInfoDB) GetRemoteObject (typeof (IGlobalSettingInfoDB), "MyGlobalSettingInfo ");

Return globalSettingInfoDB;
}
Catch (Exception ex)
{
String msg = ex. Message;
Return null;
}
}

/// <Summary>
/// Return the interface type of the O module.
/// </Summary>
/// <Param name = "type"> return value type </param>
/// <Param name = "p"> name of the remote object, which corresponds to the objectUri value in Service. config. </Param>
/// <Returns> </returns>
Private static IGlobalSettingInfoDB GetRemoteObject (Type type, string p)
{
Try
{
// Read the client. config file.
ClientConfig config = ClientConfig. GetInstance ();
/// Http: // 172.161.38: 3000/MyEmployee
String remoteUrl = String. format ("{0 }://{ 1 }:{ 2}/{3}", config. channelRef, config. channelIP, config. channelPort, uri );
// Obtain the remote activation object.
Object obj = Activator. GetObject (type, remoteUrl );

Return obj;
}
Catch (Exception ex)
{
Throw ex;
}
}
}
}

After such processing, we only need to put the self-developed module P Module D module and the Assembly dll of the R module on the corresponding AP server, and configure the service. config file. The updates of these modules of the entire system do not need to be synchronized from each client. You only need to update the dll on the AP server.

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.