**********************************************************************************
*
* KJFramework.ServiceModel 基于SOA模式的轻量级服务调用框架模型
*
**********************************************************************************
[说明:]
KJFramework框架中内置了一套轻量级RPC服务调用框架,使用此框架,将使得用户不再关心底层协议转换和网络传输的繁琐过程,解放程序员的脑力劳动。
此框架的使用也非常简单,作为服务器端的使用人员,只需要完成一个契约的实现,就可以通过配置不同的底层通信信道来达到不同通信模式需求。
在客户端,我们可以有2种方法来完成对于服务器端契约的使用:
1. 直接引用服务器端契约文件(dll文件)
2. 使用内部专门编写的VS插件(VS Add-in)来完成对于远程服务端契约的动态生成
当然,我们推荐使用第二种方式,因为使用VS插件可以帮助用户自动生成针对于每一个契约操作的异步版本。
目前此内置框架被初步定义在命名空间:KJFramework.ServiceModel内,拥有独立Assembly.
目前此框架中,仍保留了内部扩展能力,日后扩展后,将会通过配置文件来增强配置能力。
以下为DEMO示例:
*Server Side:
//以下为服务端契约接口和实现
[ServiceContract(ServiceConcurrentType = ServiceConcurrentTypes.Concurrent, Description="这是一个契约的描述", Name = "IHellowWorld 服务契约", Version = "0.0.0.1")]
public interface IHelloWorld
{
[Operation(IsOneway = true)]
void Increment();
[Operation]
int[] Hello(int[] text);
[Operation]
String Hello(int x, int y, string z);
[Operation]
string TestMultiPackage();
[Operation]
string TestIntellectObject(InObject inObject);
[Operation]
InObject3 TestReturnValue(string content);
}
//服务器端实现,具体方法的实现就不写了 :)
public class HelloWorld : IHelloWorld
{
/*Some code here.*/
}
/* *服务端用来公开一个服务的代码 *从此代码可知,公开服务的通讯方式为TCP *地址为本地的9999端口 *公开的服务全地址为: tcp://localhost:9999/Test */ ServiceHost serviceHost = new ServiceHost(typeof(HellowWorld), new TcpBinding("tcp://localhost:9999/Test")); //如果设置此属性,则会让客户端从网页上直接预览此服务的契约 serviceHost.IsSupportExchange = true; serviceHost.Opened += (sender, e) => { Console.WriteLine("Service has been opened."); }; serviceHost.Open(); Console.ReadLine();*Client Side:
//IClientProxy<T>接口是一个动态的客户端代理接口 IClientProxy<IHelloWorld> proxy= new DefaultClientProxy<IHelloWorld>(new TcpBinding("tcp://localhost:9999/Test")); InObject inObject = new InObject { Inf1 = 1, Info2 = DateTime.Now, Info = "Info", Info3 = new[] { "1", "2" }, Info4 = new[] { 3, 4 } }; //调用的使用使用proxy.Channel, 此对象的类型是服务器端公开的契约接口 //从这里看出,客户端调用的这个方法,是个异步版本,是VS插件自动生成的 proxy.Channel.TestIntellectObjectAsync(inObject, delegate(IAsyncCallResult callResult) { if (callResult.IsSuccess) Interlocked.Increment(ref callbackcount); else if (!callResult.IsSuccess && callResult.LastError != null) throw callResult.LastError; });