我正在尝试使用以下代码在 Windows 窗体应用程序中自行托管 Web Api 服务
namespace MascoteAquarium.Desktop
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"DefaultApi", "api/{controller}/id", new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMainMenu());
}
}
}
当我尝试
http://localhost:8080/api/*(some-controller)*
我在System.Web.Http.SelfHost.HttpSelfHostServer.ProcessRequestContext(ChannelContext channelContext, RequestContext requestContext)收到NullReferenceException
有人知道发生了什么吗?是否可以在 Win Forms 应用程序中自行托管?