.net core编写转发服务

我有个小伙伴问我,他需要写一个转发服务的他有很多功能要通过他的服务转发~

 

技术栈又不一定asp.net core,我就想起泥水老前辈的BeetleX.FastHttpApi

中午午休,折腾了一会儿前辈,问清楚了FastHttpApi如何配置控制器依赖注入和控制器的路由配置

花了一些时间折腾,算折腾出来了

入口调用部分代码如下

        static void Main(string[] args)
        {
            UnitWork.Instance.Register((services) =>
            {
                services.AddScoped<Forward.Core.Forward>();

                services.AddSingleton<MaillService>();

                services.AddSingleton(typeof(IConfig), (serviceProvider)=>
                {
                    var instance = new Config();
                    instance.TryAdd<MaillService>("maill");
                    return instance;
                });
            });
            UnitWork.Instance.Builder();

            var mApiServer = new HttpApiServer();
            mApiServer.ActionFactory.ControllerInstance += (o, e) =>
            {
                e.Controller = UnitWork.Instance.ServiceProvider.GetRequiredService(e.Type);
            };
            mApiServer.Register(Assembly.Load("Forward.Core"));
            mApiServer.Open();

            Console.ReadLine();
        }
    }

服务本体


public class MaillService : IForwardService { private string userName = "2598145226@qq.com"; private string pawssword = "********"; private string _host = "smtp.qq.com"; [ParamType(typeof(MaillModel))] public async Task<object> ExecuteAsync(object param) { MaillModel maillModel = param as MaillModel; using (SmtpClient smtpClient = new SmtpClient(_host, 587)) { smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 smtpClient.Host = _host;//指定SMTP服务器 smtpClient.Credentials = new NetworkCredential(userName, pawssword);//用户名和密码 smtpClient.EnableSsl = true; MailAddress fromAddress = new MailAddress(userName, "华灯"); MailAddress toAddress = new MailAddress(maillModel.ToMail); MailMessage mailMessage = new MailMessage(fromAddress, toAddress); mailMessage.Subject = maillModel.Subject;//主题 mailMessage.SubjectEncoding = Encoding.UTF8; mailMessage.Body = maillModel.Body;//内容 mailMessage.BodyEncoding = Encoding.UTF8;//正文编码 mailMessage.IsBodyHtml = true;//设置为HTML格式 mailMessage.Priority = MailPriority.Normal;//优先级 await smtpClient.SendMailAsync(mailMessage); } return true; } } internal class MaillModel { public string ToMail { get; set; } public string Subject { get; set; } public string Body { get; set; } }

转发服务部分

   [Controller(BaseUrl = "/Forward", SingleInstance = false)]
    public class Forward
    {
        private IForwardFactory ForwardFactory { get; }

        public Forward(IForwardFactory forwardFactory)
        {
            ForwardFactory = forwardFactory;
        }

        [Post(Route = "{url}")]
        [NoDataConvert]
        public async Task<ResponseModel> Service(string url,IHttpContext context)
        {
            var result = new ResponseModel()
            {
                RequestTime = DateTime.Now
            };

            try
            {
                var json = context.Request.Stream.ReadString(context.Request.Length);

                result.Data = await ForwardFactory.ForwardAsync(url, json);
                result.ResponseTime = DateTime.Now;

                result.IsSuccessFul = true;
            }
            catch(Exception ex)
            {
                result.ResponseTime = DateTime.Now;
                result.Data = ex.ToString();
            }

            return result;
        }
    }
   [Controller(BaseUrl = "/Forward", SingleInstance = false)]
    public class Forward
    {
        private IForwardFactory ForwardFactory { get; }

        public Forward(IForwardFactory forwardFactory)
        {
            ForwardFactory = forwardFactory;
        }

        [Post(Route = "{url}")]
        [NoDataConvert]
        public async Task<ResponseModel> Service(string url,IHttpContext context)
        {
            var result = new ResponseModel()
            {
                RequestTime = DateTime.Now
            };

            try
            {
                var json = context.Request.Stream.ReadString(context.Request.Length);

                result.Data = await ForwardFactory.ForwardAsync(url, json);
                result.ResponseTime = DateTime.Now;

                result.IsSuccessFul = true;
            }
            catch(Exception ex)
            {
                result.ResponseTime = DateTime.Now;
                result.Data = ex.ToString();
            }

            return result;
        }
    }

他只需要在入口地方注入服务即可

调用一个发送邮件的服务

项目在

https://github.com/htrlq/ForwardService

posted @ 2019-05-09 17:33  沉迷代码的萌新  阅读(1188)  评论(0编辑  收藏  举报