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