Reputation: 23
I created WCF service that returned a collection of product entities from an entity frame work model .that product has self-reference .I consume data from WCF service in silverlight application . i use Asynchronous methods . product entity was modeled this form of:
Public partial class Product
{
Public Product(){
this.product_11=new HashSet<Product>;
}
[DataMember]
public int Id{get; set;}
[DataMember]
public Nullable<int> subPro{get; set;}
[DataMember]
Public virtual Icollection<Product> product_11{get; set;}
[DataMember]
Public virtual Product product_12{get; set;}
}
and i use that in service methode
productEntity ef=new productEntity();
[OperationContract]
Public IEnumerable<Product> Getproduct()
{
return ef.Product;
}
in run time whene call service methode i get Time out error
"the HTTP request has ... exceeded the allotted timeout"
Upvotes: 2
Views: 359
Reputation: 5865
I think the problem lies in the usage of IQueryable as the return type in combination with WCF services.
[OperationContract]
Public IEnumerable<Product> Getproduct()
{
return ef.Product.ToList();
}
In this case query is executed with the call of the ToList()
method, thus the WCF has not to execute the IQueryable.
The second part is that you might run into serialization problems, when returning objects of the model from your context. Because EF injects the feature for lazy loading into your model classes by inheritance.
You should switch off lazy loading for your current context in the service method:
context.ContextOptions.LazyLoadingEnabled = false;
context.ContextOptions.ProxyCreationEnabled = false;
This is described in this link
The next thing is that the serializer, which translates the model classes into XML-message has troubles with circular references. Here is an example which could lead to a problem:
public class A
{
public string PropA { get; set; }
public virtual B PropB { get; set; }
}
public class B
{
public string PropC { get; set; }
public virtual A PropD { get; set; }
}
You can avoid this by using the CyclicReferencesAware
attribute for the OperationContract
method:
[OperationContract]
[CyclicReferencesAware(true)]
Public IEnumerable<Product> Getproduct()
{
return ef.Product.ToList();
}
Hope this helps you!
Upvotes: 1