Simplify distributed system development using Websharp Service Locator

xiaoxiao2021-03-06  123

Use Websharp Service Locator

Simplified distributed system development

What is WebSharp Service Locator

For multi-layered applications, we usually divide them into clients, apply service layers, and databases. In the application service layer, we need to consider at least two aspects:

ü How to achieve business logic

ü How to provide services to the client.

We may use a variety of techniques to provide service providing: WebService, .NET Remoting, Even EJBs, etc. Such a large technology, brought great flexibility, but also brings problems, one of them, how many of the service technologies have to have a corresponding client access technology. Even in some distributed applications, application logic uses different technologies development, exists on different machines, some exists in the client, and some use .NET Remoting development, existence within the LAN, some Using Web Service on the Internet, sometimes we want the same business logic to support different clients.

In this case, we need a consistent service access programming model to integrate different service access modes, simplify the development and deployment of the system. WebSharp Service Locator provides such a capability that developers only need to define service access interfaces, which can use consistent ways to access these services without paying different points between these services. The framework automatically generates the agent to access the remote service.

Websharp is a new open source project on SourceForge. The target is to provide a lightweight application system framework in a .NET environment, which contains three main content: an O / R mapping framework, an AOP frame, and a service Locator. Service Locator currently only completed the local assembly locator, the WebService Locator, and the .NET Remoting Locator, we can use the framework features it provides to help our development. Websharp Service Locator The following goals are to implement access to J2EE. You can download all source code from http://www.sourceforge.net/projects/websharp/.

Main interface of Websharp Service Locator

WSL is a lightweight framework that is very easy to use and expand. If you want to use WSL, only one class needs to be dealt: ServiceLocator, its definition is as follows:

Public Abstract Class ServiceLocator

{

Public Static Object FindService (String ServiceName, Type ClientInterface)

}

If you want to extend this frame with your own locator, only one interface needs to scale: iServiceLocator. This interface is very simple, there is only one way:

Public Interface IserveLocator

{

Object FindService (String ServiceName, Type ClientInterface);

}

Websharp Service Locator configuration file

WSL needs to be configured in three places.

First, in the Configsections section, the relevant information of the WSL configuration file is registered, the configuration method is as follows:

CONFIGSECTIONS>

Then, in the Websharp.Enterprise section, different service locators are registered. If you expand this framework, add a new service locator and register here. Among them, the format of the Locator property is: "Class full name, Assembly name". Service locator is Singleton. Here are information about the registration of the service locator supported by WSL:

Locator = "Websharp.Enterprise.localassemblyLocator, Websharp" />

Locator = "Websharp.Enterprise.WebserviceLocator, Websharp" />

Locator = "Websharp.Enterprise.dotNetRemingLocator, Websharp" />

ServiceTypes>

Websharp.Enterprise>

Finally, register each service in the Services section under Websharp.Enterprise. Each service needs dependent on different Locator implementations, but Name, Service-Type and Deploy-Model are must be. For deploy-model, there are two attribute values: Singleton and MultiInstance.

Below is an example:

Locator = "Websharp.Enterprise.localassemblyLocator, Websharp" />

Locator = "Websharp.Enterprise.WebserviceLocator, Websharp" />

Locator = "Websharp.Enterprise.dotNetRemingLocator, Websharp" />

ServiceTypes>

TYPE = "EnterpriseClient.HelloWorld, EnterpriseClient" />

URL = "http://localhost/webservicetest/hello.asmx"

Namespace = "http://www.websharp.org/webservices/" />

SERVICES>

Websharp.Enterprise>

Note: For profiles, in the web project, you can be a web.config file. For Windows projects, you can add an app.config configuration file to your project. For more information on the .NET project profile, please refer to the relevant documentation for MSDN.

How to use WebSharp Service Locator?

Using WSL, the general method is like this:

1. Define an interface that is consistent with the service you need to access (of course, if your service is implemented, you can use the interface directly). The method names and parameters of the interface must match the method names and parameters of the service class. If your method name and service method are inconsistent, you can use ServiceMethodNameAttribute to specify the method name of the service.

2. Register the service you need to access in the configuration file.

3. Call the serviceLocator's FINDSERVICE method.

4. Method of calling the interface. .

Here are some examples, these examples are developed using Visual Studio.net 2003, and can also be downloaded from SourceForge.

Hello World example of LocalassemblyLocator

Follow the steps below:

1. Create a Windows Console project called "EnterpriseClient" to join Websharp.dll.

2. Add a class, named "HelloWorld", then add a method called "getHello", the code is as follows:

Public class helloworld

{

Public String GetHello (String Hello)

{

Return hello;

}

}

3. Add a interface called "IhelloWorld", the code is as follows:

Public Interface Ihelloworld

{

String getHello;

ServiceMethodName ("gethello")]]

String getHello2 (String Hello);

}

4. Fill in the configuration file

XML Version = "1.0" encoding = "UTF-8"?>

TYPE = "Websharp.Enterprise.EnterpriseConfighandler, Websharp" />

CONFIGSECTIONS>

Locator = "Websharp.Enterprise.WebserviceLocator, Websharp" />

ServiceTypes>

Deploy-model = "singleton"

TYPE = "EnterpriseClient.HelloWorld, EnterpriseClient" />

SERVICES>

Websharp.Enterprise>

CONFIGURATION>

5. Add the following code in the main method:

Public static void

Main

(String [] ARGS)

{

Ihelloworld Hello = ServiceLocator.FindService ("HelloWorld", TypeOf (IhelloWorld) AS IHELLOWORLD;

Console.writeline (Hello World ");

Console.writeline (Hello Again ");

Console.readline ();

}

6. Operate the program, you can get the following results:

Hello World WebServiceLocator example

Follow the steps below:

1. Create a new WebService project called "WebServiceTest".

2. Create a new WebService class, named "Hello", and add a "helloWorld" method, the code is as follows:

[WebSpace (Namespace = "http://www.websharp.org/webservices/")]

Public class hello: system.web.services.webservice

{

[WebMethod]

Public String HelloWorld ()

{

Return "Hello World";

}

}

3. Use the "EnterpriseClient" project we have created above, add an interface "ihello", the code is as follows:

Public Interface Ihello

{

String helloworld ();

}

4. Fill in the configuration file

URL = "http://localhost/webservicetest/hello.asmx"

Namespace = "http://www.websharp.org/webservices/" />

5. Add the following code in the main method: Public Static Void

Main

(String [] ARGS)

{

Ihello Hello1 = ServiceLocator.FindService

("HelloWordWebservice", Typeof (Ihello) as Ihello;

Console.writeline (Hello1.helloWorld ());

Console.readline ();

}

6. Run the program, get the following result:

summary

With WSL, we can use a consistent programming model to access different types of services to simplify the development and deployment of software. For example, we can develop software using local assembly methods while starting, and then make it easy to change the service to use WebService to make software into multi-layer applications. We can also use WSL to allow the same service to support different clients, while all clients use the same programming model.

Websharp is a framework that is still in the development phase, but because he is open source, we can use him directly to further develop. Currently, WSL support is not much simpler, but also is relatively simple, but he provides a good framework and construction of a distributed application, in the future, he will provide more and more features.

转载请注明原文地址:https://www.9cbs.com/read-125897.html

New Post(0)