public static void AddCosXmlServer(this IServiceCollection services, IConfigurationRoot configuration) { services.Configure <CosXmlOptions>(options => { configuration.GetSection("CosXmlOptions").Bind(options); }); services.AddSingleton <CosXmlServer>(sp => { var options = sp.GetService <IOptions <CosXmlOptions> >()?.Value ?? throw new ArgumentNullException(nameof(CosXmlOptions)); var config = new CosXmlConfig.Builder() .SetAppid(options.Appid) .SetRegion(options.Region); QCloudCredentialProvider cosCredentialProvider = new Hunter.Tencent.COSSDK.TencentQCloudCredentialProvider(options.QCloudCredential.TmpSecretId, options.QCloudCredential.TmpSecretKey, options.QCloudCredential.DurationSecond); return(new CosXmlServer( config.Build(), cosCredentialProvider)); }); }