ServiceAuthenticationBehavior throws NullReferenceException in GetAuthorizationService if new:ed up to be applied to one specific service only
See original GitHub issueI’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:
- Created 3 months ago
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ksjoberg @allderidge please have a look to the test of the linked PR to get the expected behavior.
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:
Changing the service registration to make the behaviour transient using the following code:
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.