• DotNET企业架构应用实践 用服务定位器(SL)完成服务的多种实现的统一调用


            前面的文章服务定位器(SL)与AgileEAS.NET中的实现介绍了服务定位器的一些概念、应用场景与AgileEAS.NET平台中SL的实现,本文是这骗文件的一个例子与Demo,详细的演示SL在应用开发中的使用。

            下面我说开始例子,假设有这么一个应用场景,我们需求一个Hello服务,并且需要在XML WebService、.NET Remoting和本地同进程中三种不同环境的应用,也就是说,这个服务可能会有三中实现,具体使用那一个,在应用过程中决定,我先贴个简单的类图:

    wps_clip_image-20776

            现在我们来开始干活,一步一步实现这个应用,首先定义服务接口,建一个名称为Hello.Interface的类库项目,定义一个IHello接口:

        public interface IHello
        {
            string SayHello(string name);
        }
                     接着我们做WebService实现,添加一个Hello.WebService的ASP.NET Web服务应用程序并引用Hello.Interface,添加一个HelloService的WebService:
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [ToolboxItem(false)]
        public class HelloWebService : System.Web.Services.WebService,Hello.Interface.IHello
        {
            [WebMethod]
            public string SayHello(string name)
            {
                return "hello," + name;
            }
        }

            实现.NET Remoting和LocalComponent,添加一个名称为Hello.Service类库项目,添加一个类Hello:
            public string SayHello(string name)
            {
                return "hello," + name;
            }

            实现一个简单的.NET Remoting的运行环境,建一个名称为Hello.ServiceConsole的控制台项目:
        class Program
        {
            static void Main(string[] args)
            {
                TcpChannel channel = new TcpChannel(9001);   
                ChannelServices.RegisterChannel(channel, false);
    
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(Hello.Service.HelloService), 
                    "Hello", WellKnownObjectMode.Singleton);
                System.Console.ReadLine();
            }
        }

            最后完成我们的调用客户端,建一控制台项目Hello.Client,引用Hello.Interface项目和EAS.IOCContainer.dll、EAS.ServiceLocator.dll程序组,定义服务消费者HelloClient:
        class HelloClient
        {
            IHello hello = null;
    
            public HelloClient()
            {
                this.hello = ServiceFactory.GetService("HelloService", typeof(IHello)) as IHello;
            }
    
            public IHello Hello
            {
                get
                {
                    return this.hello;
                }
            }        
        }

            控制台程序入口Program :
        class Program
        {
            static void Main(string[] args)
            {
                System.Console.WriteLine(new HelloClient().Hello.SayHello("james"));
                System.Console.ReadLine();
            }
        }

            定义配置文件App.config :

    <configuration>
    	<configSections>
    		<section name="EAS.Services" 
    				 type="EAS.ServiceLocators.ServiceConfigHandler,EAS.ServiceLocator" />
    		<section name="EAS.Objects" 
    				 type="EAS.Objects.ConfigHandler,EAS.IOCContainer"/>
    	</configSections>
    	<EAS.Objects>
    		<object name="HelloObject" LifestyleType="Thread" 
    				type="Hello.Service.HelloService,Hello.Service" />
    	</EAS.Objects>
    	<EAS.Services>
    		<Services>
    			<Service name="HelloService" service-type="DotNetRemoting" 
    					 Singleton=" true" url="tcp://localhost:9001/Hello"/>
    				<!--<Service name="HelloService" service-type="LocalComponent" 
    							component="HelloObject" />-->
    			<!--<Service name="HelloService" service-type="WebService" Singleton=" true" 
    					 url="http://localhost:56824/HelloWebService.asmx"/>-->
    		</Services>
    	</EAS.Services>
    </configuration>

            运行客户段,可以看到如下的输入截图:

    wps_clip_image-23084

            是不是很简单,在实际应用中,对于这种服务运行环境的未知性,我们可以通过这种简单的方法进行处理,服务消费者只需求知道接口,而无需知道具体的实现,开发人员可以根据客户的不同应用场景而做出不同的实现,上例中的服务定义配置项中,我注释了DotNetRemoting和LocalComponent两种方式的Hello服务,大家可以释放放开其中的一个而注释另外两个用于配置运行。

             这个例子的源代码我已上传,请下载Hello.Example.rar

    QQ群:15118502

    链接:AgileEAS.NET应用开发平台介绍

    AgileEAS.NET之敏捷并行开发方法

    敏捷软件工程实验室

  • 相关阅读:
    制作类似QQ截图软件
    XML文件与实体类的互相转换
    MFC中真彩工具条的制作方法
    MFC使用技巧集锦(1)(转载)
    抽象工厂模式与工厂方法模式区别
    VC数据库编程分析
    如何让工具条显示256色图像
    华为软件编程规范和范例
    设计模式总结性试题
    VC++中基于ADO操作ACCESS数据库,FLEXGRID控件的综合应用
  • 原文地址:https://www.cnblogs.com/eastjade/p/1793703.html
Copyright © 2020-2023  润新知