在单个控制台应用程序中,我必须同时托管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服务器可以正常运行,但WebApi不能运行:它给我“找不到与请求URI'http://localhost:8080/api/values'匹配的HTTP资源”。我的控制器类如下:
namespace SignalRSelfHost
{
class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
}
有人可以帮助我吗?
答案 0 :(得分:1)
将您的ValuesController从private
设置为public
,它应该可以工作。