Question

I have the following table:

create table Movie
(
    id integer primary key identity(1,1),
    title varchar(40),
    synopsis varchar(200),
    movieLength integer,
    imageSmall varbinary(max),
    imageLarge varbinary(max),
    inputDate date,
);

Using ADO.NET entity model, I generated C# classes.

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace CinemaService
{
    using System;
    using System.Collections.Generic;

    public partial class Movie
    {
        public Movie()
        {
            this.Show = new HashSet<Show>();
        }

        public int id { get; set; }
        public string title { get; set; }
        public string synopsis { get; set; }
        public Nullable<int> movieLength { get; set; }
        public byte[] imageSmall { get; set; }
        public byte[] imageLarge { get; set; }
        public Nullable<System.DateTime> inputDate { get; set; }

        public virtual ICollection<Show> Show { get; set; }
    }
}

I have the following contract:

namespace CinemaService
{
    [ServiceContract]
    public interface ICinemaService
    {
        [OperationContract]
        Movie[] list_movies();
    }
}

And service:

namespace CinemaService
{
    [ServiceBehavior]
    public class CinemaService : ICinemaService
    {
        private CinemaEntities _entities;

        public CinemaService()
        {
            _entities = new CinemaEntities();
        }

        public Movie[] list_movies()
        {
            return _entities.Movie.ToArray();
        }

    }
}

The service is hosted on IIS.

When the table is empty, I am able to invoke and get results (empty result), but when there is even one row, I get an exception.

I enabled tracing in web.config, and I think the problem is that the generated classes are not serializable.

Using trace viewer, I found the following:

Exception type:

System.ServiceModel.CommunicationException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Message:

There was an error while trying to serialize parameter http://tempuri.org/:list_moviesResult. The InnerException message was 'Type 'System.Data.Entity.DynamicProxies.Movie_46EC56490AEEEA50CA29379C6E09B01C345ECBB1273756AE368BA72AA30754B5' with data contract name 'Movie_46EC56490AEEEA50CA29379C6E09B01C345ECBB1273756AE368BA72AA30754B5:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

Let me know if more details are needed.

Was it helpful?

Solution 2

Here is how I solved it:

I deleted the generated classes, and downloaded a new code generation item template: EF 6.x EntityObject Generator. (I used DBContext generator).

Now it works fine.

OTHER TIPS

Although you can achieve what you want using several methods like as described in one the comments (disable proxy generation).

If your architecture allows it I would recommend using DTOs (Data Transfer Object) together with a mapping tool like automapper.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top