Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Get Request Token and AppSettings in Application project #4235


User avatar
0
khai created

Hello, I want to ask a few questions about service in Application project.

1/ I am creating a service (let call it LibraryAppService) and in that service, I want to get the token from current request (or the UserId, TenantId to call other API - which can use the token that AspNetZero issued). How can I do that?

2/ I don't want to hardcode my other API URL inside the LibraryAppService but I want to put it inside AppSettings.json. How can I do that?

Thank you very much.


6 Answer(s)
  • User Avatar
    0
    khai created

    Hello,

    For the first question, I figured out that I can use AbpSession to get UserId and TenantId. But to get the token, how to do that?

    However, for the second question, I got some clue of injecting IConfigurationRoot into the constructor of my Service in ApplicationProject. But when I inject it, I got the unexpected error:

    • Build OK
    • Run-time OK And when I send a request to that service, I got 500 and "An internal error occurred during your request!" without any clue to figure out.

    Please help me.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Khai,

    Have you checked the Logs.txt file for error details ? Probably it contains a detailed error message.

  • User Avatar
    0
    khai created

    Hello @ismcagdas,

    I have checked the Logs.txt file, and this is the result. It seems that the IConfigurationRoot didn't register.

    ERROR 2017-11-29 23:53:40,698 [10   ] Mvc.ExceptionHandling.AbpExceptionFilter - Can't create component 'OnePlace.O1.OnePlace.Library.LibraryAppService' as it has dependencies to be satisfied.
    
    'OnePlace.O1.OnePlace.Library.LibraryAppService' is waiting for the following dependencies:
    - Service 'Microsoft.Extensions.Configuration.IConfigurationRoot' which was not registered.
    
    Castle.MicroKernel.Handlers.HandlerException: Can't create component 'OnePlace.O1.OnePlace.Library.LibraryAppService' as it has dependencies to be satisfied.
    
    'OnePlace.O1.OnePlace.Library.LibraryAppService' is waiting for the following dependencies:
    - Service 'Microsoft.Extensions.Configuration.IConfigurationRoot' which was not registered.
    
       at Castle.MicroKernel.Handlers.DefaultHandler.AssertNotWaitingForDependency()
       at Castle.MicroKernel.Handlers.DefaultHandler.ResolveCore(CreationContext context, Boolean requiresDecommission, Boolean instanceRequired, Burden& burden)
       at Castle.MicroKernel.Handlers.DefaultHandler.Resolve(CreationContext context, Boolean instanceRequired)
       at Castle.MicroKernel.DefaultKernel.ResolveComponent(IHandler handler, Type service, IDictionary additionalArguments, IReleasePolicy policy)
       at Castle.MicroKernel.DefaultKernel.Castle.MicroKernel.IKernelInternal.Resolve(Type service, IDictionary arguments, IReleasePolicy policy)
       at Castle.Windsor.MsDependencyInjection.ScopedWindsorServiceProvider.GetServiceInternal(Type serviceType, Boolean isOptional) in D:\Github\castle-windsor-ms-adapter\src\Castle.Windsor.MsDependencyInjection\ScopedWindsorServiceProvider.cs:line 55
       at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
       at Microsoft.AspNetCore.Mvc.Controllers.ServiceBasedControllerActivator.Create(ControllerContext actionContext)
       at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeInnerFilterAsync>d__14.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.<InvokeNextExceptionFilterAsync>d__23.MoveNext()
    

    <cite>ismcagdas: </cite> Hi @Khai,

    Have you checked the Logs.txt file for error details ? Probably it contains a detailed error message.

  • User Avatar
    0
    alirizaadiyahsi created

    Hi @Khai,

    Could you share your related code?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Khai,

    I think you need to use IAppConfigurationAccessor instead of IConfigurationRoot. Can you try that ?

  • User Avatar
    0
    khai created

    Hi @ismcagdas, Thank you very much, using IAppConfigurationAccessor help me pretty well. But why I need to use that instead of IConfigurationRoot?

    <cite>ismcagdas: </cite> Hi @Khai,

    I think you need to use IAppConfigurationAccessor instead of IConfigurationRoot. Can you try that ?