我的Web API由3个类组成:CustomTask,Payment和Client。
CustomTask引用(属于)Client。
付款参考CustomTask。
我添加了测试客户端,一切都很好。但是,当我添加与此客户端关联的CustomTask时,我在Web浏览器中列出错误时出错:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'System.Data.Entity.DynamicProxies.CustomTask_C8E7E37C41E5E531B4F79887952391B7F9896801C32E9876AA3C63332F3B5A9A' with data contract name 'CustomTask_C8E7E37C41E5E531B4F79887952391B7F9896801C32E9876AA3C63332F3B5A9A:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer 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 the serializer.
</ExceptionMessage>
<ExceptionType>
System.Runtime.Serialization.SerializationException
</ExceptionType>
<StackTrace>
at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle) at WriteArrayOfCustomTaskToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract ) at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()
</StackTrace>
</InnerException>
</Error>
我该如何解决这个问题?
修改
CustomTask.cs
public class CustomTask
{
public CustomTask() { }
public int ID { get; set; }
[ForeignKey("Client")]
[Required]
public int ClientID { get; set; }
public virtual Client Client { get; set; }
[Required]
public string Technology { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
[Required]
public DateTime CreateDate { get; set; }
[Required]
public DateTime DueDate { get; set; }
[Required]
public long EstimatedTime { get; set; }
public long RealTime { get; set; }
[Required]
public decimal Price { get; set; }
[DefaultValue(false)]
[Required]
public bool Finished { get; set; }
[DefaultValue(false)]
[Required]
public bool Lessons { get; set; }
public string ArchiveID { get; set; }
}
Client.cs:
public class Client
{
public Client() { }
public int ID { get; set; }
[Required]
public string Names { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string Info { get; set; }
}
我的ClientsController
和CustomTasksController
由Visual Studio生成。
ClientsController
返回漂亮的XML,使用WinForms编写的客户端应用程序也可以顺序化,没有任何问题:
<ArrayOfClient xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/...">
<Client>
<Address/>
<Email/>
<ID>1</ID>
<Info/>
<Names>saddasdasd</Names>
<Phone/>
</Client>
<Client>
<Address/>
<Email/>
<ID>2</ID>
<Info/>
<Names>saddasdasd</Names>
<Phone/>
</Client>
</ArrayOfClient>
修改 我刚刚发现,问题只发生在我的Web API部署在Azure中,本地一切正常。
修改
我找到了答案,解决方案是在部署到Azure时向连接字符串添加MultipleActiveResultSets=True
。