0

So i have got a simple question, when using our cms we can attach a driver as an executable.

The driver we want to make is an httpreceiver or just an api endpoint. SO i tought lets use asp.net web api for it -> using version .net 4.6.1. altough asp.net application requires a webserver and is not an executable, But i read on google you can use it inside a wpf application since our cms is wpf in the first place.

So my question is is there a way i can use my mvc web api project inside a wpf application? and if not what would be the best bet to have an httpreceiver or httppost receiver into an executable?

Main reason is we want to send httppost requests to the server as a desktop application. I know it's complicated but thats how it needs to be as far as I know.

In the case where asp is not an option, what the best way to make a postreqst/ httpreceiver as a desktop application?

EDit:

the resource guide from microsoft beneath was perfectly however i still have a question:

string baseAddress = "http://localhost:9000/";

            // Start OWIN host 


            using (WebApp.Start<Startup>(url: baseAddress))
            {
                // Create HttpClient and make a request to api/values 
                HttpClient client = new HttpClient();

                string username = "test".ToUpper().Trim();
                string password = "test123";

                //Mock data
                var body = new PostTemplate1();
                body.Description = "test";
                body.StateDesc = "httpdriver/username";
                body.TimeStamp = DateTime.Now;
                body.Message = "This is a post test";

                var json = JsonConvert.SerializeObject(body);
                var data = new StringContent(json, Encoding.UTF8, "application/json");

                var authToken = Encoding.ASCII.GetBytes($"{username}:{password}");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));

                var response = await client.PostAsync(baseAddress + @"api/Post", data);

                var result = response.StatusCode;
            }

As the guide says you post to url with port 9000 is there a possibility to use another port and use https?

if yes where to manage certificates for handling https?

  • You can self host a web api in winforms/wpf/console, see [this guide](https://learn.microsoft.com/en-us/aspnet/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api). Although a console app is used, you can use the same technique inside your wpf application. – Peter Bons May 14 '20 at 11:34
  • This is one of those things that's actually very easy to do, yet seems difficult by virtue of the fact that there's 100 different ways of doing it and every web site seems to tell you something different. I personally tend to implement this as explained in [the answer to this question](https://stackoverflow.com/questions/51539395/loading-views-in-wpf-self-host-asp-net-core-application/51544049), and I call `HostBuilder.Start()` / `HostBuilder.Stop()` in response to my App `Startup`/`Exit` events. I derive my controllers from `Microsoft.AspNetCore.Mvc.ControllerBase`. – Mark Feldman May 14 '20 at 12:58
  • @PeterBons thanks that was the exact guid/ resource i was looking for – Technology Researcher May 14 '20 at 16:11

0 Answers0