Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error generating XML document. The type Job was not expected

I am writing a web app using MVC3 but when trying to pass an object to the controller and show it it doesn't seem to recognize the type or something.

I have a Job object, and a JobService returning a Job like this:

public Job View(int jobId)
{
    Job job=_jobRepository.Jobs.Where(x => x.Id == jobId).FirstOrDefault();
    return job;
}

Within the WebService I call View like this:

[WebMethod]
public Job GetJob(GetJobRequest getJobRequest)
{
    var getJobResponse = new GetJobResponse();
    getJobResponse.Job = _jobService.View(getJobRequest.Id);
    return getJobResponse.Job;
}

Then the Controller calls this:

public class JobsController : Controller
{
    public ActionResult Index()
    {
        var jobModel = new JobModel();

        using (var webServiceSoapClient = new WebServiceSoapClient())
        {
            var getJobRequest = new GetJobRequest();
            getJobRequest.Id = 26038;
            jobModel.Job = webServiceSoapClient.GetJob(getJobRequest);
        }
        return View(jobModel);
    }
}

And it's throwing this error:

System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Data.Entity.DynamicProxies.Job_55765AEC3BD02AFD7E0527408ED5746E1054965A59B82A127B5A688C19C61D5B was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write9_Job(String n, String ns, Job o, Boolean isNullable, Boolean needType) at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write18_GetJobResponse(Object[] p) at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer13.Serialize(Object objectToSerialize, XmlSerializationWriter writer) at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) --- End of inner exception stack trace --- at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) at System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream) at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues) at System.Web.Services.Protocols.WebServiceHandler.Invoke() --- End of inner exception stack trace ---

At first I was passing a GetJobResponse to the service but I tried to make it as simple as possible now to get it working and I still can't figure it out. I have seen that there are other questions suggesting the use of XmlInclude and stuff but it still doesn't work.

Applying this:

public string SerializeObjectToXMLString(object theObject)
{
    // Exceptions are handled by the caller

    using (System.IO.MemoryStream oStream = new System.IO.MemoryStream())
    {
        System.Xml.Serialization.XmlSerializer oSerializer = new System.Xml.Serialization.XmlSerializer(theObject.GetType());

        oSerializer.Serialize(oStream, theObject);

        return Encoding.Default.GetString(oStream.ToArray());
    }
}

To the Job returned by View in a test it passes the test so I guess the problem comes from my webservice.

Please help meeee :'(

like image 260
minaMihaela Avatar asked Oct 22 '22 03:10

minaMihaela


1 Answers

I think the issue is because you are using Entity Framework and when it gets the Job object it is creating a Dynamic Proxy of the Job class.

I have solved this issue before by adding the following to the constructor of my DataContext

public JobDataContext()
            : base("ConnectionString")
        {
            this.Configuration.ProxyCreationEnabled = false;
        }
like image 200
Croberts Avatar answered Oct 30 '22 22:10

Croberts