danieleteti / delphimvcframework

DMVCFramework (for short) is a popular and powerful framework for WEB API in Delphi. Supports RESTful and JSON-RPC WEB APIs development.
Apache License 2.0
1.23k stars 354 forks source link

Cannot create controller #593

Closed ertankucukoglu closed 1 year ago

ertankucukoglu commented 1 year ago

Hello,

I do not know the framework. Just trying to help a friend as he does not know English. He do not know the framework, too. Though I read about the framework as much as I can before posting. Please bare with me.

Problem is that we get cannot create controller error as below

[Exception] Unknown connection default [PathInfo "POST /api/login?user=xxx&pass=yyy"] (Custom message: "Cannot create controller") [dmvcframework]

I see login defined in the controller class

 [MVCPath('/api')]
 [MVCProduces('application/json')]
 TApi = class(TMVCController)
    private
    FDConn : TFDConnection;
  protected

  public
    constructor Create; override;
    destructor Destroy; override;
    [MVCPath]
    [MVCHTTPMethod([httpGET])]
    procedure Index;

    [MVCPath('/login')]
    [MVCHTTPMethod([httpPOST])]
    procedure Login;

I also see controller class is used in the engine as following

  FMVC := TMVCEngine.Create(Self,
    procedure(Config: TMVCConfig)
    begin
      // session timeout (0 means session cookie)
      Config[TMVCConfigKey.SessionTimeout] := '0';
      //default content-type
      Config[TMVCConfigKey.DefaultContentType] := TMVCConstants.DEFAULT_CONTENT_TYPE;
      //default content charset
      Config[TMVCConfigKey.DefaultContentCharset] := TMVCConstants.DEFAULT_CONTENT_CHARSET;
      //unhandled actions are permitted?
      Config[TMVCConfigKey.AllowUnhandledAction] := 'false';
      //enables or not system controllers loading (available only from localhost requests)
      Config[TMVCConfigKey.LoadSystemControllers] := 'true';
      //default view file extension
      Config[TMVCConfigKey.DefaultViewFileExtension] := 'html';
      //view path
      Config[TMVCConfigKey.ViewPath] := 'templates';
      //Max Record Count for automatic Entities CRUD
      Config[TMVCConfigKey.MaxEntitiesRecordCount] := '-1';
      //Enable Server Signature in response
      Config[TMVCConfigKey.ExposeServerSignature] := 'true';
      //Enable X-Powered-By Header in response
      Config[TMVCConfigKey.ExposeXPoweredBy] := 'true';
      // Max request size in bytes
      Config[TMVCConfigKey.MaxRequestSize] := IntToStr(TMVCConstants.OneMiB * 500);//IntToStr(TMVCConstants.DEFAULT_MAX_REQUEST_SIZE);
    end);
  FMVC.AddController(TApi);

There are no middleware loaded.

Without some knowledge about the framework, I could not figure what's wrong. I just believe this is a very basic error in code/definitions as no such question asked in the issues before.

Any help is appreciated.

Thanks & Regards, Ertan

fastbike commented 1 year ago

What happens when you step through the code once the exception happens? What does the call stack show ?

ertankucukoglu commented 1 year ago

This is a service application. I am not quite sure how I can test it as a regular windows application to be able to run in debug mode.

fastbike commented 1 year ago

Put a sleep(5000) line in the service start and then attach the IDE debugger.

ertankucukoglu commented 1 year ago

Here is information I could collect using postman with POST request image MVCFramework.pas:2540 image

This using the framework sources from yesterday.

One thing I am not sure is if it is OK to use a POST method and use Query parameters as input in the sources for the method as codes has following references

Context.Request.QueryStringParam('user')
Context.Request.QueryStringParam('pass')

Edit: After checking again code execution is not there for these references to become a problem. Exception is earlier so problem must be something else.

fastbike commented 1 year ago

OK, it looks like you are mixing something up because the Exception message you initially posted had a message of "Cannot create controller" but the code you are stopped on would give "Not Found". You can see where that exception be raised around line 2462. So you seem to have a number of issues.

Back to the code above though, here's some things to take away and share with your friend.

The framework maps the request to the controller action using 4 key things:

So show us your postman inputs: URL, headers, body etc.

And figure out what makes the code reach the exception on line 2462 which was your original question. My guess is that something in the constructor of your controller class is incorrect and the constructor blows up, which is caught by the MVCFramework. Show us the code in the constructor.

ertankucukoglu commented 1 year ago

Initial question, log records are taken from production server. In order to be able to debug, I had to use development computer. Likely, there are some issues with it.

Just posting requested information now and I will try to walk back in the stack trace later image Body, is empty

TApi constructor is as following

constructor TApi.Create;
begin
  inherited;
  FDConn := TFDConnection.Create(nil);
  FDConn.ConnectionDefName := 'DevriyeConnX';
  //s:=FDConn.ConnectionString;
  FDConn.Connected := True;
  ActiveRecordConnectionsRegistry.AddDefaultConnection(FDConn);
end;

Just to be complete, production uses command line version. Which seemingly works fine for a few requests even for a few hours sometimes. Later, initial log errors appears.

We are trying to switch to service application as we suspect windows logouts or does some other weird things for the error to occur. Not 100% sure though.

Edit: Figured my mistake that I should not start with /rest but directly with /api Will share new stack trace of actual error hopefully.

fastbike commented 1 year ago

Ok, my guess is the constructor is having issues with the DB connection. Which explains the text of the original (outer) exception "Unknown connection default"

So this is not an MVC problem as far as I can see. I'm not familiar with the DB library. Maybe ask on the facebook group ?

ertankucukoglu commented 1 year ago

I could run as service application now. Seems to be fine for the time being. If problem continues, that will be known in about 8 hours. If it continues, I will add logging at the constructor in order to be sure about it. I cannot run debug run on the production server at the moment for getting a call stack. Thank you for your help.

fastbike commented 1 year ago

You can add a global exception handler to the MVC Engine instance and then write out the call stack to the log from some code in there. We use Eurekalog to get the call stack as formatted text, and find it invaluable.

ertankucukoglu commented 1 year ago

I'm not familiar with the DB library

Is it possible to use UniDAC with MVC?

danieleteti commented 1 year ago

@ertankucukoglu you can use any db library with dmvcframework, but TMVCActiveRecord uses FireDAC. If you are not interested in TMVCACTIVERECORD you can use anything.

danieleteti commented 1 year ago

Any news on this?

ertankucukoglu commented 1 year ago

Actually, we could not figure out the reason of the exception. It does come occasionally make it console application or service application is no difference.

Due to the pressure from customers, we had to find an alternative solution for the time being.

danieleteti commented 1 year ago

if you can provide a small, self-contained sample I can give a look to it.

Il giorno gio 24 nov 2022 alle ore 08:26 ertankucukoglu < @.***> ha scritto:

Actually, we could not figure out the reason of the exception. It does come occasionally make it console application or service application is no difference.

Due to the pressure from customers, we had to find an alternative solution for the time being.

— Reply to this email directly, view it on GitHub https://github.com/danieleteti/delphimvcframework/issues/593#issuecomment-1326054097, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAK4ZJBMJOESTWWWQ76ZK7LWJ4KCFANCNFSM6AAAAAASE35Q2M . You are receiving this because you commented.Message ID: @.***>

-- Daniele Teti CEO & CTO bit Time Professionals website [it]: www.bittimeprofessionals.it website [en]: www.bittimeprofessionals.com blog: www.danieleteti.it ​my last book on leanpub https://leanpub.com/delphimvcframework​ my​ previous​ books on amazon https://www.amazon.it/s?i=stripbooks&rh=p_27%3ADaniele+Teti mobile: +39 3496626822 office: +39 06 20761499

ertankucukoglu commented 1 year ago

I will see if I can collect necessary information. I prefer not to pass this information in public though. @danieleteti I got codes, old log file available. How can I share them in private? Thanks.

danieleteti commented 1 year ago

Yes, you can "professionals (at) bittime (dot) it"