📜 ⬆️ ⬇️

Myself SelfHost server

We write "Myself Server" using the System.Web.Http.SelfHost namespace classes.

Encountered using the classes of this namespace when it became necessary to write a web-muzzle, accessible from:

image Windows service.

Let's create two projects
1st - actually our server
2nd will contain api controllers
')
Our server will look like this:

public class HTTPServer { public static string ServiceAddress = string.Format("http://{0}:{1}", Common.Config.HTTPServerIP /*  IP*/, Common.Config.HTTPServerPort /*   */); private HttpSelfHostServer _HTTPserver = null; private static HttpSelfHostConfiguration _config = null; private static HttpSelfHostServer CreateHost() { _config = new HttpSelfHostConfiguration(ServiceAddress); AssembliesResolver assemblyResolver = new AssembliesResolver(); _config.Services.Replace(typeof(IAssembliesResolver), assemblyResolver); _config.Routes.MapHttpRoute( name: "default", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { controller = "Home", id = RouteParameter.Optional }); HttpSelfHostServer server = new HttpSelfHostServer(_config); server.OpenAsync().Wait(); return server; } public void Start() { _HTTPserver = CreateHost(); } public void Stop() { if (_HTTPserver != null) _HTTPserver.CloseAsync().Wait(); } } 


Now our service needs to specify where our api controllers will come from, for this we inherit a class in the server project:

 DefaultAssembliesResolver 


 class AssembliesResolver : DefaultAssembliesResolver { public override ICollection<Assembly> GetAssemblies() { ICollection<Assembly> baseAssemblies = base.GetAssemblies(); List<Assembly> assemblies = new List<Assembly>(baseAssemblies); string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Infrastructure.Server.HTTP.Controllers.dll" /*,      api  */); assemblies.Add(Assembly.LoadFrom(path)); return assemblies; } } 


We proceed to the second project with the controllers. Here we add a class whose name should contain the ending Controller, for example:

Users controller
Regions controller
Devices controller

Overload the ApiController class of the System.Web.Http namespace and add several methods:

 public class UsersController : ApiController { [HttpGet] public object hello() { return "Hello page"; } [HttpPost] public object SendData(int data) { return new HttpResponseMessage(HttpStatusCode.OK); } } 


Let's pay attention to the line again:

 "api/{controller}/{action}/{id}" 


This line describes the route pattern.
We will send requests to the ip-address and port set in ServiceAddress, then write / api , then / the name of our controller class without the Controller prefix, for example:

Users
Regions
Devices
, - and then / method name and parameter list via &

To debug the application, I use the Postman - REST Client 0.8.4.10 chrome extension .

Here is an example of a Get request:
192.168.1.1 : 8080 / api / Users / Hello

Here is an example of a Post request:
192.168.1.1 : 8080 / api / Users / SendData? Data = 1

Source: https://habr.com/ru/post/242917/


All Articles