日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

.net6?使用Senparc開發小程序配置過程_實用技巧

作者:康Sir7 ? 更新時間: 2022-09-06 編程語言

1.添加引用

2.添加配置文件

/// <summary>
    /// 微信
    /// </summary>
    public class WeChat
    {
        // 小程序
        public string WxOpenAppId { get; set; }
        public string WxOpenAppSecret { get; set; }
        //// 微信支付
        //public string TenPayV3_AppId { get; set; }
        //public string TenPayV3_AppSecret { get; set; }
        //public string TenPayV3_MchId { get; set; }
        //public string TenPayV3_Key { get; set; }
        //public string TenPayV3_CertPath { get; set; }
        //public string TenPayV3_CertSecret { get; set; }
        //public string TenPayV3_TenpayNotify { get; set; }
        //public string TenPayV3_RechargeTenpayNotify { get; set; }
        //public string TenPayV3_RefundNotify { get; set; }
        //public string TenPayV3_RechargeRefundNotify { get; set; }
        //public string TenPayV3_WxOpenTenpayNotify { get; set; }
        /// <summary>
        /// 是否是開發環境
        /// </summary>
        public string IsDevelopment { get; set; }
    }

3.在appsetting.jso里面配置參數

 "WeChat": { 
    // 小程序
    "WxOpenAppId": "",
    "WxOpenAppSecret": ""
    ////微信支付
    //"TenPayV3_AppId": "", //商戶平臺綁定了微信小程序就可以直接用這個
    //"TenPayV3_AppSecret": "", //商戶平臺綁定了微信小程序就可以直接用這個
    //"TenPayV3_MchId": "", //
    //"TenPayV3_Key": "", //
    //"TenPayV3_CertPath": "", //(新)支付證書物理路徑,如:D:\\cert\\apiclient_cert.p12
    //"TenPayV3_CertSecret": "", //(新)支付證書密碼(原始密碼和 MchId 相同)
    //"TenPayV3_TenpayNotify": "https://river.runtoinfo.com/agr/api/TenPayV3/PayNotifyUrl",
    ////http://YourDomainName/TenpayV3/PayNotifyUrl
    ////如果不設置TenPayV3_WxOpenTenpayNotify,默認在 TenPayV3_TenpayNotify 的值最后加上 "WxOpen"
    //"TenPayV3_WxOpenTenpayNotify": "https://river.runtoinfo.com/agr/api/TenPayV3/PayNotifyUrlWxOpen" //http://YourDomainName/TenpayV3/PayNotifyUrlWxOpen
  }

4.配置program

builder.Services.AddMemoryCache();

builder.Services.AddMemoryCache();
//微信
builder.Services.AddSenparcGlobalServices(builder.Configuration)//Senparc.CO2NET 全局注冊
                   .AddSenparcWeixinServices(builder.Configuration);//Senparc.Weixin 注冊
                                                                    //添加 微信支付api證書
                                                                    //var mac_id = builder.Configuration["WeChat:TenPayV3_MchId"];
                                                                    //builder.Services.AddCertHttpClient(mac_id + "_", mac_id, AppDomain.CurrentDomain.BaseDirectory + "/wwwroot/" + builder.Configuration["WeChat:TenPayV3_CertPath"]);
var senparcSetting = builder.Services.BuildServiceProvider().GetRequiredService<IOptions<SenparcSetting>>();
IRegisterService register = RegisterService.Start(senparcSetting.Value).UseSenparcGlobal();// 啟動 CO2NET 全局注冊,必須!
var senparcWeixinSetting = builder.Services.BuildServiceProvider().GetRequiredService<IOptions<SenparcWeixinSetting>>();
//register.UseSenparcWeixin(senparcWeixinSetting.Value, senparcSetting.Value);//微信全局注冊,必須!
register.UseSenparcWeixin(senparcWeixinSetting.Value, weixinRegister =>
{
    #region 微信相關配置
    /* 微信配置開始
    * 
    * 建議按照以下順序進行注冊,尤其須將緩存放在第一位!
    */
    #region 微信緩存(按需,必須放在配置開頭,以確保其他可能依賴到緩存的注冊過程使用正確的配置)
    //注意:如果使用非本地緩存,而不執行本塊注冊代碼,將會收到“當前擴展緩存策略沒有進行注冊”的異常
    // DPBMARK_END
    // 微信的 Memcached 緩存,如果不使用則注釋掉(開啟前必須保證配置有效,否則會拋錯)    -- DPBMARK Memcached
    //if (UseMemcached(senparcSetting.Value, out _))
    //{
    //    app.UseEnyimMemcached();
    //    weixinRegister.UseSenparcWeixinCacheMemcached();
    //}                                                                                     // DPBMARK_END
    #endregion
    /* 微信配置結束 */
    #endregion
});//微信全局注冊,必須!
//注冊 Token容器 應用憑證
var con = AccessTokenContainer.RegisterAsync(builder.Configuration["WeChat:WxOpenAppId"], builder.Configuration["WeChat:WxOpenAppSecret"]);
con.Wait();

原文鏈接:https://www.cnblogs.com/kangsir7/archive/2022/07/14/16476463.html

欄目分類
最近更新