0

My program is for Self hosting a Web API in a Windows Application. As I dont have much experience in web servie, I request someone's help to fix the problem.

I could make a console application successfully. But when I change it in to Windows application, my IDE goes stuck with Error "The application is in Break Mode".

Console Program;

using System.Web.Http;
using System.Web.Http.SelfHost;

Main Function:


    var config = new HttpSelfHostConfiguration("http://localhost:8080");
                config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional });
    using (HttpSelfHostServer server = new HttpSelfHostServer(config))
    {
    server.OpenAsync().Wait();
    Console.WriteLine("Press Enter to quit.");  // Removed in Win Form
    Console.ReadLine(); // Removed in Win Form
    }

API Controller Class; I need to receive data here from "FromBody" attribute.

 public class FieldsController : ApiController
    {
        [HttpPost]
        public void PostAction([FromBody] Field model)
        {
            int patientID;
            string name;
            string age;
            patientID = model.patientID;
            name = model.patientName;
            age = model.patientAge;
        }
    }
stuartd
  • 70,509
  • 14
  • 132
  • 163
  • 3
    Why on earth are you hosting a Web API in a windows forms app? – stuartd Jul 04 '19 at 11:58
  • 1
    It is needed in windows forms, and I got to make it from this link https://learn.microsoft.com/en-us/aspnet/web-api/overview/older-versions/self-host-a-web-api – lorolabs Kochi Jul 04 '19 at 12:03

1 Answers1

0

Can you put your error in more detail ? I have also done in similar way and my one is working. In the solution of the project I added a new class libraray type project for the webapi. From the main windowsForm application I am intatiating the webapi. The webapi project is as below:

using System;
using System.ServiceModel;
using System.Web.Http;
using System.Web.Http.SelfHost;

namespace MYWebAPI
{
    public class WebApiProgram : IDisposable
    {
        private string URL = "http://localhost:1234/";
        private HttpSelfHostServer server;

        public WebApiProgram()
        {

        }
        public WebApiProgram(string url)
        {
            this.URL = url;

        }

        public void Dispose()
        {
            server.CloseAsync().Wait();
        }

        public void Start()
        {

            var config = new HttpSelfHostConfiguration(URL);
            config.TransferMode = TransferMode.Streamed;

            AddAPIRoute(config);

            server = new HttpSelfHostServer(config);
            var task = server.OpenAsync();
            task.Wait();
            //Console.WriteLine("Web API Server has started at:" + URL);
            //Console.ReadLine();

        }

        private void AddAPIRoute(HttpSelfHostConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "ActionApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

        }
    }

From the main() [program.cs file] winform applition I call the webapi start function.

static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main(string[] args)
        {
            //Start the webAPI
            WebApiProgram.StartMain();

            Thread.CurrentThread.CurrentCulture
                = Thread.CurrentThread.CurrentUICulture
                    = CultureInfo.InvariantCulture;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var mainForm = new MainForm();
            Application.Run(mainForm);
        }


    }
rasoo
  • 73
  • 1
  • 7