Error creating XML document. Job type not expected

I am writing a web application using MVC3, but when I try to pass the object to the controller and show it, it does not seem to recognize the type or anything else.

I have a Job object, a JobService returns a Job as follows:

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

In the WebService, I call View as follows:

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

The controller then 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 he throws this error:

System.Web.Services.Protocols.SoapException: The server could not process the request. ---> System.InvalidOperationException: An error occurred while generating the XML document. ---> System.InvalidOperationException: Type System.Data.Entity.DynamicProxies.Job_55765AEC3BD02AFD7E0527408ED5746E1054965A59B82A127B5A688C19C61D5B was not expected. Use the XmlInclude or SoapInclude attribute to indicate types that are not known statically. in Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write9_Job (String n, String ns, Job o, Boolean isNullable, Boolean needType) in Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write18_GetJobRespon) .Serialization.GeneratedAssembly.ArrayOfObjectSerializer13.Serialize (Object objectToSerialize, XmlSerializationWriter writer) in System.Xml.Serialization.XmlSerializer.Serialize (XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespace, String encodingStyle end, String --- in System.Xml.Serialization.XmlSerializer.Serialize (XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) in System.Web.Services.Protocols.SoapServerProtocol.WriteReturns (Object [] returnValues, Stream outputStream ) in System.Web.Services.Prot ocols.WebServiceHandler.WriteReturns (Object [] returnValues) in System.Web.Services.Protocols.WebServiceHandler.Invoke () --- End of the internal exception stack trace ---

At first I passed GetJobResponse service, but I tried to make it as simple as possible in order to make it work, and I still cannot figure it out. I saw that there are other issues suggesting using XmlInclude and stuff, but this 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 task returned by View in the test, the test passes, so I think the problem comes from my web service.

Please help meeee: '(

+4
source share
2 answers

I think the problem is that you are using the Entity Framework and when it receives the Job object, it creates a dynamic proxy for the Job class.

I solved this problem earlier by adding the following to the constructor of my DataContext

 public JobDataContext() : base("ConnectionString") { this.Configuration.ProxyCreationEnabled = false; } 
+2
source

I had a similar problem since I was trying to XML serialize / deserialize some custom classes. I had a parent class and a child class. Both 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 XmlSerializer when you encounter an object of type Parent and you need to serialize it, also think that it might be an instance of type child. It worked for me.

So, I think you should add the correct serialization attributes to the GetJobRequest and Job class.

0
source

Source: https://habr.com/ru/post/1496181/


All Articles