question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ServiceAuthenticationBehavior throws NullReferenceException in GetAuthorizationService if new:ed up to be applied to one specific service only

See original GitHub issue

I’m trying to apply a ServiceAuthenticationBehavior on a specific ServiceHostBase only. I do this by:

serviceBuilder.ConfigureAllServiceHostBase(serviceHost =>
{
    if (serviceHost.Description.ServiceType == typeof(MyCustomService))
    {
        serviceHost.Description.Behaviors.Remove<ServiceAuthorizationBehavior>(); // The shared Singleton instance
        serviceHost.Description.Behaviors.Add(new ServiceAuthorizationBehavior
        {
            ExternalAuthorizationPolicies = new ReadOnlyCollection<IAuthorizationPolicy>(new List<IAuthorizationPolicy> { new MyCustomAuthorizationPolicy() }),
            PrincipalPermissionMode = PrincipalPermissionMode.Custom,
        });
    }
}

However, when I do so, a NullReferenceException is thrown:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=CoreWCF.Primitives
  StackTrace:
   at CoreWCF.Description.ServiceAuthorizationBehavior.CoreWCF.Description.IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase) in /_/src/CoreWCF.Primitives/src/CoreWCF/Description/ServiceAuthorizationBehavior.cs:line 216

This code is called from DispatchBuilder.InitializeServiceHost.

It appears to be because the internal setter property ServiceScopeFactory is never assigned a value, so it remains null.

Only workaround I’ve found is to set it through reflection, i.e:

typeof(ServiceAuthorizationBehavior)
    .GetProperty("ServiceScopeFactory", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
    .SetValue(tempServiceAuthorizationBehavior, app.ApplicationServices.GetService<IServiceScopeFactory>());

But this can’t be right. Is there a better way of doing this or have I stumbled upon a bug or unsupported scenario?

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
g7ed6ecommented, Jun 24, 2023

@ksjoberg @allderidge please have a look to the test of the linked PR to get the expected behavior.

0reactions
allderidgecommented, Jun 19, 2023

I have the same issue. I’ve worked around using refection as above as in my case there are many ServiceAuthorizationBehavior’s with configuration dependent on the service.

So inside the code: builder.ConfigureServiceHostBase(service.Type, serviceHost =>

The behaviour is replaced via:

serviceHost.Description.Behaviors.RemoveAll<ServiceAuthorizationBehavior>();
var replace = new ServiceAuthorizationBehavior
{
    PrincipalPermissionMode = PrincipalPermissionMode.Custom,
    ExternalAuthorizationPolicies =
        ... policies that are dependent on service.Type ...
};
replace.GetType().GetProperty("ServiceScopeFactory", BindingFlags.Instance | BindingFlags.NonPublic)!
    .SetValue(replace, app.ApplicationServices.GetService<IServiceScopeFactory>());
serviceHost.Description.Behaviors.Add(replace);

Changing the service registration to make the behaviour transient using the following code:

            var registration = builder.Services.First(e => e.ServiceType == typeof(ServiceAuthorizationBehavior));
            builder.Services.Remove(registration);
            builder.Services.AddTransient<ServiceAuthorizationBehavior>();

Triggers the same null ref exception.

As far as I can tell there isn’t a way of fixing this by altering the setup of the app services.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reasons to throw a NullReferenceException explicitly?
InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments.
Read more >
How to Fix NullReferenceException in C#! (4 Step Process)
Let's learn what is a NullReferenceException, what causes it and a Step-by-Step process for how to Fix NullReferenceException in C#!
Read more >
c# - Determining the object that caused a null reference ...
To prevent an X/Y problem, the goal is to simply record what object causes a null reference exception to be thrown, since the...
Read more >
How can I fix the error: System.NullReferenceException
NullReferenceException : 'Object reference not set to an instance of an object.' Doesn't make sense to me cause the statement under it is...
Read more >
An unconventional way of investigating a ...
The crash. This one started when trying to understand why an integration test was failing, only on Linux with ARM64.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found