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

技术栈又不一定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, ))
{
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

.net core编写转发服务的更多相关文章

  1. .net core编写转发服务(二) 添加服务发布订阅

    源设计就单纯完成了把服务转发到特定的服务模块,一定程度上解耦了业务流程 但是我们实际开发过程中会面临服务转发后还有一些列关联的服务 举个例子 你调用了发送邮件的服务,接下来会面临扣费的服务,扣费之后会 ...

  2. .net core编写转发服务(三) 接入Polly

    在web服务里面,很常见出现各种问题,需要一些响应的策略,比如服务繁忙的时候,重试,或者重试等待 服务繁忙的时候根据策略即使处理 关于接入Polly我还是沿用之前的代码,继续迭代 Web Api用的是 ...

  3. .Net Core with 微服务 - Ocelot 网关

    上一次我们通过一张架构图(.Net Core with 微服务 - 架构图)来讲述了微服务的结构,分层等内容.从现在开始我们开始慢慢搭建一个最简单的微服务架构.这次我们先用几个简单的 web api ...

  4. .Net Core with 微服务 - Consul 注册中心

    上一次我们介绍了 Ocelot 网关的基本用法.这次我们开始介绍服务注册发现组件 Consul 的简单使用方法. 服务注册发现 首先先让我们回顾下服务注册发现的概念. 在实施微服务之后,我们的调用都变 ...

  5. 使用AWS亚马逊云搭建Gmail转发服务(三)

    title: 使用AWS亚马逊云搭建Gmail转发服务(三) author:青南 date: 2015-01-02 15:42:22 categories: [Python] tags: [log,G ...

  6. 使用AWS亚马逊云搭建Gmail转发服务(二)

    title: 使用AWS亚马逊云搭建Gmail转发服务(二) author:青南 date: 2014-12-31 14:44:27 categories: [Python] tags: [Pytho ...

  7. 使用AWS亚马逊云搭建Gmail转发服务(一)

    title: 使用AWS亚马逊云搭建Gmail转发服务(一) author:青南 date: 2014-12-30 15:41:35 categories: Python tags: [Gmail,A ...

  8. C#码农的大数据之路 - 使用Ambari自动化安装HDP2.6(基于Ubuntu16.04)并运行.NET Core编写的MR作业

    准备主机 准备3台主机,名称作用如下: 昵称 Fully Qualified Domain Name IP 作用 Ubuntu-Parrot head1.parrot 192.168.9.126 Am ...

  9. 手把手教你使用spring cloud+dotnet core搭建微服务架构:服务治理(-)

    背景 公司去年开始使用dotnet core开发项目.公司的总体架构采用的是微服务,那时候由于对微服务的理解并不是太深,加上各种组件的不成熟,只是把项目的各个功能通过业务层面拆分,然后通过nginx代 ...

随机推荐

  1. vue重置data里的值

    this.$options.data() 这个可以获取原始的data值,this.$data 获取当前状态下的data,拷贝重新赋值一下就行了. Object.assign(this.$data, t ...

  2. 题解:2018级算法第三次上机 C3-Zexal的浩瀚星辰

    题目描述: 样例: 实现解释: 一道结合了火箭发射的贪心题目 知识点: 贪心,优先队列 题目分析: 根据题目描述可知,延迟后时间是正常推进的,也就是假设共有n个火箭,推迟k小时.则在到达k+1小时时, ...

  3. Win10 环境变量

    在你的环境变量前面加入下面的目录; 有奇效 %USERPROFILE%\AppData\Local\Microsoft\WindowsApps\

  4. 1-The next outbreak we're not ready

    When I was a kid, the disaster we worried about most was a nuclear war. [wen aɪ wəz ə kɪd]

  5. JVM 专题十四:本地方法接口

    1. 本地方法接口 2. 什么是本地方法? 简单来讲,一个Native Method就是一个Java调用非Java代码的接口.一个Native Method是这样一个java方法:该方法的实现由非Ja ...

  6. web 部署专题(一):Gunicorn运行与配置方法

    Gunicorn“绿色独角兽”是一个被广泛使用的高性能的Python WSGI UNIX HTTP服务器,移植自Ruby的独角兽(Unicorn )项目,使用pre-fork worker模式,具有使 ...

  7. Python Ethical Hacking - MODIFYING DATA IN HTTP LAYER(3)

    Recalculating Content-Length: #!/usr/bin/env python import re from netfilterqueue import NetfilterQu ...

  8. 【NET开发】图片处理类-仿照七牛云图片处理功能

    介绍 Sop.Common.Img **请看 七牛云的图片处理功能介绍文档了解本项目功能 ** 此项目是有https://github.com/Sopcce/.Net-Common-Utility中的 ...

  9. C++语法小记---前置操作符和后置操作符

    前置操作符和后置操作符 单独的"++i"和"i++"是否有区别 对于基本类型: 二者没有区别,因为编译器会对代码进行优化,二者的汇编代码完全相同 对于类类型: ...

  10. 因为喜欢所以升级,MyStaging-3.0 继续

    我为什么维护MyStaging 目前该项目只有我一个人在维护,权当学习交流.为什么要继续维护呢,说一千道一万,还是因为喜欢,由于他的简单易用,从而促使我决定对 MyStaging 进行升级,目前 3. ...