The Owin full name is open Web Interface for. NET, which is targeted. NET platform's open Web interface.
With Owin, you can get ASP. NET out of IIS and no longer care about the Web server, which is especially handy in the ASP. NET Web API test, which, of course, can be managed with memory in addition to using Owin hosting.
So how to use Owin hosting?
0. Create a new ASP. NET Web API 2 Project
Asp. NEt Web API 2 appears so long, in addition to the old project, I believe no one will use the first version of it. I have tried to add Owin hosting in the old project, especially trouble with the various assemblies incompatible, and finally did not succeed. Therefore, do not know whether Owin does not support the first version of the API or other reasons, if you want to use Owin, it is recommended to upgrade to the new version of the API.
Because the use of Owin is mainly convenient unit testing, is the new project when the "Add Unit Test" hook.
1. Adding Owin References and Startup.cs startup files
Add a reference to Owin with NuGet (Iappbuilder here):
Pm>install-package Owin
Then add Startup.cs to the project:
using System.Web.Http; using Owin; namespace owinhost{ publicclass Startup { publicvoid Configuration (iappbuilder appBuilder) { var config=New Httpconfiguration (); Webapiconfig.register (config); Appbuilder.usewebapi (config); }}}
Then add Microsoft.AspNet.WebApi.Owin with NuGet (Usewebapi extension method here)
Pm>install-package Microsoft.AspNet.WebApi.Owin
When you add a reference using NuGet, nuget updates the reference together if the dependent assembly version is incompatible.
Here, the API Project add Owin has finished, and the following starts to start it in the unit test.
2. Enabling the project in unit tests
Before starting Owin hosting, also introduce Microsoft.Owin.Hosting (Webapp.start method here) and Microsoft.Owin.Host.HttpListener in the test project
Pm> install-PackageMicrosoft.Owin.HostingPM> Install-package Microsoft.Owin.Host.HttpListener
Add startup test code when you're done referencing
namespaceowinhost.tests{[TestClass] Public classowinhoststarttests:idisposable {Private Const stringHostaddress ="http://localhost:9000"; Private StaticIDisposable _webapp; Private StaticHttpClient _client;
Publicowinhoststarttests () {_webapp= webapp.start<startup>(hostaddress); _client=NewHttpClient (); _client. BaseAddress=NewUri (hostaddress); } [TestMethod] Public voidstarttest () {varResponse = _client. Getasync ("api/values"). Result; Assert.AreEqual (Httpstatuscode.ok,response. StatusCode); Assert.AreEqual ("value1", Response. content.readasasync<list<string>> (). Result.elementat (0)); } Public voidDispose () {_webapp.dispose (); } }}
The result was a smooth passage.
In just a few steps, it's easy to add Owin hosting, which is a great convenience for testing, fully implementing a one-click Run test without concern for IIS.
3. Issues that may be encountered
In the process of using, it is possible that some assemblies are dependent on version incompatibility issues, and it is good to upgrade the package directly with NuGet.
4. Summary
Adding Owin hosting does not have any side effects on your project, and does not affect your IIS deployment, so take it easy. Of course, Owin also directly host ASP. NET is no problem, now has a very good component support, can be used as an alternative to IIS.
Using Owin to host ASP. NET Web API 2