简体   繁体   中英

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 :'(

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;
        }

I had a similar problem as I was trying to XML-serialize/deserialize some custom classes. I had a parent class and a child class. Both of them were declared [Serializable] . I met the same error you are reporting and my solution was to add this attribute to the parent class: [XmlInclude(typeof(Child))]. I think this declaration tells the XmlSerializer, when you meet an object of type Parent, and you have to serialize it, please also consider it could be an instance of type child. It worked for me.

So I think you should add the proper serialization attributes to your GetJobRequest class and Job .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM