How to call cloud development platform in C #?

▌ About the Author

Su Zhenwei, cloud development Linker plan members, "the development of micro-channel depth analysis" author, Senparc.Weixin of micro-channel SDK, Microsoft Most Valuable Professional (MVP), Sheng school network founder and chief architect, Microsoft Ignite Technology Conference lecturer, engaged in software and Internet research for 26 years, issued China's first commercial application of HTML5, presided over the architecture, the development of many large-scale projects and integrated, in-depth and practical understanding of networking, big data and artificial intelligence technologies, has been to More than 30 Fortune 500 companies to provide solutions and technical support.

I have been concerned about the ecological development of micro-channel field and cloud development, developers need to see a lot of server-side SDK, on ​​the server calls a cloud database development and function, he joined the related capacity in my own SDK, helping more and more developers build better applications

About SDK ▌

Senparc.Weixin SDK is currently the highest utilization rate of the micro-channel C # /. NET SDK, including the micro-channel public number, applets, open platform, micro-channel pay, corporate micro-channel, micro-channel hardware platform almost a full interface. Senparc.Weixin SDK is currently ranked first in China GitHub integrated .NET open source projects. The project is compatible with multiple versions of the .NET Framework, .NET Standard and .NET Core, has all the elements of enterprise-class systems integration: Support for distributed caching strategies degradation and disaster recovery hot-swappable, log system, AccessToken lifecycle management module, message context module, the message queue module, encryption and decryption algorithm module, and a full synchronous / asynchronous method.

▌ Tutorial - instructions to get started

Use Senparc.Weixin SDK development project can refer to the current official Demo, which includes most of the commonly used functions demo, and support different versions of .NET https://github.com/JeffreySu/WeiXinMPSDK/tree/master/Samples.

The following small program uses .NET Core development, for example, a project to develop cloud-function interface Tencent cloud can be called.

1. Initialize a project

使用 Visual Studio 或 Visual Studio Code 创建一个 ASP.NET Core 项目,并配置项目的基础信息

选择具体的项目模板

看到这里,就说明你完成了项目的初始化

2.安装 SDK

鼠标点击【WeChatCloudFunction.Web】项目右键,选择【管理Nuget程序包...】

在 Nuget 管理窗口内,【浏览】标签下,搜索:Senparc.Weixin.WxOpen

注意:如需开发公众账号,则搜索 Senparc.Weixin.MP,以下操作以小程序为例,仅命名空间不同,对应功能的开发过程对于公众号(MP)、企业微信(Work)是完全一致的,可以举一反三。

3.配置 Senparc.Weixin 框架

在使用 Senparc.Weixin SDK 之间,还有两步基础的配置工作。

除了使用以下手动配置的方式,也可以参考现成的 Demo 直接复制对应代码:https://url.cn/5vUHVMF。

3.1 配置 appsetting.json

在 appsetting.json 追加如下内容:

//CO2NET 设置
  "SenparcSetting": {
    "IsDebug": true,
    "DefaultCacheNamespace": "DefaultCache",

    //分布式缓存
    "Cache_Redis_Configuration": "#{Cache_Redis_Configuration}#", //Redis配置
    "Cache_Memcached_Configuration": "#{Cache_Memcached_Configuration}#", //Memcached配置
    "SenparcUnionAgentKey": "#{SenparcUnionAgentKey}#"
  },
  //Senparc.Weixin SDK 设置
  "SenparcWeixinSetting": {
    //微信全局
    "IsDebug": true,
    //小程序
    "WxOpenAppId": "#{WxOpenAppId}#",
    "WxOpenAppSecret": "#{WxOpenAppSecret}#",
    "WxOpenToken": "#{WxOpenToken}#",
    "WxOpenEncodingAESKey": "#{WxOpenEncodingAESKey}#"
    }

其中, WxOpenAppId 和 WxOpenAppSecret 的字符串值(包括#{}#占位符)替换为小程序后台的值,如,将"#{WxOpenAppId}#" 替换为:"wx12b4f63276b14d4c"。

WxOpenToken 和 WxOpenEncodingAESKey 对应于对话消息管理的 Token 和 EncodingAESKey 参数,如果仅开发高级接口,可以忽略。

3.2 配置 startup.cs

在 ConfigureServices() 方法中追加代码(可以使用自动提示 using 命名空间):

services.AddSenparcWeixinServices(Configuration);
//Senparc.Weixin 注册

完整方法代码

在 Configure() 方法中添加两个参数,自动引入 appsettings.json 中的配置:IOptionssenparcSetting, IOptionssenparcWeixinSetting,完整代码:

在方法体末尾追加代码:

app.UseSenparcGlobal(env, senparcSetting.Value, null, true)
.UseSenparcWeixin(senparcWeixinSetting.Value,
weixinRegister =>
{
    weixinRegister.RegisterWxOpenAccount(senparcWeixinSetting.Value,
"【云函数】小程序");
});

其中第一行代码是配置启用 CO2NET(Senparc.Weixin 的一个基础库)全局配置,第二行代码开始配置 Senparc.Weixin SDK 及小程序参数。

完整代码:

在 startup.cs 中添加以上3段代码,就可以开始使用所有微信的高级接口了。

4.调用小程序云开发的云函数

高级接口可以在任意地方出发,这里为了方便演示,我们将其放置在首页中。

打开 Controllers/HomeController.cs,创建一个新的 Action 方法 Api(),添加如下代码,即可调用云函数相关的接口:

var wxOpenSetting =
Senparc.Weixin.Config.SenparcWeixinSetting.WxOpenSetting;
var envId= "senparc-robot-5f5128";
var result = Senparc.Weixin.WxOpen.AdvancedAPIs.Tcb
              .TcbApi.DatabaseCollectionGet(wxOpenSetting.WxOpenAppId,
envId);
return Content(result.ToJson(true));

其中,wxOpenSetting 是通过 startup.cs 中代码自动进行了全局配置的全套小程序配置参数,evnId 是云函数的环境ID,可以通过以下方式获取到:

打开小程序开发工具,点击【云开发】按钮

在新窗口中点击【设置】按钮

即可在右上角看到“环境ID(Environment ID)”。

完整代码:

启动项目后,即可看到默认的首页:

访问新建的页面/Home/Api,即可看到成功获得到接口结果:

▌其他

演示项目地址:https://github.com/Senparc/WeChatCloudFunction/tree/master

▌关于 Linker 计划

Linker计划,是云开发推出的,面向开源作者和开源团队的开源协调合作计划,旨在共建良好的云开发开源生态,为【云开发者】提供更多便利好用的开源SDK、案例或适配软件。

Linker计划,鼓励开源项目提供云开发版本或内建云开发支持,云开发将助力合作开源项目成长。如果有兴趣,可以发送您的开源项目、希望和云开发的合作方式等信息到 [email protected]

云开发目前已有两个不同语言的社区 SDK:Golang 和 C#, 除了这两门语言,你还期待什么语言 SDK 呢?

源码地址

https://github.com/TencentCloudBase/Good-practice-tutorial-recommended


如果你想要了解更多关于云开发CloudBase相关的技术故事/技术实战经验,请扫码关注【腾讯云云开发】公众号~

Guess you like

Origin www.cnblogs.com/CloudBase/p/12123640.html