问题描述
我有一个简单的Web API,它返回的IObservable。我使用的HttpClient来获取观测,这样我可以订阅它。我的问题是关于认购返回的IObservable发出一个空的结果。
I have a simple Web API which returns an Iobservable. I am using HttpClient to get the Observable so that I can subscribe to it. My problem is the returned Iobservable on subscription sends out an 'empty' result.
服务器
public IObservable<DataItem> GetDataItems()
{
return Observable.Generate(0, i => i < 10, i => i + 1,
i => new DataItem
{
Id = i,
Name = String.Format("Storage{0}",i)
});
}
客户端
public IObservable<DataItem> GetDataItems()
{
using (HttpClient apiClient = new HttpClient())
{
apiClient.BaseAddress = new Uri("http://localhost:9001");
apiClient.DefaultRequestHeaders.Add("x-user-authentication", "xxxxxx");
return apiClient
.GetAsync("api/xxxx/yyyy").Result.Content
.ReadAsAsync<DataItem>().ToObservable();
}
}
var source = GetDataItems();
List<DataItem> items = new List<DataItem>();
IDisposable consoleSubscription = source.Subscribe(
x => Console.WriteLine("{0}:{1}", x.Id, x.Name),
ex => Console.WriteLine("OnError : {0} ", ex.Message),
() => Console.WriteLine("Encountered End of Stream")
);
consoleSubscription.Dispose();
我的问题,我没有得到任何数据从服务器返回。我得到一个'空'观察到。我写了一个单元测试对我的控制器,它并带回国内的dataitems。
My issue is I am not getting any data back from the server. I get an 'Empty' observable. I wrote a unit test against my controller and it does give back the dataitems.
任何建议,请帮助。无法理解我要去的地方错了。有在服务器或客户端没有任何错误。
Any suggestions please help. Unable to understand where I am going wrong. There are no errors on server or client.
推荐答案
你是一个小雄心勃勃的的IObservable&LT期待; T&GT;
来通过线路流自动。我怕的WebAPI不会为你做的。
You're being a little ambitious expecting an IObservable<T>
to be streamed over the wire automatically. I'm afraid WebAPI won't do that for you.
你们看到的是默认的JSON序列化输出的性能的后果的IObservable&LT; T&GT;
- 有没有让您得到空括号
What you are seeing is the consequence of the default json serializer outputting the properties of IObservable<T>
- and there are none so you get empty braces.
您的单元测试工作,因为这一切都在内存中 - 没有序列化/反序列化正在发生的事情。
Your unit test works because it's all in-memory - no serialization/deserialization is happening.
有使用的Htt presponseMessage的StreamContent财产流出来,你的结果的方式可以的桥/从的IObservable&LT; T&GT;
- 但它不是真正地道的WebAPI。的WebAPI的异步支持真正目的是与服务器上的单项响应请求的异步处理,而不是不断地返回流的事件。
There are ways of using HttpResponseMessage's StreamContent property to stream results out which you could bridge to/from IObservable<T>
- but it's not really idiomatic WebApi. WebApi's async support is really aimed at asynchronous processing of requests with single-item responses on the server, not at returning continuously streaming events.
底线是,我想的WebAPI(至少在写作的时候)是错误的技术选择在这里。你是好得多看着这是专门建造了这种情况,而且包括在ASP.NET的最新版本。它拥有JavaScript和.NET客户端的支持,并且可以弥补到的IObservable&LT; T&GT;
很容易。有些人已经看过这个,比如in这个帖子的体育例如code 。
Bottom line is that I think WebApi (at least at the time of writing) is the wrong technology choice here. You are far better off looking at SignalR which is purpose built for this sort of scenario, and is included in the current release of ASP.NET. It has both javascript and .NET client support, and you can bridge to IObservable<T>
fairly easily. Some people have already looked at this, such as in this post sporting example code.
有些消息中间件就像我的渠道(编辑:既然被Terracotta收购并包装成通用消息的),和CEP解决方案,如SQL Server 有开箱的的IObservable
支持吧。
Some messaging middleware like my-Channels Nirvana ( since bought out by Terracotta and wrapped into Universal Messaging), and CEP solutions like SQL Server StreamInsight have out of box IObservable
support too.
这篇关于如何获得的IObservable从网页API回来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!