使用 Linq 将 IList 作为 REST API 返回
过去 2 年我一直在使用 REST(使用 WCF),但对实体模型和实体模型还是新手。然而 Linq 使用了 nHibernate。
我用以下方法做了一个简单的服务:-
NorthwindModel [具有 Product、Order 和 Order_Detail 表的数据模型] ProductService 通过特定配置公开为 REST(不使用 WebServiceHostFactory)
服务合同:-
[ServiceContract]
public interface IProductService
{
[OperationContract]
[WebGet(
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)
]
IList<Product> GetProducts();
}
服务实现:-
public class ProductService : IProductService
{
#region IProductService Members
public IList<Product> GetProducts()
{
IList<Product> prodList = new List<Product>();
NorthwindEntities ne = new NorthwindEntities();
var query = from category in ne.Products select category;
prodList = query.ToList<Product>();
return prodList;
}
#endregion
}
服务标签:-
<services>
<service name="HellooData.ProductService" behaviorConfiguration="wcfJSONBehavior">
<endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding"
contract="HellooData.IProductService" bindingConfiguration="RESTBINDING">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="webHttpBinding" contract="IMetadataExchange" bindingConfiguration="RESTBINDING" />
</service>
</services>
绑定:-
<webHttpBinding>
<binding name="RESTBINDING" maxReceivedMessageSize="2147483647" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</webHttpBinding>
尝试从浏览器调用服务后,我得到“错误 324 (net::ERR_EMPTY_RESPONSE) :服务器关闭了连接,没有发送任何数据” Chrome 中出现错误,Mozilla 中出现空白页面。
已尝试调试,但数据已正确获取(77 条记录)并返回,没有任何异常。我对数据的配置或大小有疑问,但不确定。尝试过 篡改结果集(只有 2 个而不是 77 个)结果仍然相同。
有人可以指出出了什么问题吗?
I am working with REST (using WCF) from past 2 years but new to Entity Model & Linq, however have used nHibernate.
I made a simple service with folloiwng :-
NorthwindModel [A data model having Product, Order and Order_Detail tables]
ProductService exposed as REST with specific config (not using WebServiceHostFactory)
Service Contract :-
[ServiceContract]
public interface IProductService
{
[OperationContract]
[WebGet(
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)
]
IList<Product> GetProducts();
}
Service Implementation :-
public class ProductService : IProductService
{
#region IProductService Members
public IList<Product> GetProducts()
{
IList<Product> prodList = new List<Product>();
NorthwindEntities ne = new NorthwindEntities();
var query = from category in ne.Products select category;
prodList = query.ToList<Product>();
return prodList;
}
#endregion
}
Service Tag :-
<services>
<service name="HellooData.ProductService" behaviorConfiguration="wcfJSONBehavior">
<endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding"
contract="HellooData.IProductService" bindingConfiguration="RESTBINDING">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="webHttpBinding" contract="IMetadataExchange" bindingConfiguration="RESTBINDING" />
</service>
</services>
Binding : -
<webHttpBinding>
<binding name="RESTBINDING" maxReceivedMessageSize="2147483647" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</webHttpBinding>
After trying to call the service from the browser i get "Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data" error in Chrome and an empty page in Mozilla.
Have tried to debug but data is fetched properly (77 records) and returned without any exception. I somehow have a doubt in config or size of the data but not sure. Did try
to tamper the result set (only 2 instead of 77) result was still the same.
Can someone indicate what's going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有这个确切的问题;就我而言,我在 SQL 控制器完成使用数据库上下文之前就对其进行了处理。
I had this exact issue; in my case, I was disposing the database context before my SQL controller finished using it.