▌ About the Author

Su Zhenwei, cloud development Linker plan members, “micro channel development depth analysis” author, Senparc.Weixin micro channel SDK author, Microsoft most valuable expert (MVP), Sheng Pai network founder and chief architect, Microsoft Ignite technology conference lecturer, engaged in software and Internet research and development for 26 years, Launched China’s first HTML5 commercial application, presided over the architecture, development and integration of many large projects, has in-depth practice and understanding of Internet of Things, big data and artificial intelligence related technologies, has provided solutions and technical support for more than 30 Fortune 500 enterprises.

I have been paying attention to wechat ecological development and cloud development, and saw that many developers need server SDK, so I added related capabilities to my SDK to help more developers develop better applications

▌ about the SDK

Senparc.Weixin SDK is the wechat C#/.NET SDK with the highest utilization rate at present, including wechat public account, small program, open platform, wechat pay, enterprise wechat, wechat hardware platform and almost a full set of interfaces. Senparc.Weixin SDK is also the first Chinese.NET open source project on GitHub. Project compatible with multiple versions simultaneously. NET Framework,.NET Standard, and.NET Core, with all the elements of enterprise-level system integration: Distributed cache policy, log system, AccessToken lifecycle management module, message context module, message queue module, encryption and decryption algorithm module, and a full set of synchronous and asynchronous methods are supported.

▌ How to use it – Getting started

Development projects using the Senparc.Weixin SDK can refer to the existing official Demo, which includes most of the commonly used features and supports different.NET versions github.com/JeffreySu/W…

The following uses the small program developed by.NET Core as an example to develop a project that can call the cloud function interface of Tencent Cloud.

1. Initialize a project

Create an ASP.NET Core project using Visual Studio or Visual Studio Code and configure the basic information for the project

Select the specific project template

When you see this, you have completed the initialization of the project

2. Install the SDK

Click on the wechatCloudFunction. Web project and choose Manage Nuget package.

In the Nuget administration window, under the Browse TAB, search for senparc.weixin.wxopen

Note: if you need to develop a public account, search senparc.weixin.mp. The following operations take a small program as an example. Only the namespace is different, and the development process of corresponding functions is completely the same for the public account (MP) and enterprise wechat (Work).

3. Configure the Senparc.Weixin framework

There are two basic configuration steps between using the Senparc.Weixin SDK.

In addition to using the following manual configuration method, you can also refer to the existing Demo to directly copy the corresponding code: https://url.cn/5vUHVMF.

3.1 configuration appsetting. Json

Append the following to appsetting.json:

/ / CO2NET Settings
  "SenparcSetting": {
    "IsDebug": true."DefaultCacheNamespace": "DefaultCache".// Distributed cache
    "Cache_Redis_Configuration": "#{Cache_Redis_Configuration}#"./ / Redis configuration
    "Cache_Memcached_Configuration": "#{Cache_Memcached_Configuration}#"./ / the Memcached configuration
    "SenparcUnionAgentKey": "#{SenparcUnionAgentKey}#"
  },
  / / Senparc. Weixin SDK Settings
  "SenparcWeixinSetting": {
    // Wechat global
    "IsDebug": true./ / small programs
    "WxOpenAppId": "#{WxOpenAppId}#"."WxOpenAppSecret": "#{WxOpenAppSecret}#"."WxOpenToken": "#{WxOpenToken}#"."WxOpenEncodingAESKey": "#{WxOpenEncodingAESKey}#"
    }
Copy the code

Where, the string values of WxOpenAppId and WxOpenAppSecret (including #{}# placeholders) are replaced with the values of the applet background, for example, “#{WxOpenAppId}#” is replaced with: “wx12b4F63276b14d4c”.

WxOpenToken and WxOpenEncodingAESKey correspond to the Token and EncodingAESKey parameters for dialog message management, which can be ignored if only advanced interfaces are developed.

3.2 configuration startup. Cs

Append code to the ConfigureServices() method (you can use the automatic prompt using namespace) :

services.AddSenparcWeixinServices(Configuration);
/ / Senparc. Weixin registration
Copy the code

Complete method code

In Configure () method to add two parameters, introducing the appsettings automatically. The configuration of the json: IOptionssenparcSetting, IOptionssenparcWeixinSetting, complete code:

Append code to the end of the method body:

app.UseSenparcGlobal(env, senparcSetting.Value, null.true)
.UseSenparcWeixin(senparcWeixinSetting.Value,
weixinRegister =>
{
    weixinRegister.RegisterWxOpenAccount(senparcWeixinSetting.Value,
"[Cloud function] applets");
});
Copy the code

The first line of code is to configure enabling CO2NET (a basic library of Senparc.Weixin) global configuration, the second line of code to configure the Senparc.Weixin SDK and applets parameters.

Complete code:

Add the above three pieces of code to startup.cs and you’re ready to use all of wechat’s advanced interfaces.

4. Call the cloud function developed by the applets cloud

The advanced interface can be started anywhere, and we’ve placed it on the home page for demonstration purposes.

Open the Controllers/HomeController. Cs, create a new Action method Api (), add the following code, can be called cloud function related interface:

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));
Copy the code

Where, wxOpenSetting is a full set of applet configuration parameters automatically configured globally by the code in startup.cs, and evnId is the environment ID of the cloud function, which can be obtained in the following ways:

Open the applets development tool and click the “Cloud Development” button

Click the “Settings” button in the new window

You can view Environment ID in the upper right corner.

Complete code:

After starting the project, you see the default home page:

/Home/Api = /Home/Api

▌ other

Demo project address: github.com/Senparc/WeC…

▌ About Project Linker

Linker project, launched by cloud development, is an open source coordination and cooperation plan for open source authors and open source teams, aiming to build a good open source ecosystem of cloud development and provide [cloud developers] with more convenient open source SDK, cases or adaptation software.

Linker initiative encourages open source projects to provide cloud development versions or built-in cloud development support, which will help partner open source projects grow. If you are interested, send information about your open source projects and how you would like to work with cloud development to [email protected]

There are currently two community SDKS for cloud development in different languages: Golang and C#. What other language SDKS do you expect?

The source address

Github.com/TencentClou…


If you want to know more about the cloud development of CloudBase related technical stories/technical combat experience, please scan the code to follow [Tencent cloud development] public account ~

About Cloud Development

CloudBase is a product solution of cloud integration. It adopts serverless architecture, eliminates operation and maintenance transactions such as environment construction, supports one cloud and multiple terminals, and helps to quickly build small programs, Web applications and mobile applications.

  • Technical documentation: www.cloudbase.net/
  • Wechat search: Tencent Cloud Development, get the latest progress of the project