Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.net7没有Startup注册报错:Object reference not set to an instance of an object. #2898

Closed
3 tasks done
pcvc opened this issue Aug 8, 2023 · 5 comments
Closed
3 tasks done

Comments

@pcvc
Copy link

pcvc commented Aug 8, 2023

var builder = WebApplication.CreateBuilder(args);

// .......

var app = builder.Build();

var _senparcSetting = app.Services.GetService<IOptions>()!.Value;
var _senparcWeixinSetting = app.Services.GetService<IOptions>()!.Value;

//由下面这句引起的:
app.UseSenparcGlobal(app.Environment, _senparcSetting, globalRegister => { })
.UseSenparcWeixin(_senparcWeixinSetting, _senparcSetting)
.RegisterWxOpenAccount(_senparcWeixinSetting);

/////////////////////

app.UseSenparcWeixin(app.Environment,
null /* 不为 null 则覆盖 appsettings 中的 SenpacSetting 配置*/,
null /* 不为 null 则覆盖 appsettings 中的 SenpacWeixinSetting 配置*/,
register => { },
(register, weixinSetting) =>
{
//注册公众号信息(可以执行多次,注册多个小程序)
register.RegisterWxOpenAccount(weixinSetting, "【盛派网络小助手】小程序");
});

用以上这句也是同样。

Unhandled exception. System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.)
---> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.Extensions.Caching.Memory.CacheExtensions.Get(IMemoryCache cache, Object key)
at Senparc.CO2NET.Cache.LocalObjectCacheStrategy.CheckExisted(String key, Boolean isFullKey)
at Senparc.CO2NET.Cache.LocalObjectCacheStrategy.Set(String key, Object value, Nullable1 expiry, Boolean isFullKey) at Senparc.CO2NET.Cache.LocalObjectCacheStrategy.Update(String key, Object value, Nullable1 expiry, Boolean isFullKey)
at Senparc.CO2NET.Cache.LocalObjectCacheStrategy.<>c__DisplayClass21_0.b__0()
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
at Senparc.CO2NET.Cache.LocalObjectCacheStrategy.UpdateAsync(String key, Object value, Nullable1 expiry, Boolean isFullKey) at Senparc.Weixin.Containers.BaseContainer1.UpdateAsync(String shortKey, TBag bag, Nullable1 expiry) at Senparc.Weixin.WxOpen.Containers.AccessTokenContainer.<>c__DisplayClass5_0.<<RegisterAsync>b__0>d.MoveNext() --- End of stack trace from previous location --- at Senparc.Weixin.WxOpen.Containers.AccessTokenContainer.RegisterAsync(String wxOpenAppId, String wxOpenAppSecret, String name) --- End of inner exception stack trace --- at System.Threading.Tasks.Task.WaitAllCore(Task[] tasks, Int32 millisecondsTimeout, CancellationToken cancellationToken) at System.Threading.Tasks.Task.WaitAll(Task[] tasks, Int32 millisecondsTimeout) at Senparc.Weixin.WxOpen.Containers.AccessTokenContainer.Register(String wxOpenAppId, String wxOpenAppSecret, String name) at Senparc.Weixin.WxOpen.Register.RegisterWxOpenAccount(IRegisterService registerService, ISenparcWeixinSettingForWxOpen weixinSettingForWxOpen, String name) at EngTSY.Program.<>c.<Main>b__0_5(IRegisterService register, SenparcWeixinSetting weixinSetting) in D:\workspaces\projects\EngTSY\EngTSY\Program.cs:line 130 at Senparc.Weixin.WeixinRegister.UseSenparcWeixin(IRegisterService registerService, SenparcWeixinSetting senparcWeixinSetting, Action2 registerConfigure, IServiceProvider serviceProvider)
at Senparc.Weixin.AspNet.WeixinRegister.UseSenparcWeixin(IApplicationBuilder app, IHostEnvironment env, SenparcSetting senparcSetting, SenparcWeixinSetting senparcWeixinSetting, Action1 globalRegisterConfigure, Action2 weixinRegisterConfigure, Boolean autoScanExtensionCacheStrategies, Func`1 extensionCacheStrategiesFunc)

模块对应的 .net 版本
  • .net 7.0
开发环境
  • Visual Studio 2022
缓存环境
  • 服务器内存缓存(默认)
@pcvc
Copy link
Author

pcvc commented Aug 8, 2023

builder.Services.AddSenparcGlobalServices(builder.Configuration) // Senparc.CO2NET 全局注册
.AddSenparcWeixinServices(builder.Configuration); // Senparc.Weixin 注册

var app = builder.Build();

@pcvc
Copy link
Author

pcvc commented Aug 8, 2023

已自行解决:

builder.Services
.AddMemoryCache()//使用本地缓存
.AddSenparcGlobalServices(builder.Configuration)
.AddSenparcWeixinServices(builder.Configuration)//Senparc.Weixin 注册
.AddOptions();

@pcvc pcvc closed this as completed Aug 8, 2023
@leuoson-coding
Copy link

我也是用的.NET7,请问一下 使用 app.UseSenparcWeixin 这个方法需要引用哪一个命名空间?

@pcvc
Copy link
Author

pcvc commented Aug 12, 2023

位于程序集 Senparc.Weixin, Version=6.16.3.2, Culture=neutral, PublicKeyToken=null�。
命名空间:Senparc.Weixin�

@Schreber
Copy link

我也用.NET7,请问一下使用app.UseSenparcWeixin这个方法需要引用哪一个命名空间?

Senparc.Weixin.AspNet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants