Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service Provider object is becoming null while running the script through commandline #121

Open
kjakkam opened this issue Apr 23, 2024 · 8 comments
Labels
bug Something isn't working

Comments

@kjakkam
Copy link

kjakkam commented Apr 23, 2024

In Startup.cs file, created an object for ServiceProvider as below

services.AddSingleton<IRestLibrary, RestLibrary>();
IServiceProvider serviceProvider;
serviceProvider = services.BuildServiceProvider();

During, any of other methods execution in that same Project, trying to get the data of serviceProvider. But that serviceProvider's data is showing as null.

The issue is only happening through the commandline execution. If we try to execute the same scenario through Visual Studio, it is working fine without any issues.

@pengweiqhca pengweiqhca added the bug Something isn't working label Apr 24, 2024
@pengweiqhca
Copy link
Owner

Can you give us any more information? such as a demo?

@kjakkam
Copy link
Author

kjakkam commented Apr 30, 2024

Sure, I will create a sample project, and share the details.

@kjakkam
Copy link
Author

kjakkam commented May 3, 2024

@pengweiqhca, I have created a demo project, and in which the issue reproducing.

Please go through the below demo project
https://github.com/jakkamk4/XUnitTestAutomation

**Issue: **
From CommandPrompt, go to following project execution Path - bin\Debug\net6.0 folder
Execute below command - It will execute - "GetLocationOfTestEmployee" Test case
dotnet test TestProject1.dll

While executing the test case through Comman Prompt, it is throwing System.NullReferenceException at ServiceLocator.cs:line 35
i.e. IServiceProvider is getting null

If we execute same test case through Visual studio, it is executing successfully. The issue is only with CommandPrompt execution.

Note: For more details of execution, you can find the log details of execution in the TestExecutionLogs file under the bin\Debug\net6.0 folder

@pengweiqhca
Copy link
Owner

Please use [MethodData], more see [MemberData] how to inject?

@kjakkam
Copy link
Author

kjakkam commented May 6, 2024

Please use [MethodData], more see [MemberData] how to inject?

@pengweiqhca , seeing below error after replacing the [MemberData] attribute with [MethodData] attribute (ex: [MethodData(nameof(TestData.GetLocationObject))])

Error message after running the test case:
System.NotSupportedException : Specified method is not supported.

@kjakkam
Copy link
Author

kjakkam commented May 6, 2024

Please use [MethodData], more see [MemberData] how to inject?

@pengweiqhca , seeing below error after replacing the [MemberData] attribute with [MethodData] attribute (ex: [MethodData(nameof(TestData.GetLocationObject))])

Error message after running the test case: System.NotSupportedException : Specified method is not supported.

@pengweiqhca ,One observation, through command prompt execution, I assumed that - Startup file is not being called first.

@pengweiqhca
Copy link
Owner

Fix jakkamk4/XUnitTestAutomation#1

@kjakkam
Copy link
Author

kjakkam commented May 8, 2024

Fix jakkamk4/XUnitTestAutomation#1

@pengweiqhca , Thank you for providing the fix details. It was executing successfully through Command Line and Visual Studio without any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants