I've spent the better part of a couple days trying to get PushStreamContent to work with Web API's self host. I have my controller:
namespace TestPushConsole {
public class PushController : ApiController {
private static readonly ConcurrentBag<StreamWriter> _Writers = new ConcurrentBag<StreamWriter>();
public HttpResponseMessage Get(HttpRequestMessage request) {
var response = request.CreateResponse();
response.Content = new PushStreamContent(OnStreamAvailable, "text/event-stream");
return response;
}
private void OnStreamAvailable(Stream stream, HttpContent content, TransportContext context) {
stream.Flush();
_Writers.Add(new StreamWriter(stream));
}
public static void Say(string text) {
foreach (var w in _Writers) {
w.WriteLine("data:{ text : \"" + text + "\" }");
w.Flush();
//w.Close();
}
}
}
}
And my Main:
static void Main(string[] args) {
var config = new HttpSelfHostConfiguration("http://localhost:22222");
config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using(var server = new HttpSelfHostServer(config)) {
server.OpenAsync().Wait();
Console.WriteLine("Press enter to quit.");
Console.ReadLine();
}
}
And, basically, push messages don't come through. If i move the controller to an ASP.net project running through the VS 2010 web development server, push messages DO work. Again... exact same code. For self-hosting, everything else seems to work, including
sending web pages. It just seems to be PushStreamContent that's not working.
daytrip00
0 Points
6 Posts
Web API Self hosting and PushStreamContent
Sep 18, 2012 05:25 PM|LINK
I've spent the better part of a couple days trying to get PushStreamContent to work with Web API's self host. I have my controller:
namespace TestPushConsole { public class PushController : ApiController { private static readonly ConcurrentBag<StreamWriter> _Writers = new ConcurrentBag<StreamWriter>(); public HttpResponseMessage Get(HttpRequestMessage request) { var response = request.CreateResponse(); response.Content = new PushStreamContent(OnStreamAvailable, "text/event-stream"); return response; } private void OnStreamAvailable(Stream stream, HttpContent content, TransportContext context) { stream.Flush(); _Writers.Add(new StreamWriter(stream)); } public static void Say(string text) { foreach (var w in _Writers) { w.WriteLine("data:{ text : \"" + text + "\" }"); w.Flush(); //w.Close(); } } } }And my Main:
static void Main(string[] args) { var config = new HttpSelfHostConfiguration("http://localhost:22222"); config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); using(var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Press enter to quit."); Console.ReadLine(); } }And, basically, push messages don't come through. If i move the controller to an ASP.net project running through the VS 2010 web development server, push messages DO work. Again... exact same code. For self-hosting, everything else seems to work, including sending web pages. It just seems to be PushStreamContent that's not working.
rpgmaker2013
Member
2 Points
1 Post
Re: Web API Self hosting and PushStreamContent
Jan 17, 2013 06:36 AM|LINK
This should work for you.
namespace PushConsole { public class PushController : ApiController { private static readonly ConcurrentBag<StreamWriter> _Writers = new ConcurrentBag<StreamWriter>(); public HttpResponseMessage Get(HttpRequestMessage request) { var response = request.CreateResponse(); response.Headers.Add("Access-Control-Allow-Origin", "*"); response.Headers.Add("Cache-Control", "no-cache, must-revalidate"); response.Content = new PushStreamContent(OnStreamAvailable, "text/event-stream"); return response; } private void OnStreamAvailable(Stream stream, HttpContent content, TransportContext context) { _Writers.Add(new StreamWriter(stream) { AutoFlush = true }); } public static void Say(string text) { try { foreach (var w in _Writers) { w.Write("data:{ text : \"" + text + "\" }\n\n"); } } catch { } } } class Program { static void Main(string[] args) { var config = new HttpSelfHostConfiguration("http://localhost:22222"); config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); config.TransferMode = System.ServiceModel.TransferMode.Streamed; using (var server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Type exit to quit."); while (true) { var text = Console.ReadLine(); if (text.ToLower() == "exit") break; PushController.Say(text); } } } } }