Simplify distributed system development with WEBSHARP service locator

Source: Internet
Author: User
Tags config extend readline visual studio
web| distributed use of WEBSHARP service Locator

Simplify Distributed system development

What is Websharp Service Locator
For multi-tier application Systems, we usually divide them into clients, application services, and databases. At the application service level, we need to consider at least two issues:

• How to implement business logic

• How to provide services to clients.

We may use a variety of technologies to implement service delivery: Webservice,. Net Remoting, and even ejbs. With so much implementation technology, it brings a lot of flexibility, but it also brings problems, one of which is how many server-side technologies are available, and how many corresponding client access technologies are available. Even in some distributed application systems, the application logic is developed using different technologies, exist on different machines, some exist in the client computer, some use the. Net remoting, exist in the LAN, some use Web Service on the Internet, and sometimes We want the same business logic to support different clients.

In this case, we need a consistent service access programming model to simplify the development and deployment of the system by integration of different service access patterns. The Websharp service Locator (WSL) provides the ability for developers to define a service provider only to access these services in a consistent manner without regard to the differences between these services. The framework automatically generates the agents needed to access the remote service.

Websharp is a new open source project on SourceForge, with the goal of providing one. NET environment, it contains three main content: an O/R mapping Framework, an AOP framework, and a service Locator. Service Locator Currently only completes the initial development of the local assembly locator, the WebService locator, and the. Net Remoting Locator, but we can use the framework features it provides to help us with our development. Websharp Service Locator The following goal is to achieve the visit to Java EE. All source code can be downloaded 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 extend. If you want to use WSL, then only one class needs to be dealt with: Servicelocator, which is defined as follows:

Public abstract class Servicelocator

{

public static Object Findservice (String Servicename,type clientinterface)

}


If you want to extend the framework with your own locator, then only one interface needs to be extended: Iservicelocator. This interface is very simple, there is only one way:

public interface Iservicelocator

{

Object Findservice (String Servicename,type clientinterface);

}


Configuration file for Websharp Service locator
WSL need to be configured in three places.

First, in the configsections section, register the relevant information about the WSL profile processing class, and configure the following methods:

<configSections>

<section name= "Websharp.enterprise"

Type= "Websharp.enterprise.enterpriseconfighandler,websharp"/>

Configsections>


Then, in the Websharp.enterprise section, register a different service locator. If you expand the framework yourself, add a new service locator and register here. Where the locator property is in the format: "Class full name, assembly name." The service locator is singleton. The following is information about the registration of the service locator currently supported by WSL:

<Websharp.Enterprise>

<ServiceTypes>

<servicetype name= "localassembly"

Locator= "Websharp.enterprise.localassemblylocator,websharp"/>

<servicetype name= "WebService"

Locator= "Websharp.enterprise.webservicelocator,websharp"/>

<servicetype name= "Dotnetremoting"

Locator= "Websharp.enterprise.dotnetremotinglocator,websharp"/>

Servicetypes>

Websharp.enterprise>


Finally, in the Services section under Websharp.enterprise, register each service. The attributes required for each service depend on the implementation of different locator, but name, Service-type, and Deploy-model are required. For Deploy-model, you can have two property values: Singleton and multiinstance.

Here is an example:

<Websharp.Enterprise>

<ServiceTypes>

<servicetype name= "localassembly"

Locator= "Websharp.enterprise.localassemblylocator,websharp"/>

<servicetype name= "WebService"

Locator= "Websharp.enterprise.webservicelocator,websharp"/>

<servicetype name= "Dotnetremoting"

Locator= "Websharp.enterprise.dotnetremotinglocator,websharp"/>

Servicetypes>

<Services>

<service name= "HelloWorld" service-type= "localassembly" deploy-model= "Singleton"

Type= "Enterpriseclient.helloworld,enterpriseclient"/>

<service name= "Helloworldwebservice" service-type= "WebService"

Deploy-model= "Singleton"

Url= "Http://localhost/webservicetest/hello.asmx"

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

Services>

Websharp.enterprise>


Note: For a configuration file, in a Web project, you can be a Web.config file, and for Windows projects you can add a app.config profile for your project yourself. For more information about. NET project profiles, refer to the MSDN documentation.

How do I use the Websharp service Locator?
The general approach to using WSL is this:

1. Define an interface that is consistent with the service you need to access (of course, if your service is implementing an interface, you can use that interface directly). The method name and parameters of an interface must be consistent with the method name and parameters of the service class. If your method name and service's method name are inconsistent, you can use Servicemethodnameattribute to indicate the method name of the service.

2. Register the service you need to access in the profile press.

3. Call the Servicelocator Findservice method.

4. Call the method of the interface.

Here are some examples of these examples that are developed using Visual Studio.NET 2003 and can also be downloaded from SourceForge.

Localassemblylocator's Hello World example
Follow these steps:

1. Create a Windows Console project named "Enterpriseclient" and add a reference to Websharp.dll.

2. Add a class named "HelloWorld" and add a method named "Gethello" with the following code:

public class HelloWorld

{

public string Gethello (string hello)

{

return hello;

}

}


3. Add an interface called "Ihelloworld" with the following code:

public interface Ihelloworld

{

String Gethello (string hello);



[Servicemethodname ("Gethello")]

String GetHello2 (string hello);

}


4. Fill in the configuration file

XML version= "1.0" encoding= "Utf-8"?>

<configuration>

<configSections>

<section name= "Websharp.enterprise"

Type= "Websharp.enterprise.enterpriseconfighandler,websharp"/>

Configsections>



<Websharp.Enterprise>

<ServiceTypes>

<servicetype name= "localassembly"

Locator= "Websharp.enterprise.localassemblylocator,websharp"/>

<servicetype name= "WebService"

Locator= "Websharp.enterprise.webservicelocator,websharp"/>

Servicetypes>



<Services>

<service name= "HelloWorld" service-type= "localassembly"

Deploy-model= "Singleton"

Type= "Enterpriseclient.helloworld,enterpriseclient"/>

Services>

Websharp.enterprise>

Configuration>


5. Add the following code to the Main method:

public static void Main (string[] args)

{

Ihelloworld hello= servicelocator.findservice ("HelloWorld", typeof (Ihelloworld)) as Ihelloworld;

Console.WriteLine (hello. Gethello ("Hello World"));

Console.WriteLine (hello. GetHello2 ("Hello Again"));

Console.ReadLine ();

}


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



Hello World's Webservicelocator example
Follow these steps:

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

2. Create a new WebService class named "Hello" and add a "HelloWorld" method with the following code:

[WebService (namespace= "http://www.websharp.org/webservices/")]

public class Hello:System.Web.Services.WebService

{

[WebMethod]

public string HelloWorld ()

{

Return to "Hello World";

}

}


3. Using the "Enterpriseclient" project we created above, add an interface "Ihello" with the following code:

public interface Ihello

{

String HelloWorld ();

}


4. Fill in the configuration file

<service name= "Helloworldwebservice" service-type= "WebService" deploy-model= "Singleton"

Url= "Http://localhost/webservicetest/hello.asmx"

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


5. Add the following code to the Main method:

public static void Main (string[] args)

{

Ihello hello1= Servicelocator.findservice

("Helloworldwebservice", typeof (Ihello)) as Ihello;

Console.WriteLine (Hello1. HelloWorld ());

Console.ReadLine ();

}


6. Run the program to get the following results:




Summary
With WSL, we can use a consistent programming model to access different types of services, simplifying software development and deployment. For example, we can start by using a local assembly to develop the software, and then easily change to use WebService to publish the service and turn the software into multi-tier applications. We can also use WSL to enable the same services to support different clients, and 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 for further development. Currently WSL support services are not many, implementation is also relatively simple, but he provides a good framework and build a distributed application program, in the future, he will provide more and more features.




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.