WCF 简单架构(二) 实现服务

来源:互联网 发布:李奇微为什么被换 知乎 编辑:程序博客网 时间:2024/06/04 19:47

基于wcf契约的实现,可根据需要自行处理。这里加上简单的抽象基类,方便之后扩展,基类中可加入计数,日志等监控功能

/// <summary>    /// 所有wcf实现处理基类    /// </summary>    public abstract class WcfServiceBase : IWcfService    {        protected abstract WcfResponse RequestFun(WcfRequest req);        /// <summary>        /// 所有外部请求的接收方法        /// </summary>        /// <param name="req"></param>        /// <returns></returns>        public virtual WcfResponse Request(WcfRequest req)        {            WcfResponse res = null;            try            {   // 可在此处加入日志等内容.........                var resFun = RequestFun(req);                if (resFun != null)                {                    resFun.Head.IsSuccess = true;                    return resFun;                }                throw new Exception(                    string.Format("调用的方法不存在 {0}", req.Head.RequestMethodName));            }            catch (Exception ex)            {                if (res == null)                    res = new WcfResponse();                Exception innerExp = ex;                while (innerExp.InnerException != null)                {                    innerExp = innerExp.InnerException;                }                res.Head.Message = string.Format("{0}-{1}", ex.Message, innerExp.Message);                res.Head.IsSuccess = false;            }            return res;        }    }

具体使用时只需要继承此类和对应的契约接口,可以如下实现,实现的是具体的业务接口对应的方法

    public class Hello_WCF : WcfServiceBase, IWcfHello    {                protected override WcfResponse RequestFun(WcfRequest req)        {            WcfResponse res = new WcfResponse();            object[] objs = (object[])req.Body.Parameters;            Hello biz = new Hello();            switch (req.Head.RequestMethodName)            {                case "CallHello":                    res.Body.Data = biz.CallHello((UserEntity)objs[0]);                    break;                case "Times":                    res.Body.Data = biz.Times();                    break;                case "Reset":                    biz.Reset();                    break;                default :                    return null;            }            res.Head.IsSuccess = true;            return res;        }    }


为了能跑起来,还需要加上配置,为了通用不进行xml配置了,使用代码简单实现,有需要的同学也可以把它改为反射配置,会更灵活

        static void Main(string[] args)        {            try            {                string address = "net.tcp://127.0.0.1:20001/WCFService/IHello";                Uri add = new Uri(address);                var bind = new NetTcpBinding();                bind.Security.Mode = SecurityMode.None;                 var host = new ServiceHost(typeof(Hello_WCF));                var contract = typeof(IWcfHello);                host.AddServiceEndpoint(contract, bind, address);                host.Open();                Console.WriteLine("Press enter will be closed  .... ");                Console.ReadLine();            }            catch(Exception ex)            {                Console.WriteLine(ex.Message);            }        }

完整的源代码可到 此地下载




0 0
原创粉丝点击