1

I'm working on a web application and serializing objects using JSON.Net.now, i want to add some WCF services to this application that will be used in any platform for example android.

now, when i send a simple ajax request to web service, it's go to infinite loop and chrome console logs net::ERR_CONNECTION_RESET, but when i add DataContract to the model, this problems get to solved, but in the other forms in the entire of application, JSON.Net does not serialize objects.

Here is my codes:

[ServiceContract]
public interface IProductService
{
    [OperationContract]
    [WebInvoke(
        Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "product/grid/special?start={start}&size={size}&orderBy={orderBy}&orderByType={orderByType}&productListId={productListId}")]
    PagedResult<Product> specialGrid(int start, int size, string orderBy, string orderByType, int productListId);
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ProductService : IProductService
{
    IProductRepository productRepository;

    public PagedResult<Product> specialGrid(int start, int size, string orderBy, string orderByType, int productListId)
    {
        //return start + " " + size + " " + orderBy + " " + orderByType + " " + productListId;
        productRepository = new ProductRepository();
        SearchOption s = new SearchOption(start, size, orderBy, orderByType);
        return productRepository.getSpecialSaleProducts(s, productListId);
    }
}

[Table("Product")]
[DataContract]
public class Product : BaseEntity
{
    public string title { get; set; }
}
Rasool Ghafari
  • 4,128
  • 7
  • 44
  • 71
  • Have are you invoking your service? – Greg Apr 20 '15 at 17:55
  • With a simple jquery ajax request, i want to make these services usable in my web application (to calling with jquery ajax) and in other applications may be android application – Rasool Ghafari Apr 20 '15 at 18:09
  • 1
    Data contract serialization is [opt in](https://msdn.microsoft.com/en-us/library/ms733127%28v=vs.110%29.aspx), so if you add `[DataContract]` to your classes you need to add `[DataMember]` to each property or field you want serialized. Perhaps it would be easier to make wcf use Json.NET? See http://stackoverflow.com/questions/11003016/c-sharp-wcf-rest-how-do-you-use-json-net-serializer-instead-of-the-default-dat, http://stackoverflow.com/questions/3118504/how-to-set-json-net-as-the-default-serializer-for-wcf-rest-service, ... – dbc Apr 20 '15 at 18:15
  • It looks like your utilizing MVC, do you really need WCF? You should be able to simply Ajax a Controller, which can return a JSON Result back to you. – Greg Apr 20 '15 at 18:30
  • @Greg, my web application is asp.net web form and not MVC! – Rasool Ghafari Apr 20 '15 at 18:40
  • @RasoolGhafari Why are you getting angry, maybe tag it as Web-Forms then. – Greg Apr 20 '15 at 18:41
  • @Greg, i'm not angry, it was the exclamation sign, i'm sorry, it was my faults, i added Web-Forms tag to querstion. – Rasool Ghafari Apr 20 '15 at 18:45
  • @dbc, when i add the `[DataMember]` the wcf service works fine, but `JSON.Net` does not serialize objects as i mentioned above, is it possible to use wcf service without `[DataContract]`? – Rasool Ghafari Apr 20 '15 at 18:50
  • @dbc, i'm added the `[JsonObject(MemberSerialization.OptOut)]` annotation to `Product` class, both of `JSON.Net` and `Web Service` works fine, but some features of `JSON.Net` like `[JsonProperty]` doesn't work – Rasool Ghafari Apr 20 '15 at 18:58
  • @RasoolGhafari - you can try switching to Json.NET for wcf by following the instructions in the answers I linked. – dbc Apr 20 '15 at 19:23
  • @dbc, my problem is this: using WCF service without `[DataContract]` and `[DataMemeber]`. – Rasool Ghafari Apr 20 '15 at 19:47
  • 1
    @RasoolGhafari - my suggestion for "using WCF service without [DataContract] and [DataMemeber]" *is* to switch to Json.NET. For other suggestions, see here; http://stackoverflow.com/questions/5921635/is-datacontract-attributes-required-for-wcf – dbc Apr 20 '15 at 20:00
  • 1
    WCF is notoriously difficult to set up but once you have it working it's not too bad. If you're using web forms then I'd recommend looking into using ASMX services if you're not too set on WCF. ASMX services don't have as many features but is easily configured to send back json. Much easier to deal with. Just my two cents. – DaneEdw Apr 21 '15 at 21:29

0 Answers0