在单个控制台应用程序中自行托管 SignalR 和 Web Api
Self-Host SignalR and Web Api in a single Console Application
在单个控制台应用程序中,我必须自行托管 SignalR 服务器和 Web Api。
我正在使用此代码
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
using System.Web.Http;
using System.Net.Http;
namespace SignalRSelfHost
{
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
//////////
// Create HttpCient and make a request to api/values
HttpClient client = new HttpClient();
var response = client.GetAsync(url + "/api/values").Result;
Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.ReadLine();
//////////
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
////////
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
app.UseWebApi(config);
/////////
app.MapSignalR();
}
}
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
}
并且我输入了以下命令:
Install-Package Microsoft.AspNet.SignalR.SelfHost
Install-Package Microsoft.Owin.Cors
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
现在 SignalR 服务器可以正常工作,但网络不行Api:它给了我 "No HTTP resource was found that matches the request URI 'http://localhost:8080/api/values'"。我的控制器 class 如下:
namespace SignalRSelfHost
{
class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}
谁能帮帮我?
将您的 ValuesController 从 private
设置为 public
,它应该可以工作。
在单个控制台应用程序中,我必须自行托管 SignalR 服务器和 Web Api。
我正在使用此代码
using System;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin.Hosting;
using Owin;
using Microsoft.Owin.Cors;
using System.Web.Http;
using System.Net.Http;
namespace SignalRSelfHost
{
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8080";
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
//////////
// Create HttpCient and make a request to api/values
HttpClient client = new HttpClient();
var response = client.GetAsync(url + "/api/values").Result;
Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.ReadLine();
//////////
Console.ReadLine();
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
////////
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
app.UseWebApi(config);
/////////
app.MapSignalR();
}
}
public class MyHub : Hub
{
public void Send(string name, string message)
{
Clients.All.addMessage(name, message);
}
}
}
并且我输入了以下命令:
Install-Package Microsoft.AspNet.SignalR.SelfHost
Install-Package Microsoft.Owin.Cors
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
现在 SignalR 服务器可以正常工作,但网络不行Api:它给了我 "No HTTP resource was found that matches the request URI 'http://localhost:8080/api/values'"。我的控制器 class 如下:
namespace SignalRSelfHost
{
class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}
谁能帮帮我?
将您的 ValuesController 从 private
设置为 public
,它应该可以工作。