技术开发 频道

Nbear高级应用-使用分布式服务工厂

IT168技术文档】

    假设对于使用本地服务工厂的WEB项目,为了提高系统的可扩展性,我们要让他支持分布式服务(及将不同的服务的实现部署到多台服务器上,而不仅仅是一台),那么该怎么做呢?是不是很复杂呢?不复杂,一点也不复杂。 

    1.  将NBear.IoC.Servers.ServiceMQServer.exe和NBear.IoC.Servers.ServiceMQServer.exe.config文件复制到应用程序部属目录可以修改exe和NBear.IoC.Servers.ServiceMQServer.exe.config中的设置,修改remoting协议和端口等参数。典型的该配置文件内容如下: 

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="ServiceFactory" type="NBear.IoC.Service.Configuration.ServiceFactorySetting, NBear.IoC" /> </configSections> <ServiceFactory type="Remoting" name="MMQ" protocol="HTTP" server="127.0.0.1" port="8000" debug="true" /> </configuration>

    2. NBear.IoC.Hosts.ServiceHost.exe和NBear.IoC.Hosts.ServiceHost.exe.config文件复制到应用程序部属目录 

    可以修改exe和NBear.IoC.Hosts.ServiceHost.exe.config中的设置,修改remoting协议和端口等参数,并注意设置castle块,将service interface的实现类和接口关联起来。典型的该配置文件内容如下: 

<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" /> <section name="ServiceFactory" type="NBear.IoC.Service.Configuration.ServiceFactorySetting, NBear.IoC" /> </configSections> <castle> <components> <!--You can use standard castle component decleration schema to define service interface impls here--> <!--<component id="sample service" service="ServiceInterfaces.ISampleService, ServiceInterfaces" type="ServiceImpls.SampleServiceImpl, ServiceImpls"/>--> </components> </castle> <ServiceFactory type="Remoting" name="MMQ" protocol="HTTP" server="127.0.0.1" port="8000" debug="true" /> <connectionStrings> <add name="TestAccessDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\skeleton\EnterpriseSample\Core\website\App_Data\TestAccessDb.mdb" providerName="NBear.Data.MsAccess.AccessDbProvider"/> <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/> </connectionStrings> </configuration>

    注意,注释部分用来设置服务接口对应的实现类,当然,别忘了在这里设置数据库连接字串,因为,host是要运行实现类代码的。 

    3. 使用分布式服务工厂   

on name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor"/> <section name="ServiceFactory" type="NBear.IoC.Service.Configuration.ServiceFactorySetting, NBear.IoC" /> </configSections> <castle> <components> <!--<component id="sample service" service="ServiceInterfaces.ISampleService, ServiceInterfaces" type="ServiceImpls.SampleServiceImpl, ServiceImpls"/>--> </components> </castle> <ServiceFactory type="Remoting" name="MMQ" protocol="HTTP" server="127.0.0.1" port="8000" <?xml version="1.0"?> <configuration> ????configSections> ????????section name="castle" <connectionStrings> <add name="TestAccessDb" connectionString="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Teddy\NBear\skeleton\EnterpriseSample\website\App_Data\TestAccessDb.mdb" providerName="NBear.Data.MsAccess.AccessDbProvider"/> <add name="Northwind" connectionString="Server=(local);Database=Northwind;Uid=sa;Pwd=sa" providerName="NBear.Data.SqlServer.SqlDbProvider"/> </connectionStrings> <appSettings/> <system.web> <compilation debug="true"> <assemblies> <add assembly="System.Runtime.Serialization.Formatters.Soap, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Runtime.Remoting, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> <add assembly="System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies> </compilation> <authentication mode="Windows"/> </system.web> </configuration>

    以上代码中,注意截粗的部分和被注释的部分,加粗部分设置了remoting的ServiceFactory的参数,而因为此时我们的服务实现类部署在host端了,这里不再需要部署实现类了。和前面的使用本地服务工厂相比,此时,NBear.Ioc.Service.ServiceFactory类使用远程mq实例来初始化。 

    注意,真正使用服务的页面的代码没有作任何修改,依然如下: 

public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ServiceInterfaces.ISampleService service = ServiceFactory.GetService<ServiceInterfaces.ISampleService>(); GridView1.DataSource = service.LoadAllProductsByPage(10, 2); GridView2.DataSource = service.LoadAllUsers(); DataBind(); } }

    注意,必须先运行ServiceMQServer,再ServiceHost,最后运行website,才能获得正确的运行结果。

0
相关文章