遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

asp.net Hessian 服务的注册

Hessian服务端实现了IHttpHandle,

默认情况下是在Web.Config中的handles接点中注册,这样当有 很多实现时比较麻烦

这个时候可以实现IHttpHandleFactory注册到Web.Config中,在Factory中实现对具体服务的实例化,

另外也可以使用RouteTable方式,自己实现以个IRouteHandle,注册到RouteTable的Routes表中,

需要注意的是,RouteTable方式在asp.net管线的位置靠前,会屏蔽掉后面的IHttphandle方式.

另外注意在IIS 6中,需要在IIS中添加对.hessian的印射(取消确认文件存在选择),而在IIS7.0中需要在Web.Config的Web.Server配置节里注册HttpHandle或HttpHandleFactory-如果采用的不是RouteTable方式的话

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Collections.Concurrent;
using System.Reflection;
namespace PhoneAPI.Service
{
    public class HessianRouteHandle : IRouteHandler
    {
        private Lazy<ConcurrentDictionary<String, Type>> _ServiceLazy = new Lazy<ConcurrentDictionary<string, Type>>(() => {
            var dic = new ConcurrentDictionary<String, Type>();

            var assembly = Assembly.Load("F.Studio.Prime.Hessian");
            //接口类完全限定名将".I"替换成".Impl."
            //F.Studio.Prime.Hessian.Impl.AccountService 转换后如下
            //f-studio-prime-hessian-accountservice
            foreach (var type in assembly.GetTypes().ToList())
            {
                if (type.FullName.IndexOf(".Impl.") > 0 && type.FullName.EndsWith("Service"))
                {
                    var key = type.FullName.Replace(".Impl.", ".").Replace(".", "-").ToLower();
                    dic[key] = type;
                }
            }

            return dic;
        }, true);
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            
            var service = requestContext.RouteData.Values["service"].ToString();
            if (_ServiceLazy.Value.ContainsKey(service))
            {
                return Activator.CreateInstance(_ServiceLazy.Value[service]) as IHttpHandler;
            }
            return null;
        }
    }
}
View Code
复制代码
  void Application_Start(object sender, EventArgs e)
        {
            // 在应用程序启动时运行的代码
            RouteTable.Routes.Add(new Route("{service}.hessian", new HessianRouteHandle()));
        }
复制代码
       private T GetHessionProxy<T>()
            where T : class
        {

            var url = ServerUrl + typeof(T).FullName.Replace(".I", ".").Replace(".", "-").ToLower() + ".hessian";
            return factory.Create(typeof(T), url) as T;
      

        }
复制代码

 

posted on   遗忘海岸  阅读(657)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2007-10-30 代码生成器参考文挡一---简单三层与数据库约定
点击右上角即可分享
微信分享提示