相比 WebService , Hessian 更简单、快捷。采用的是二进制 RPC 协议,因为采用的是二进制协议,所以它很适合于发送二进制数据。下面演示一个简单的 Hessian 示例程序。
知道Hessian 已经远超期待!
Java、Flash、Python、C++、.NET C#、D、Erlang、PHP、Ruby,看看伟大的Hessian 当前所支持的语言吧!
HessianCSharp 1.3.3版本前使用泛型以及接口interface继承问题解决方案!!(实例代码) 其他废话不说啦...
--------------------------------------------------------------------------------------------------------------------------------------
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<authentication mode="Windows"/>
<webServices>
<protocols>
<remove name="HttpPost"/>
<remove name="HttpGet"/>
</protocols>
</webServices>
<httpHandlers>
<add verb="*" path="FilterService.hessian" type="App.Services.FilterService, Filter"/>
<add verb="*" path="HttpService.hessian" type="App.Services.HttpService, Filter"/>
<add verb="*" path="QueueService.hessian" type="App.Services.QueueService, Filter"/>
</httpHandlers>
<compilation debug="true">
</compilation>
</system.web>
<startup><supportedRuntime version="v2.0.50727"/></startup>
<system.codedom>
</system.codedom>
<system.webServer>
</system.webServer>
</configuration>
-----------------------------------------------------------------------------
QueueService
public class QueueService : ServiceBase, IQueueService
{
string baseDataPath = @"d://queue//";
public string Dequeue()
{
return GroupID;
}
public void Enqueue(string item)
{
}
public void Load()
{
}
public void Save()
{
}
public List<string> vv()
{
return new List<string>();
}
-------------------------------------------------------------------------------------------------------------------------
public class ServiceBase : CHessianHandler
{
string id = "default";
string groupId = "default";
public string ID
{
set { id = value; }
get { return id; }
}
public string GroupID
{
set { groupId = value; }
get { return groupId; }
}
}
-------------------------------------------------------------------------------------------------------------------------------
public interface IQueueService : IQueueService<string>
{
}
public interface IQueueService<T>
{
string ID { set; get; }
string GroupID { set; get; }
string Dequeue();
void Enqueue(T value);
void Load();
void Save();
List<string> vv();
}
------------------------------------------------------------------------------------------------------------------------
调试
class Program
{
static void Main(string[] args)
{
CHessianProxyFactory f = new CHessianProxyFactory();
f.IsOverloadEnabled = true;//
string url="http://localhost:3263/";
IQueueService service = f.Create(typeof(IQueueService), url + "QueueService.hessian") as IQueueService;
url = "http://blog.csdn.net/zfrong";
string data = "";
List<string> v;
data= service.Dequeue();
v = service.vv();//
}
以上调试结果 data返回null;v返回null;无法调用.....
问题原因 : 接口继承的情况下, proxyType是接口的话, proxyType.GetMethods();找不到接口继承的接口方法...
IQueueService service = f.Create(typeof(IQueueService), url + "QueueService.hessian") as IQueueService;
改成
IQueueService<string> service = f.Create(typeof(IQueueService<string>), url + "QueueService.hessian") as IQueueService<string>;
以上调试结果 data返回"default";v返回 new实例;调用成功!!!!!!!!
--------------------------------------------------------------------------------------------------------
下面 看如何 解决....解决方案如下...... 修改源代码 中 CHessianProxyStandardImpl类
hessiancsharp.client
/// <summary>
/// Proxy that works with .NET - Remote proxy framework
/// </summary>
public class CHessianProxyStandardImpl
-------------------------
添加 以下3个新方法 add new method;
/// <summary>
/// 曾繁荣 [email protected] blog:http://blog.csdn.net/zfrong
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
MethodInfo[] GetTypeMethods(Type t)
{
if (t.IsInterface)
{
System.Collections.Generic.List<MethodInfo> mColl=new System.Collections.Generic.List<MethodInfo>();
GetInterfaceMethods(t,mColl);
return mColl.ToArray();//
}
else
return t.GetMethods();//
}
/// <summary>
/// 曾繁荣 [email protected] blog:http://blog.csdn.net/zfrong
/// </summary>
/// <param name="type"></param>
/// <param name="mColl"></param>
void GetInterfaceMethods(Type type,System.Collections.Generic.List<MethodInfo> mColl)
{
if (type.IsInterface)
{
mColl.AddRange(t.GetMethods());//
Type[] ts = type.GetInterfaces();
foreach (Type t in ts)
{
GetInterfaceMethods(t, mColl);//
}
}
}
/// <summary>
/// /// 曾繁荣 [email protected] blog:http://blog.csdn.net/zfrong
/// </summary>
/// <param name="methodMessage"></param>
/// <returns></returns>
MethodInfo GetMethodInfoFor(IMethodCallMessage methodMessage)
{
foreach (MethodInfo m in this.m_methods)
{
if (m.Name.Equals(methodMessage.MethodName))
return m;
}
return null;
}
...以上3方法辅助接口找到所有继承的接口的接口方法c# interface
然后-------修改 源代码中 ........
public CHessianProxyStandardImpl(Type proxyType,CHessianProxyFactory hessianProxyFactory, Uri uri) : base(typeof(IHessianProxyStandard))
{
this.m_proxyType = proxyType;
this.m_methodCaller = new CHessianMethodCaller(hessianProxyFactory,uri);
//this.m_methods = proxyType.GetMethods();
this.m_methods =GetTypeMethods(proxyType) ;//zfrong
//问题原因 : 接口继承的情况下, proxyType是接口的话, proxyType.GetMethods();找不 到接口继承的接口方法...
}
public CHessianProxyStandardImpl(Type proxyType, CHessianProxyFactory hessianProxyFactory, Uri uri, string username, string password)
: base(typeof(IHessianProxyStandard))
{
this.m_proxyType = proxyType;
this.m_methodCaller = new CHessianMethodCaller(hessianProxyFactory, uri, username, password);
//this.m_methods = proxyType.GetMethods();
this.m_methods = GetTypeMethods(proxyType);//zfr ong
}
private MethodInfo GetMethodInfoForMethodBase(IMethodCallMessage methodMessage)
{
if (IsMethodNameUnique(methodMessage.MethodName))
{
if(this.m_proxyType.IsInterface)
return GetMethodInfoFor(methodMessage);
return this.m_proxyType.GetMethod(methodMessage.MethodName);
}
else
{
if (this.m_proxyType.IsInterface)
return GetMethodInfoFor(methodMessage);
return this.m_proxyType.GetMethod(methodMessage.MethodName, CHessianMethodCaller.GetArgTypes(methodMessage.Args));
}
}
测试....结果....成功!!!!!如有疑问 请联系我:曾繁荣 zfrong2000##hotmail.com blog:http://blog.csdn.net/zfrong