Unable to fetch service from context

I am trying to access the service , I have added to the serviceContext but it keeps giving error unable to find service.Same code was working earlier suddenly stopped working. Link to repo GitHub - vishi-chauhan/hims-loginaudit: Login trail of user in OPENMRS Using it like this LoginAuditService service = Context.getService(LoginAuditService.class);

Module manager shows, module(loginaudit) as running Pastebin server log 18-Feb-2021 16:36:39.659 INFO [main] org.apache.catalina.startup.VersionLoggerLi - Pastebin.com

2 Likes

what happens when you simply auto wire the LoginAuditService , i will pass through your code and get here by the end of tommorrow

Thanks in advance.

Have not tried simply autowiring as openmrs docs suggested using service injected beans via context only.

And also does simply autowiring will work ?

One more thing, If I don’t inject bean in service context will autowiring than also work ?

try calling the service using Autowire even after having the service context declared

Found the issue, I was calling the REST service in which servicecontext was used just after starting the server, due to which I believe all dependencies were not not injected.

But, if I open the application in browser and login and move to the admin setting-manage module which will make application to load the module and all dependencies properly than it does not gives me error even once.

Yes, Autowire works perfectly.

But it also requires me to open app and go to manage modules atleast once to make it work properly else will give internal server error no qualified bean found.

Once the dependency injected, everything works fine without any issue.

You do not need to do this. When are you trying to access the service? At application startup or some other time?

But I have tried this multiple times, it requires to login into reference app else it gives bean not found error.

Share the error log and line where you call the service.

I am calling the service here

To recreate issue

  1. Build loginaudit module in local system 2.Inject the module dependency in webservice omod 1.9 3.In session controller Autowire the bean 4.Call the method in the get session method 5.Run the tomact 6.Call session service from POSTMAN

Error started to occur again. Error log on browser - The following error occurred at startup:Unable to start OpenMRS. Error thrown - Pastebin.com

Error log on tomcat console - 19-Feb-2021 12:18:59.823 INFO [main] org.apache.catalina.startup.VersionLoggerLi - Pastebin.com

If I don’t Autowire the dependency, OPENMRS startup works but when I fetch the service from service context - LoginAuditService service = Context.getService(LoginAuditService.class);

It gives error "error": { "message": "[Service not found: interface org.openmrs.module - Pastebin.com

Don’t know why it is not getting injected. Sometimes it works, sometimes start giving error.

Why are you modifying the webservice module?

I have to intercept session service call which is used for Authentication so that I can save the entry to the db.Any other way to do this, please suggest.

Can you fully describe the use case? Or share the ticket url?

We are using React as front end which is hosted on different server from OPENMRS.

For user to login/logout, using rest service–> openmrs/ws/rest/v1/session.

We need to keep track of user login and logout in OPENMRS.

To do so created a module auditlogin in which defined service(LoginAuditService) with two methods savelogindetail and savelogoutdetail which are called in get session and delete session respectively.

To access LoginAuditService in webservices module, injected the same in servercontext.

1.Tried Autowire for LoginAuditService which stops OPENMRS from starting with error dependency not found.

2.Tried getting service from context OPENMRS starts up(as Loginservice is fetched inside a method not as class variable) but throws error when getting service is processed at method call.

Why don’t you simply implement this? openmrs-core/UserSessionListener.java at 2.2.0 · openmrs/openmrs-core · GitHub

Great!! This worked.

But now we have another issue. JSESSIONID does not work bcz browser does not allow to set cookie for other domain(OPENMRS instance is running on different server than front end).So we need to pass user and password with each rest call and it creates login/logout entry for each rest call.

The rest call from your front end to the OpenMRS instance domain will result into a JSESSIONID cookie which you can pass in subsequent rest calls from the front end to the OpenMRS instance domain.