开发者

Returning IList as a REST API using Linq

开发者 https://www.devze.com 2023-03-04 11:21 出处:网络
I am working with REST (using WCF) from past 2 years but new to Entity Model & Linq, however have used nHibernate.

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?


I had this exact issue; in my case, I was disposing the database context before my SQL controller finished using it.

0

精彩评论

暂无评论...
验证码 换一张
取 消