[英]Self hosting Web API
My program is for Self hosting a Web API in a Windows Application.我的程序用于在 Windows 应用程序中自托管 Web API。 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".
但是当我将其更改为 Windows 应用程序时,我的 IDE 卡住了错误“应用程序处于中断模式”。
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; API控制器类; I need to receive data here from "FromBody" attribute.
我需要从“FromBody”属性接收数据。
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;
}
}
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.
在项目的解决方案中,我为webapi添加了一个新的类库类型项目。 From the main windowsForm application I am intatiating the webapi.
在主 windowsForm 应用程序中,我正在启动 webapi。 The webapi project is as below:
webapi项目如下:
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.我从 main() [program.cs file] winform applition 调用 webapi start 函数。
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.