Teams Bot开发系列:Middleware

middleware是目前一些framework比较流行的概念,通常一个开发框架需要提供一些可扩展可定制化的功能。所以middleware这种pattern就很实用。

熟悉asp.net core的开发可能第一个想到的就是asp.net core的middleware,如下图:

Middleware

当一个http request进入到处理的pipeline后,先被一个个middleware嵌套的执行,完成后在一个个推出。如果我们需要一些定制化功能,比如想把每个request要做一个统计记录,那开发可以自己写个middleware,加入到这个pipeline里,这样任何一个request都会从这个middleware通过,middleware就可以对request进行统计分析。

我自己整理了一下bot framework的middleware,如下图:

Middleware

可以看到当 Adapter 把 TurnContext 创建好后,就会开始一个个调用middleware,每一个middleware会通过调用next()来触发下一个middleware,在middleware pipeline的终点是ActivityHandler的OnTurnAsync()方法。

如果一个middleware想要短路(Short circuiting)整个turn的处理,可以很简单的不调用next来达到这个目的。

下面我们先来看一下IMiddleware接口:

public interface IMiddleware
{Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default(CancellationToken));
}

一个最简单的middleware如下:

public class MyMiddleware : IMiddleware
{public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default){// some logicawait next(cancellationToken).ConfigureAwait(false);// more logic}
}

有了自己的middleware后,我们需要在我们自己的Adapter类的构造函数里,把middleware加入到middleware列表。

public class MyHandler : BotFrameworkHttpAdapter
{public MyHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger): base(configuration, logger){Use(new MyMiddleware());}
}

Middleware的调用顺序是如何的?

我们用代码来说明,创建两个middleware,A和B

public class MyHandler : BotFrameworkHttpAdapter
{public MyHandler(IConfiguration configuration, ILogger<BotFrameworkHttpAdapter> logger): base(configuration, logger){Use(new TestAMiddleware(logger));Use(new TestBMiddleware(logger));}
}public class TestAMiddleware : IMiddleware
{private readonly ILogger _logger;public TestAMiddleware(ILogger logger){_logger = logger;}public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default){_logger.LogInformation("Middleware A begin");await next(cancellationToken).ConfigureAwait(false);_logger.LogInformation("Middleware A end");}
}public class TestBMiddleware : IMiddleware
{private readonly ILogger _logger;public TestBMiddleware(ILogger logger){_logger = logger;}public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default){_logger.LogInformation("Middleware B begin");await next(cancellationToken).ConfigureAwait(false);_logger.LogInformation("Middleware B end");}
}

当在处理一个activity时,我们可以看到打印出来的log:

    Middleware A beginMiddleware B beginOnMessageActivityAsyncMiddleware B endMiddleware A end

现在各位看到这里就清楚了吧,先通过Use()加入的middleware将会先被调用到。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/277930.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何获取租户中所有的Team

大家在使用Graph API开发Teams App的时候&#xff0c;有时候会需要获取某个租户Tenant的所有team&#xff0c;在写这篇文章的时候Graph API并没有提供这么一个功能&#xff0c;没有一个类似于”GET /teams”的api。 在Micorsoft Graph官方文档的已知问题中&#xff0c;也提到了…

mysql常用快速查询修改操作

mysql常用快速查询修改操作 一、查找并修改非innodb引擎为innodb引擎 # 通用操作 mysql> select concat(alter table ,table_schema,.,table_name, engineinnodb;) from information_schema.tables where table_schema not in (information_schema,mysql,performance_schem…

ElasticSearch教程——自定义分词器(转学习使用)

一、分词器 Elasticsearch中&#xff0c;内置了很多分词器&#xff08;analyzers&#xff09;&#xff0c;例如standard&#xff08;标准分词器&#xff09;、english&#xff08;英文分词&#xff09;和chinese&#xff08;中文分词&#xff09;&#xff0c;默认是standard. s…

使用Azure Serverless来开发Teams App

Azure Function可以说比较早期的一个serverless服务&#xff0c;随着这些年云服务的大行其道&#xff0c;Serverless在概念越来越火&#xff0c;什么叫serverless&#xff1f; Serverless computing (or serverless for short), is an execution model where the cloud provide…

Angular之RouterModule的forRoot与forChild

Angular 提供了一种方式来把服务提供商从模块中分离出来&#xff0c;以便模块既可以带着 providers 被根模块导入&#xff0c;也可以不带 providers 被子模块导入。 区别&#xff1a; forRoot creates a module that contains all the directives, the given routes, and the r…

关于 someone could be eavesdropping on you right now (man-in-the-middle attack) ssh的解决办法

关于 someone could be eavesdropping on you right now (man-in-the-middle attack) ssh的解决办法 记录工作中遇到的问题 someone could be eavesdropping on you right now (man-in-the-middle attack) ssh  由于远程机器或者重组或者更新了ssh server导致本地记录的验证信…

使用AzureFunction开发最简单的Teams Outgoing Webhook

上篇文章讲了teams app的serverless架构&#xff0c;这篇主要讲如何真正使用Azure Function来开发一个最最简单的Teams Outgoing Webhook。 我们先登入azure的portal&#xff0c;创建一个azure function。我这里创建了一个名字叫outgoing-webhook的azure function。完成后如下…

Java 基础 之 标识符

www.verejava.com/?id1699254… /* 标识符的命名规则: 1. 是以字母&#xff0c;数字&#xff0c;下滑线_和美元符号$ 组成 2. 不能以数字开头 3. 区分大小写 4. 不能是java的保留关键字 5. 最好是见名思意 */ public class Identifier {public static void main(String[] args…

Ubuntu宿主机与VMware中其他系统虚拟机的互通

Ubuntu做宿主机&#xff0c;VMware中创建Windows10&#xff0c;并且通过三种模式实现两系统互通&#xff0c;其实并非是件难事。在有线网卡未接网线的环境下&#xff0c;关闭两系统防火墙&#xff0c;基本遵从下文便可实现。 转载&#xff1a;https://note.youdao.com/ynotesha…

使用Azure轻松实现Teams App的全球合规性

我在之前的一篇博客里面讲了合规性对于我们Teams app是非常重要的&#xff0c;因为office365平台就是面向全世界用户的&#xff0c;我们开发的teams app一旦发布后&#xff0c;立刻就会有各国各地区的用户来进行安装使用&#xff0c;所以符合用户所在地区的要求是非常重要的。 …

【php复习之】php创建数组的几种方式

1、array()函数 1.1无key值 $arrarray(1,2,3,4); 1.2键值对 $arrarray( name>myj,age>18,phone>1888888888);1.3空数组 $arrarray(); 2、compact()函数 compact函数可以把变量转换为数组。 $a aaa;$b bbb;$c ccc;$arr3 compact(a,b,c);输出&#xff1a;{"a&q…

ADC知识(2)——直流参数(输入电压参考,参考电流输入,积分非线性误差,差分非线性误差)...

目录 四、 输入参考电压范围 五、 参考电流 六、 非线性问题 差分非线性误差 积分非线性 四、 输入参考电压范围 大多数数据手册中&#xff0c;将它定义为一个特定的参考电压值&#xff0c;通常这个电压作为 此转换器最常用的参考电压。在参考输入电压…

LuckyDraw app使用CosmosDB的成本分析

我在以前的博客里说过我的LuckyDraw app在数据存储方面使用的是 Azure Table Storage&#xff0c;当时选择这个的原因是成本考虑&#xff0c;因为它实在是便宜&#xff0c;对于我这种个人开发维护的免费的teams app来说&#xff0c;成本是一个很重要的考量点。 当然&#xff0…

React 重温之 组件生命周期

生命周期 任何事物都不会凭空产生&#xff0c;也不会无故消亡。一个事物从产生到消亡经理的各个阶段&#xff0c;我们称之为 生命周期。 具体到我们的前端组件上来&#xff0c;一个组件的生命周期可以大体分为创建、更新、销毁这个三个阶段。 本文主要介绍React 的组件生命周期…

迁移聊天记录到Teams

有一些朋友问我teams是否支持将其他平台/系统里的聊天记录迁移某个channel里&#xff0c;答案是肯定的&#xff0c;teams团队在去年年中的时候就提供了这个功能。这个功能是通过graph api来完成的&#xff0c;我们今天就来看看如何迁移聊天记录到teams里。 首先&#xff0c;我…

leetcode-191-Number of 1 Bits

题目描述&#xff1a; Write a function that takes an unsigned integer and returns the number of 1 bits it has (also known as the Hamming weight). Example 1: Input: 11 Output: 3 Explanation: Integer 11 has binary representation 000000000000000000000000000010…

androidsdk里的android.bat和uiautomatorview.bat启动就闪退问题

进入D:\androidsdk\tools文件夹&#xff1a; 使用编辑文件工具&#xff1a; rem Check we have a valid Java.exe in the path.set java_execall lib\find_java.bat 替换成下列代码&#xff1a; rem Check we have a valid Java.exe in the path.set java_exeC:\Program Files\…

10 个优质的 Laravel 扩展推荐

这里有 10 个用来搭建 Laravel 应用的包 为何会创建这个包的列表&#xff1f;因为我是一个「比较懒」的开发者&#xff0c;在脸书上是多个 Laravel 小组的成员。平日遇到最多的问题就是开发是需要用那些包。我很懒所以我不想每次都从头开始搞这些东东。 为何此文没有包括管理包…

Teams AppId, InstallationId 和 ExternalId 的区别

大家如果看teams的 graph api 开发文档&#xff0c;可能会把 app id, installation id 和 external id 搞混&#xff0c;我自己一开始的时候就有点被搞晕了&#xff0c;再加上app manifest里面的 id 和 bot id&#xff0c;基本就彻底晕掉了。 那我们今天这篇文章就来讲讲这几种…

osi参考模型(开放系统互连参考模型)

自互联网诞生以来&#xff0c;随着网络飞速发展&#xff0c;用户迫切要求能在不同体系结构的网络空间交换信息&#xff0c;使得不同的网络能够互联起来。 国际化标准组织&#xff08;International Organization for Standardization&#xff0c;即ISO&#xff09;从1977年开始…