Developer forum

Forum » CMS - Standard features » UserAndGroupType API crashes with null reference exception

UserAndGroupType API crashes with null reference exception

Hans Kloppenborg
Reply

Hello,

Has anyone succesfully implemented the UserAndGroupType Api (Dynamicweb.Security.UserManagement.UserTypes)?

There are no examples in the documentation, and whichever api call I try to use it always leads to a null reference exception:

[NullReferenceException: Object reference not set to an instance of an object.]
   Dynamicweb.Core.SystemInformation.GetBasePath(String& relativePath) +271
   Dynamicweb.Core.SystemInformation.MapPath(String relativePath) +61
   Dynamicweb.Security.UserManagement.UserTypes.UserAndGroupTypeRepository..cctor() +60

 

The service is kind of weird also, if you instantiate a new instance it has no methods, and if you call a method on the type directly it leads to the same error:

[NullReferenceException: Object reference not set to an instance of an object.]
   Dynamicweb.Core.SystemInformation.GetBasePath(String& relativePath) +271
   Dynamicweb.Core.SystemInformation.MapPath(String relativePath) +61
   Dynamicweb.Security.UserManagement.UserTypes.UserAndGroupTypeRepository..cctor() +60

[TypeInitializationException: The type initializer for 'Dynamicweb.Security.UserManagement.UserTypes.UserAndGroupTypeRepository' threw an exception.]
   Dynamicweb.Security.UserManagement.UserTypes.UserAndGroupTypeRepository..ctor() +24
   Dynamicweb.Security.UserManagement.UserTypes.UserAndGroupTypeService..cctor() +48

[TypeInitializationException: The type initializer for 'Dynamicweb.Security.UserManagement.UserTypes.UserAndGroupTypeService' threw an exception.]

[HttpException (0x80004005): The type initializer for 'Dynamicweb.Security.UserManagement.UserTypes.UserAndGroupTypeService' threw an exception.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +521
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +185
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +418
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +369

[HttpException (0x80004005): The type initializer for 'Dynamicweb.Security.UserManagement.UserTypes.UserAndGroupTypeService' threw an exception.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +534
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +718

 

I have updated the Dynamicweb.Security package from 4.1.1 to the latest 4.3 version, but that made no difference.

Greets Hans

 

 

 


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

Hi Hans,

In what context are you using this API? It looks like the Dynamicweb application has not been initialized properly.
 

Votes for this answer: 1
 
Hans Kloppenborg
Reply

Hi Morten,

I am using it in a odata webapi call, in a virtual application under the DW-site, since DW refuses to work with odata webapi routing. It is running in the same application pool though.

Greets Hans

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply
This post has been marked as an answer

You can make your odata routing work within Dynamicweb. You just need to add a few notification subscribers to let Dynamicweb know about it :)

Here is some sample code for OData v4...

1) Create your odata controllers and models

Here is a basic sample that uses routing attributes:

[ODataRoutePrefix("UserAndGroupTypes")]
public class UserAndGroupTypesController : ODataController
{
    [EnableQuery()]
    [ODataRoute()]
    public IHttpActionResult Get()
    {
        return Ok(UserAndGroupTypeService.GetAll().AsQueryable());
    }
}

2) Map your odata routes on application start

[Subscribe(Standard.Application.BeforeDynamicwebStart)]
public class DynamicwebStartSubscriber : NotificationSubscriber
{
    public override void OnNotify(string notification, NotificationArgs args)
    {
        GlobalConfiguration.Configure(c => c.MapODataServiceRoute("CustomODataRoute""odata", GetEdmModel()));
    }
 
    private IEdmModel GetEdmModel()
    {
        ODataModelBuilder builder = new ODataConventionModelBuilder();
 
        // TODO: Replace this with your own definitions
        builder.EntitySet<UserAndGroupType>("UserAndGroupTypes").EntityType.HasKey(e => e.SystemName).Count().OrderBy().Filter().Select().Expand();
 
        return builder.GetEdmModel();
    }
}

3) Make sure that Dynamicweb doesn't handle URLs related to your odata services (404)

[Subscribe(Standard.Application.AuthenticateRequest)]
public class DynamicwebAuthenticateRequestSubscriber : NotificationSubscriber
{
    public override void OnNotify(string notification, NotificationArgs args)
    {
        var authenticateArgs = args as Standard.Application.AuthenticateRequestArgs;
        var request = authenticateArgs.Application.Context.Request;
        var isHandled = request.Path.StartsWith("/odata");
 
        // When this is set to true then Dynamicweb will skip the default handling of the current request
        authenticateArgs.Handled = isHandled;
    }
}

4) Make sure that advanced odata routes (functions etc.) which contains . (dot) is not treated as a static file by adding a handler entry in web.config

<configuration>
  ...
  <system.webServer>
    <handlers>
      <add name="ODataURIs" path="/odata/*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      ...
    </handlers>
    ...
  </system.webServer>
  ...
</configuration>

Best regards,
Morten

Votes for this answer: 1
 
Hans Kloppenborg
Reply

Hi Morten,

Thanks for your extensive answer, it solved my odata routing issue, real issue was in the end using older code in the Authenticate Request subscriber.

Greetings Hans

 

You must be logged in to post in the forum