I am working a asp .net core 6.0 web api project.
Problem overview:
I faced an issue. When running the project the get request's response is 200 ok. BUT while building the project, a get request show 500Internal Server Error
and in terminal also I got error.
Detail:
When I run the project with dotnet run
(Hosting environment: Development, port is 7045 or 5032),
And https://localhost:7045/api/public/opayo-payment/retrieve-transaction/9A5CAE22-7109-D006-A017-41BF9F138076
this get request's response is 200
BUT when I build the project with dotnet publish -c Release
, dotnet /home/PaymentApi.dll
(Hosting environment: Production, port is 5000 or 5001)
and, https://localhost:5001/api/public/opayo-payment/retrieve-transaction/9A5CAE22-7109-D006-A017-41BF9F138076
this get request' response is 500Internal Server Error
And in the terminal I got below error
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMDJ000A9MDB", Request id "0HMDJ000A9MDB:00000002": An unhandled exception was thrown by the application.
System.ArgumentNullException: Value cannot be null. (Parameter 'uriString')
at System.Uri..ctor(String uriString)
at Program.<>c__DisplayClass0_0.<<Main>$>b__1(HttpClient c) in /home/PaymentApi/Program.cs:line 16
at Microsoft.Extensions.Http.DefaultHttpClientFactory.CreateClient(String name)
at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.AddTransientHelper[TClient,TImplementation](IServiceProvider s, IHttpClientBuilder builder)
at Microsoft.Extensions.DependencyInjection.HttpClientBuilderExtensions.<>c__DisplayClass13_0`2.<AddTypedClientCore>b__0(IServiceProvider s)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitDisposeCache(ServiceCallSite transientCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
at lambda_method9(Closure , IServiceProvider , Object[] )
at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass7_0.<CreateActivator>b__0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass6_0.<CreateControllerFactory>g__CreateController|0(ControllerContext controllerContext)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
in program.cs
builder.Services.Configure<MerchantSessionConfig>(builder.Configuration.GetSection("Payments:TestOpayo"));
builder.Services.AddHttpClient<IOpayoPaymentService, OpayoPaymentService>("PublicOpayoApi", c =>
c.BaseAddress = new Uri(builder.Configuration.GetValue<string>("Payments:TestOpayo:Url"))
);
service
private readonly HttpClient _client;
private readonly IHttpClientFactory _clientFactory;
private readonly MerchantSessionConfig _merchantSessionConfigoptions;
private readonly DataDbContex _dataDbContex;
public OpayoPaymentService(HttpClient client, IHttpClientFactory clientFactory,
IOptions<MerchantSessionConfig> options, DataDbContex dataDbContex)
{
client = clientFactory.CreateClient("PublicOpayoApi");
_clientFactory = clientFactory;
_client = client;
_merchantSessionConfigoptions = options.Value;
_dataDbContex = dataDbContex;
// Basic Authentication
_client.DefaultRequestHeaders.Add("Accept", "application/json");
var authenticationString = $"{_merchantSessionConfigoptions.Username}:{_merchantSessionConfigoptions.Password}";
var base64EncodedString = Convert.ToBase64String(System.Text.ASCIIEncoding.UTF8.GetBytes(authenticationString));
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", base64EncodedString);
}
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=<server>; port=5432; user id=postgres; password=<password>; database=<db>; Integrated Security=true; Pooling=true; CommandTimeout=300;Include Error Detail=true;Log Parameters=true"
},
"Payments": {
"TestOpayo": {
"Url": "https://pi-test.sagepay.com",
"VendorName": "vendor",
"Username": "username",
"Password": "password"
}
}
}
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Please anyone help me to find the mistake.
ok try it :
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Server=<server>; port=5432; user id=postgres; password=<password>; database=<db>; Integrated Security=true; Pooling=true; CommandTimeout=300;Include Error Detail=true;Log Parameters=true"
},
"Payments": {
"TestOpayo": {
"Url": "https://pi-test.sagepay.com",
"VendorName": "vendor",
"Username": "username",
"Password": "password"
}
}
}