Developer forum

Forum » Development » Custom API - api enabler stop working after test controller added

Custom API - api enabler stop working after test controller added

Mateusz Struzik
Reply

Hi 

I am trying to switch on custom api for dw 9.16.6. I added api enabler as it is recomended with some logs:

 

[Subscribe(Standard.Application.AuthenticateRequest)]
public class ApiEnabler : NotificationSubscriber
{
public override void OnNotify(string notification, NotificationArgs args)
{
logger.Log("Api Enabler before args");
 
if (!(args is Standard.Application.AuthenticateRequestArgs authenticateArgs)) return;
 
logger.Log("Api Enabler after args");
 
var requestPath = HttpContext.Current.Request.Path;
var isApiRequest = requestPath.StartsWith($"/{RoutePrefix}");
 
if (isApiRequest)
{
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
 
// When this is set to true then Dynamicweb will skip the default handling of the current request
authenticateArgs.Handled = true;
return;
}
}
 
And i can notice in monitor that it is working, but after i added simple controller it sudennly stop and api is also not working i am getting 404.:
[RoutePrefix("api/")]
public class TestController : ApiController
{
private readonly ILogger logger;
 
public TestController()
{
logger = LogManager.System.GetLogger(LogCategory.Monitoring, "Relekta Event Handlers");
}
 
[Route("test")]
[HttpPost]
public IHttpActionResult Test(int prefferencePageId)
{
logger.Log("test controller");
 
return Ok();
}
}
 
Does somone know what going on with this ?
 
Best regards 
Mateusz

Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I wonder how this even compiles. Take a look at this:

var isApiRequest = requestPath.StartsWith($"/{RoutePrefix}");
I don't see where RoutePrefix is defined. It's usually defined as a string constant in the same class holding the API path like so:
public const string RoutePrefix = "api";
Are you sure your code compiles and runs correctly?
 
Imar
 
Mateusz Struzik
Reply

Hi Imar 

You need to treat this code more like pseudo code and just sample that: i didn't put anything specific it just simple enabler and test controller. And if there were problems with code compile i will ask about this. Part of this code contains things that i cannot share that why quickly friday editing just to show that this is nothing fancy.

Also if it will not compile how i will know api enabler is working and later stop? 

So again my question is: did you have problem after added controller that api enabler stop working and api controller seems not be available to call or maybe you have some advise about this situation?  

Best regards sorry for confusion 

Mateusz 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Debugging pseudo code over a forum is pretty difficult so may I suggest you clean this up to a point where it contains sharable code and still has the issue?

My guess is that there’s a mismatch between the root URL for the enabler and the controller, or that the enabler somehow doesn't end up in your bin folder correctly.

>>did you have problem after added controller that api enabler stop working 

Nope, the whole point of the API enabler is to enable the API to work. So, with a clean and simple enable (like yours but then complete) and a controller using the same path it should just work.

Imar

 

 
Mateusz Struzik
Reply

Hi Imar 

Basically i am not trying any one to make my code debugging. mechanism is simple after added scheduler i can se in event viewer that request are coming throught it, after controller added not request are logged. it seems added controller some howe disabled subscriber it tis case api  enabler. 

Controller

using System.Web.Http;
namespace Degree.Relekta.Ecommerce.WebApi.Controller
{
public class DWTest : ApiController
{
}
}
 
SUBSCRIBER
using Dynamicweb.Extensibility.Notifications;
using Dynamicweb.Logging;
using Dynamicweb.Notifications;
using System.Web;
namespace Degree.Relekta.Ecommerce.WebApi
{
[Subscribe(Standard.Application.AuthenticateRequest)]
public class ApiEnabler : NotificationSubscriber
{
public const string RoutePrefix = "api/";
private readonly ILogger logger;
        public ApiEnabler()
        {
logger = LogManager.System.GetLogger(LogCategory.Monitoring, "Event Handlers");
}
public override void OnNotify(string notification, NotificationArgs args)
{
logger.Log("Api Enabler before args");
if (!(args is Standard.Application.AuthenticateRequestArgs authenticateArgs)) return;
logger.Log("Api Enabler after args");
var requestPath = HttpContext.Current.Request.Path;
var isApiRequest = requestPath.StartsWith($"/{RoutePrefix}");
if (isApiRequest)
{
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
// When this is set to true then Dynamicweb will skip the default handling of the current request
authenticateArgs.Handled = true;
return;
}
}
}
}
 
This logs are available before addidng api controller; 
logger.Log("Api Enabler before args");
logger.Log("Api Enabler after args");
 
Best regards 
Mateusz
 

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

I put your code in a local project of mine and it works just fine with and without the controller.

However your controller has not routes or prefixes defined, so it still isn't 100% exactly what you are using. Also note that your initial controller is invalid (route prefixes cannot end with a /) so we're still comparing apples to oranges.

So, can you provide a working copy of your enabler and a trimmed down version of your controller (a working one, not pseudo code) with the proper prefixes and routes you are using that demonstrate the problem?

>> basically i am not trying any one to make my code debugging. 

Have you tried debugging yourself? If you attach a debugger, you should see the enable being hit (at least, it is for me).

Imar

 

 
Mateusz Struzik
Reply

Hi again 

problem appear even when i am adding this part of code in controller, again i am not asking to make mine controller working now i am asking why schheduler seems to be disabled after added new class to project. 

did you know the answer ? ever hit something similar ? 

These are my question when my sheduler will be turned on than i can check is my controller working on note right this is the purpose of api enabler i assumed

regards 

Mateusz 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Yes, and that's why I keep asking for your actual code. If I take this code, compile it, drop the DLL in the Bin folder of a DynamicWeb site and attach a debugger, OnNotify gets hit every single time for every request.

using Dynamicweb.Extensibility.Notifications;
using Dynamicweb.Logging;
using Dynamicweb.Notifications;
using System.Net.Http;
using System.Web;
using System.Web.Http;
namespace Degree.Relekta.Ecommerce.WebApi
{
  [Subscribe(Standard.Application.AuthenticateRequest)]
  public class ApiEnabler : NotificationSubscriber
  {
    public const string RoutePrefix = "api/";
    private readonly ILogger logger;
    public ApiEnabler()
    {
      logger = LogManager.System.GetLogger(LogCategory.Monitoring, "Event Handlers");
    }
    public override void OnNotify(string notification, NotificationArgs args)
    {
      logger.Log("Api Enabler before args");
      if (!(args is Standard.Application.AuthenticateRequestArgs authenticateArgs)) return;
      logger.Log("Api Enabler after args");
      var requestPath = HttpContext.Current.Request.Path;
      var isApiRequest = requestPath.StartsWith($"/{RoutePrefix}");
      if (isApiRequest)
      {
        HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
        // When this is set to true then Dynamicweb will skip the default handling of the current request
        authenticateArgs.Handled = true;
        return;
      }
    }
  }
}
using System.Web.Http;
namespace Degree.Relekta.Ecommerce.WebApi.Controller
{
  public class DWTest : ApiController
  {
  }
}

So what that means is:

1. The Web API enabler works as expected.

2. Adding a controller does not break the enabler (that would defeat the whole purpose as the purpose of the enabler is to let your custom controllers run instead of DynamicWeb taking over).

As a first step, I recommend you do the same: just create a new class library, add in the simplest possible code and confirm it working in a debugger. 

So, having confirmed that the enabler works in a simple scenario, what's left are issues in your code that you're now showing or not fixing (like adding a trailing / to a RoutePrefix). Since I don't know how you test your code, run it and what you expect to see where, i can't help much other then recommending what I said above: remove your custom code, add the simplest API enabler and confirm that it works, then add the simplest controller and work from there.

 

 
Mateusz Struzik
Reply

Hi 

Again i was doing erything like you said but for me addig class dervide by api controller just turning off the subscriber, and even if my prefixes were wrong i still should be able to see logs from other paths, right?.

and the code i exactly this i have only subsciuber and this empty controller.

 

So i dont know how i can write it differently: when controller is not added subscriber is working and adding logs for each one page request 

whent controller is added subscriber is turn off

Regards 

Mateusz

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Like I said, you may have code in your controller that breaks all this. Since you show random bits and pieces, and don't respond to things I bring up like the / on the RoutePrefix, I can't tell for sure but only guess. Did you try it with that simple approach from above (i.e. no other code than the controller) and did that also break it? And how do you test this? Are you hitting a specific URL? Does the backend and frontend continue to work normally or are you seeing errors?

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

BTW:

>>  and even if my prefixes were wrong i still should be able to see logs from other paths, right?.

No, because incorrect RoutePrefix formats cause errors at startup time, so ASP.NET won't even start.

 
Morten Snedker Dynamicweb Employee
Morten Snedker
Reply

Hi Mateusz,

Can I please have you create at ticket for us at https://care.dynamicweb.com/. Add a 7z with the dll that contains your controller. Then I'll try and debug it locally. When creating ticket, please refer with url to this thread.

BR
Snedker

 

You must be logged in to post in the forum