Updated from 4.0.60 to 4.0.62

mywebsite Works fine in 4.0.60, after upgrade to 4.0.62. It generate some errors below, how to solve it? :

============================================ 
2016-08-10 08:53:31.7065 【ERROR】【ServiceStackHost】 ServiceStack.ServiceStackHost.OnLogError ServiceBase::Service Exception 未将对象引用设置到对象的实例。 System.NullReferenceException NullReferenceException System.NullReferenceException: 未将对象引用设置到对象的实例。
在 ServiceStack.Auth.ApiKeyAuthProvider.Register(IAppHost appHost, AuthFeature feature)
在 ServiceStack.AuthFeature.<>c__DisplayClassd.b__c(IAuthPlugin x)
在 ServiceStack.EnumerableExtensions.Each[T](IEnumerable`1 values, Action`1 action)
在 ServiceStack.AuthFeature.Register(IAppHost appHost)
在 ServiceStack.ServiceStackHost.LoadPluginsInternal(IPlugin[] plugins) Void Register(ServiceStack.IAppHost, ServiceStack.AuthFeature) 在 ServiceStack.Auth.ApiKeyAuthProvider.Register(IAppHost appHost, AuthFeature feature)
在 ServiceStack.AuthFeature.<>c__DisplayClassd.b__c(IAuthPlugin x)
在 ServiceStack.EnumerableExtensions.Each[T](IEnumerable`1 values, Action`1 action)
在 ServiceStack.AuthFeature.Register(IAppHost appHost)
在 ServiceStack.ServiceStackHost.LoadPluginsInternal(IPlugin[] plugins) 
============================================ 
============================================ 
2016-08-10 08:53:31.8175 【ERROR】【ServiceStackHost】 ServiceStack.ServiceStackHost.LogInitComplete Initializing Application 微信网站DEMO took 767.0439ms. 1 error(s) detected: [{"ErrorCode":"NullReferenceException","Message":"未将对象引用设置到对象的实例。","StackTrace":"[Object: 2016/8/10 0:53:31]:\n[REQUEST: ]\nSystem.NullReferenceException: 未将对象引用设置到对象的实例。\r\n 在 ServiceStack.Auth.ApiKeyAuthProvider.Register(IAppHost appHost, AuthFeature feature)\r\n 在 ServiceStack.AuthFeature.<>c__DisplayClassd.b__c(IAuthPlugin x)\r\n 在 ServiceStack.EnumerableExtensions.Each[T](IEnumerable`1 values, Action`1 action)\r\n 在 ServiceStack.AuthFeature.Register(IAppHost appHost)\r\n 在 ServiceStack.ServiceStackHost.LoadPluginsInternal(IPlugin[] plugins)","Errors":[]}] 
============================================ 

my Auth initialize code

   public void RegisterAuth(Container container)
    {
        //var appSettings = new AppSettings();
        //1. Registering the authorization provider.
        Plugins.Add(new AuthFeature(() => new CustomUserSession(),
          new IAuthProvider[]
          {
              new BasicAuthProvider(AppSettings) {PersistSession = true},    //Sign-in with Basic Auth
              new CredentialsAuthProvider(AppSettings) {SkipPasswordVerificationForInProcessRequests = true},
              new ApiKeyAuthProvider(AppSettings) {RequireSecureConnection =false}, 
              new JwtAuthProvider(AppSettings) {
                  RequireSecureConnection =false,  AuthKey = AesUtils.CreateKey() },
          })
    {
        AuthEvents = new List<IAuthEvents> { new LogAuthEvents() },
    });
   }

Not sure what the issue is, the ApiKeyAuthProvider wasn’t untouched.

Can you try commenting out new ApiKeyAuthProvider(AppSettings) to see if that’s the issue, if it is, can you try commenting out AuthEvents = new List<IAuthEvents> { new LogAuthEvents() } instead to see if that’s somehow causing it?

if I commenting out new ApiKeyAuthProvider(AppSettings) {RequireSecureConnection =false}, It works fine.

commenting out AuthEvents = new List { new LogAuthEvents() }, It was no affect.

Then I’m assuming the NullReferenceException must happen when throwing the Exception.

You need to register an IAuthRepository.

How to Register an IAuthProvider ?
I Have IUserAuthRepository registered before IAuthProvider

        var dbFactory = AppService.RegisterDbConnectionFactory();
        container.Register(dbFactory); //默认的

        var ormLiteRepository = new OrmLiteAuthRepository<WxUserAuth, UserAuthDetails>(dbFactory)  //changed by wenzhili 2016年6月7日09时05分39秒
        {
            UseDistinctRoleTables = ConfigHelper.GetAppSetting("UseDistinctRoleTables", true),
        };
        //Registering the repository.
        container.Register<IUserAuthRepository>(ormLiteRepository);

        //var appSettings = new AppSettings();
        //1. Registering the authorization provider.
        Plugins.Add(new AuthFeature(() => new CustomUserSession()...

Sorry I meant IAuthRepository.

Change this to:

container.Register<IAuthRepository>(ormLiteRepository);

Thanks. after I follow you guide to change the code, It works.

1 Like