下面在 Web 空应用里展示一个简单的例子来实现发送文本消息。

本文目录:

创建 Web 空应用

命令行方式创建

$ dotnet new web --name ASPNETCoreWeixinWorkDemo

dotnet 是程序的名字

new 是一个子程序的名字

web 是要使用的项目模板的名字

--name ASPNETCoreWeixinWorkDemo 指定要创建的项目的名字是 ASPNETCoreWeixinWorkDemo

添加SDK引用

命令行方式

进入项目目录
$ cd ASPNETCoreWeixinWorkDemo

添加包引用

$ dotnet add package Senparc.Weixin.Work

这个命令的执行效果可以在 WeixinWorkDemo.csproj 文件中看到。

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup> <ItemGroup>
<PackageReference Include="Senparc.Weixin.Work" Version="3.7.104.2" />
</ItemGroup> </Project>

配置和使用SDK

添加appsettings.Development.json文件

aappsettings.Development.json 文件一般用作 ASP.NET Core 项目的开发环境配置文件,在 ASP.NET Core 项目中通常可以看到。

文件内容如下,其中需要替换你自己的信息进去。

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"SenparcSetting": {
"IsDebug": true,
"DefaultCacheNamespace": "DefaultCache"
},
"SenparcWeixinSetting": {
"IsDebug": true,
"WeixinCorpId": "替换为你的企业微信企业ID",
"WeixinCorpAgentId": "替换为你的企业微信应用ID",
"WeixinCorpSecret": "替换为你的企业微信应用的Secret"
}
}

修改Startup.cs,配置服务

public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();//注册控制器
services.AddMemoryCache();//注册本地缓存
services.AddSenparcWeixinServices(Configuration);//注册全局微信服务
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<SenparcSetting> senparcSetting, IOptions<SenparcWeixinSetting> senparcWeixinSetting)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseRouting(); app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Hello World!"); });
endpoints.MapControllers();//设置路由匹配
}); app.UseSenparcGlobal(env, senparcSetting.Value, globalRegister => { })
.UseSenparcWeixin(senparcWeixinSetting.Value, weixinRegister =>
{
weixinRegister.RegisterWorkAccount(senparcWeixinSetting.Value, "替换为你的应用名字");//注册企业微信
});
}
}

添加Controller,在Get方法中发送消息

在项目下添加Controller目录,在目录下添加SendMessageController.cs,添加内容如下

using Microsoft.AspNetCore.Mvc;
using Senparc.Weixin;
using Senparc.Weixin.Work.AdvancedAPIs;
using Senparc.Weixin.Work.Containers; namespace ASPNETCoreWeixinWorkDemo.Controllers
{
[ApiController]
[Route("[controller]")]
public class SendMessageController : ControllerBase
{
static readonly string CorpId = Config.SenparcWeixinSetting.WorkSetting.WeixinCorpId;//通过全局对象获取配置
static readonly string CorpSecret = Config.SenparcWeixinSetting.WorkSetting.WeixinCorpSecret;//通过全局对象获取配置
static readonly string AppId = Config.SenparcWeixinSetting.WorkSetting.WeixinCorpAgentId;//通过全局对象获取配置
static readonly string AppKey = AccessTokenContainer.BuildingKey(CorpId, CorpSecret);//用于获取token的标识 // GET
[HttpGet]
public IActionResult Get()
{
// 通过标识获取 access token
var token = AccessTokenContainer.GetToken(AppKey); // 使用 SDK 的消息 API 发送文本信息
MassApi.SendText(token, AppId, "Hello World!", "替换为要发送的人员账号"); return Ok("Send Message To OK.");
}
}
}

然后在浏览器里访问 https://localhost:5001/SendMessage,就可以看到页面显示“Send Message To OK.”,在企业微信客户端里就可以看到“Hello World!”消息了。

【原创】在 ASP.NET Core 3.1 中使用 Senparc.Weixin.Work 企业微信 SDK —— 发送文本消息的更多相关文章

  1. 【原创】在 .NET Core 3.1 中使用 Senparc.Weixin.Work 企业微信 SDK —— 发送文本消息

    下面在控制台应用里展示一个简单的例子来实现发送文本消息. 本文目录: 创建控制台应用 添加SDK引用 命令行方式 进入项目目录 添加包引用 配置和使用SDK 添加appsettings.json文件 ...

  2. ASP.NET Core HTTP 管道中的那些事儿

    前言 马上2016年就要过去了,时间可是真快啊. 上次写完 Identity 系列之后,反响还不错,所以本来打算写一个 ASP.NET Core 中间件系列的,但是中间遇到了很多事情.首先是 NPOI ...

  3. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  4. 在ASP.NET Core 1.0中如何发送邮件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...

  5. ASP.NET Core 1.0 中使用 Swagger 生成文档

    github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...

  6. 用ASP.NET Core 1.0中实现邮件发送功能

    准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试 ...

  7. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  8. 在ASP.NET Core 2.0中使用CookieAuthentication

    在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...

  9. 使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目

    今天,Visual Studio中没有内置工具来测试WEB API.使用浏览器,只能测试http GET请求.您需要使用Postman,SoapUI,Fiddler或Swagger等第三方工具来执行W ...

随机推荐

  1. $CF938G\ Shortest\ Path\ Queries$ 线段树分治+线性基

    正解:线段树分治+线性基 解题报告: 传送门$QwQ$ 考虑如果只有操作3,就这题嘛$QwQ$ 欧克然后现在考虑加上了操作一操作二 于是就线段树分治鸭 首先线段树叶子节点是询问嘛这个不用说$QwQ$. ...

  2. 【汇编】1.汇编环境的搭建:DOSBox的安装

    前言 DOSBox是一款在windows系统运行DOS程序的环境模拟器.可以解决在64位机中汇编程序编译调试等问题. 本文以 DOSBox 0.74 为例,汇编编译程序采用MASM6. 第一步下载相关 ...

  3. 基于Redis的分布式锁和Redlock算法

    1 前言 前面写了4篇Redis底层实现和工程架构相关文章,感兴趣的读者可以回顾一下: Redis面试热点之底层实现篇-1 Redis面试热点之底层实现篇-2 Redis面试热点之工程架构篇-1 Re ...

  4. dos2unix命令 – 将DOS格式的文本文件转换成UNIX格式

    今天做题的时候,出现了个很冷门的: 查找子目录src下所有后缀为.txt的文件执行dos2unix命令,把文件从Dos格式转换为Linux格式,正确的命令是:find src "*.txt& ...

  5. [AI开发]零代码分析视频结构化类应用结构设计

    视频结构化类应用涉及到的技术栈比较多,而且每种技术入门门槛都较高,比如视频接入存储.编解码.深度学习推理.rtmp流媒体等等.每个环节的水都非常深,单独拿出来可以写好几篇文章,如果没有个几年经验基本很 ...

  6. pom.xml引入依赖时顺序错误而编译异常

    java.lang.NoClassDefFoundError: Lorg/springframework/beans/factory/access/BeanFactoryReference; at j ...

  7. 深度学习论文翻译解析(六):MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Appliications

    论文标题:MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Appliications 论文作者:Andrew ...

  8. angular.foreach 循环方法

    angular循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” 尽量避免代码的冲突,最好不要jq angular 混用 var o ...

  9. ubuntu频繁死机--独立显卡问题

    问题:笔记本安装ubuntu时以及装好后有时会出现花屏.死机的问题,系统报错 *ERROR* UVD not responding, trying to reset the VCPU!!! *ERRO ...

  10. Spring Boot 入门(十一):集成 WebSocket, 实时显示系统日志

    以前面的博客为基础,最近一篇为Spring Boot 入门(十):集成Redis哨兵模式,实现Mybatis二级缓存.本篇博客主要介绍了Spring Boot集成 Web Socket进行日志的推送, ...