概述


85374发布的版本增加在IoC中集成远程调用方式,即对于使用服务者来说,获取服务对象是远程对象,但是是透明的,使用者勿需关心服务实现者是在本地还是在远程;
同时提供一种比较便捷的使用Remoting来发布服务。
注意:采用本通讯方式,仅需.NET Framework 2.0,仅需引用HTB.DevFx.BaseFx.dll即可

使用Remoting技术发布服务(服务端)

有如下服务契约接口定义
namespace ClassLibrary1//定义在ClassLibrary1.dll
{
	public interface IClassService
	{
		string HelloWorld();
	}
}
服务端有如下实现:
using System;
using ClassLibrary1;
using HTB.DevFx.Remoting;

namespace ConsoleApplication1
{
	internal class ClassServiceInternal : IClassService//定义在ConsoleApplication1.exe中
	{
		public string HelloWorld() {
			return DateTime.Now.ToString();
		}
	}
}
使用如下格式发布服务:
<?xml version="1.0"?>
<configuration>
	<configSections>
		<section name="htb.devfx" type="HTB.DevFx.Config.ConfigSectionHandler, HTB.DevFx.BaseFx" />
	</configSections>

	<htb.devfx>
		<remoting>
			<services>
				<add name="classService.rem" type="ClassLibrary1.IClassService, ClassLibrary1" serviceType="ConsoleApplication1.ClassServiceInternal, ConsoleApplication1" /><!--name为发布地址,需以.rem为后缀,type为契约接口定义,serviceType为服务实现-->
			</services>
		</remoting>
	</htb.devfx>

	<system.runtime.remoting><!--标准的Remoting配置,主要是配置一些使用的通讯协议等-->
		<application>
			<channels>
				<channel
				   ref="http"
				   port="8086" />
			</channels>
		</application>
	</system.runtime.remoting>
</configuration>
对于非Web服务宿主,请按如下进行初始化:
using System;
using ClassLibrary1;
using HTB.DevFx.Remoting;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args) {
			RemotingHelper.RemotingServiceInitialize();
			Console.WriteLine("Ready....");
			Console.ReadLine();
		}
	}
}
在Web应用中可以采用替换*.rem的处理工厂方式,把System.Runtime.Remoting.Channels.Http.HttpRemotingHandlerFactory"替换成HTB.DevFx.Remoting.Web.HttpRemotingHandlerFactory
<?xml version="1.0"?>
<configuration>
	<system.web>
		<httpHandlers>
			<remove path="*.rem" verb="*" />
			<add path="*.rem" verb="*" type="HTB.DevFx.Remoting.Web.HttpRemotingHandlerFactory, HTB.DevFx.BaseFx" />
		</httpHandlers>
	</system.web>

	<system.webServer>
		<handlers>
			<add name="HttpRemotingHandlerFactory" path="*.rem" type="HTB.DevFx.Remoting.Web.HttpRemotingHandlerFactory, HTB.DevFx.BaseFx" verb="*" />
		</handlers>
	</system.webServer>
</configuration>

使用Remoting技术调用服务(客户端)

按如下格式配置IoC
<?xml version="1.0"?>
<configuration>
	<configSections>
		<section name="htb.devfx" type="HTB.DevFx.Config.ConfigSectionHandler, HTB.DevFx.BaseFx" />
	</configSections>

	<htb.devfx>
		<objects configSet="{tag:'object'}">
			<object name="ClassServiceClient" type="ClassLibrary1.IClassService, ClassLibrary1" mapTo="http://localhost:8296/classService.rem" builder="@RemotingObjectBuilder" />
		</objects>
	</htb.devfx>
</configuration>
按一般方式获取和调用服务:
using System;
using System.Collections.Generic;
using System.Text;
using ClassLibrary1;
using HTB.DevFx.Esb;

namespace ConsoleApplication2
{
	class Program
	{
		static void Main(string[] args) {
			var svr = ServiceLocator.GetService<IClassService>();
			Console.WriteLine(svr.HelloWorld());
			Console.ReadLine();
		}
	}
}

Last edited Nov 8, 2011 at 2:02 AM by R2, version 5

Comments

No comments yet.