Stand up a ASP.Net Core Web API service in an NUnit Test - asp.net-core-webapi

I have a NuGet package that I am trying to write some integration tests around. I am using NUnit to run these tests.
I would like to stand up a basic Web API service using .Net 6.0. Most of the examples I see require a full project dedicated to running the service.
Is there a way I can standup a simple Web API service in the [setup] of my test?
It can just run on localhost and only need a single GET operation.

Related

WebApplicationFactory and release pipeline

I am new to WebApplicationFactory use and want to know
what is the use of this WebApplicationFactory class for integration tests. as my understanding, it is useful only for mocking external services (this is what we do in unit tests).
Should we use WebApplicationFactory for simple web api integration testing?
what happens in release pipelines?
when i run integration tests (written using WebApplicationFactory) open the web api automatically and uses overrided appsettings set in a custom WebApplicationFactory class.
so when i publish the tests code on azure and creates release pipeline stage for integration tests. does the above test code (custom WebApplicationFactory ) override the actual app dll's appsetting. ( i mean does it start the app and use test appsetting logic)
MSFT has pretty good documentation about integration testing and the usage of WebApplicationFactory to help facilitate this type of testing for Web Applications. This is a good place to learn why integration testing is done and how to do it with WebApplicationFactory.
What is the use of this WebApplicationFactory class for integration tests?
The WebApplicationFactory helps you build an in-memory Http Server and an HttpClient capable of making requests on that server.
By default, WebApplicationFactory will build this server using all the same services and dependencies that are registered in the application you are testing. You can, if desired, modify the services that are registered to the IServiceCollection so that Fakes or Mocks can be used instead of concrete implementations, but this is more of a feature of WebApplicationFactory and not its purpose. Whether you use Mocks or not should depend on what you're trying to test, and whether it's OK to use the application's services for testing purposes.
WebApplicationFactory helps test the configuration and middleware pipeline of your web application in a way that unit tests can't. By making HTTP requests into your in-memory server, you're able to ensure your application and its endpoints are working properly.
What happens in release pipelines?
Your integration tests should be in a separate project from your main application. You don't want the integration test project to be in your release pipeline and, unless you have a good reason to, it shouldn't be published to Azure.

Web API as a windows service

So I'm creating a new .Net Framework 4.8 Web API in Visual Studio 2019 and I'm wanting to know how to create the API as a windows service? I can't seem to find any examples or online resources to do so. I can run the API locally in VS and it opens Chrome and shows the responses under the local IIS Server it spins up. How do I take this same project and compile it as a windows service while still using HTTPS?
Web API is fully capable of being self hosted on top of OWIN, and does not require IIS to run.
Web API self hosted is basically just a console app. So the techniques for turning a Web API console app into a Windows Service are the same as for any other .NET console app. You can use a service manager such as NSSM, or create a Windows service project directly (by inheriting from the appropriate classes, pretty messy) or use a library like TopShelf.
Note that it's generally not a good idea to directly expose this self hosted app directly to the public. IIS provides a lot of security benefits out of the box designed to protect against malicious requests. If you're planning to publicly expose it, make sure you stick a proxy in front of it that will fulfill those security needs.

Service Fabric reliable service with .NET Core

So I am playing around with Service Fabric for rolling out a system to the cloud, and so far I have most of the stateless services ported over and running well using ASP.NET Core.
I have another set of services that need to be stateful, and I would like to leverage .NET Core for those services as well. For those I will need to run a custom TCP protocol, not HTTP/WebAPI/WCF.
So the question is; is this possible? The templates only have a ASP.NET Core stateless service template. Can I build something like a .NET Core console application to run as a "Reliable Service"? If so, is there any documentation on how to do this?
Yes, it's possible.
You can create the .NET 4.5.1 stateful service via the template, and then port that project to .NET Core - we've done this and it works just fine.
There doesn't appear to be any official documentation on this process.

Testing ASP.NET site using WatiN. Is it possible to programmatically deploy an ASP.net site locally?

I would like to use WatiN to test the functionality of a website I'm developing. Ideally, I would programmatically deploy the website (asp.net MVC3) before the tests start running, and then refresh the data before each test. Is this possible?
Read here about using MSBuild to deploy web projects. Getting your application into automated deploy on build success (read: Continuous Integration) is really nice.
MSBuild - How to use MSBuild to deploy an ASP.NET MVC application
Using NUnit, for refreshing the data before each test you can decorate a method with [Setup] (or [TestFixtureSetup]) to run code once before each test (or once before each group of tests). I use this for cases that require specific data setup and it works like a champ. I'm assuming other XUnit frameworks have similar mechanisms in place.
Setup - http://www.nunit.org/index.php?p=setup&r=2.2.10

Automating WSDL.exe in a Custom Build

I have a web application written in C# that consumes several internal web services. We have a development tier, a testing tier, and a production tier. Also, we use the WSDL.exe command to generate a Proxies.cs file for a given tier's web services.
When we are ready to deploy our code up the stack from development to test or test to production, we need to run the WSDL.exe command to point to the appropriate version of the web services.
Is there a generally accepted way to automate this?
There are a number of way to do it. A NAnt build script will do it, but I think the most commonly accepted method now is to use MSBuild. See MSDN for details.
Our company uses a combination of NANT + Cruise Control + Custom Utility apps to build our products. More specifically, the task in NANT will allow you to fire off those command-line applications such as WSDL.exe

Resources