完整的错误是:
{
Message: "An error has occurred.",
ExceptionMessage: "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
ExceptionType: "System.InvalidOperationException",
StackTrace: null,
InnerException: {
Message: "An error has occurred.",
ExceptionMessage: "Incorrect number of arguments for constructor",
ExceptionType: "System.ArgumentException",
StackTrace: " at System.Linq.Expressions.Expression.ValidateArgumentCount(MethodBase method, ExpressionType nodeKind, Int32 count, ParameterInfo[] pis) at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments) at System.Linq.Expressions.Expression.New(ConstructorInfo constructor, IEnumerable`1 arguments) at System.Data.Objects.ELinq.InitializerMetadata.ProjectionNewMetadata.Emit(Translator translator, List`1 propertyTranslatorResults) at System.Data.Common.Internal.Materialization.Translator.HandleLinqRecord(RecordColumnMap columnMap, InitializerMetadata initializerMetadata) at System.Data.Common.Internal.Materialization.Translator.Visit(RecordColumnMap columnMap, TranslatorArg arg) at System.Data.Query.InternalTrees.RecordColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg) at System.Data.Common.Internal.Materialization.Translator.ProcessCollectionColumnMap(CollectionColumnMap columnMap, TranslatorArg arg, ColumnMap discriminatorColumnMap, Object discriminatorValue) at System.Data.Common.Internal.Materialization.Translator.Visit(SimpleCollectionColumnMap columnMap, TranslatorArg arg) at System.Data.Query.InternalTrees.SimpleCollectionColumnMap.Accept[TResultType,TArgType](ColumnMapVisitorWithResults`2 visitor, TArgType arg) at System.Data.Common.Internal.Materialization.Translator.TranslateColumnMap[TRequestedType](QueryCacheManager queryCacheManager, ColumnMap columnMap, MetadataWorkspace workspace, SpanIndex spanIndex, MergeOption mergeOption, Boolean valueLayer) at System.Data.Common.Internal.Materialization.ShaperFactory.TypedShaperFactoryCreator`1.TypedCreate(QueryCacheManager cacheManager, ColumnMap columnMap, MetadataWorkspace metadata, SpanIndex spanInfo, MergeOption mergeOption, Boolean valueLayer) at System.Data.Common.Internal.Materialization.ShaperFactory.Create(Type elementType, QueryCacheManager cacheManager, ColumnMap columnMap, MetadataWorkspace metadata, SpanIndex spanInfo, MergeOption mergeOption, Boolean valueLayer) at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Span span, ReadOnlyCollection`1 compiledQueryParameters, AliasGenerator aliasGenerator) at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper(Object list) in c:\Dev\Releases\Working\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonArrayContract.cs:line 108 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) in c:\Dev\Releases\Working\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 128 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) in c:\Dev\Releases\Working\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 342 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty) in c:\Dev\Releases\Working\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 123 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value) in c:\Dev\Releases\Working\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalWriter.cs:line 58 at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value) in c:\Dev\Releases\Working\Newtonsoft.Json\Src\Newtonsoft.Json\JsonSerializer.cs:line 608 at System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.<WriteToStreamAsync>b__c() at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)"
}
}
这似乎与我的计算机隔离,因为我们的服务器和其他同事可以正常运行相同的代码。
我唯一的线索是我刚刚将 Visual Studio 2012 Express 与我正在运行的 VS 2010 并排安装。尽管它们都出现了相同的错误。
更令人困惑的是,json.net库是在本地引用的,并没有改变。
只有一个 web api 控制器抛出错误(联系人是一个 IQueryable,在此之前已经以多种方式过滤,但在调试器中工作直到渲染):
var r =
contacts
.Skip(offset).Take(count)
.Join(
crm.ContactViews,
c => c.Id,
cv => cv.Id,
(c, cv) => new {
contact = c,
view = cv,
userInfo = c.ContactUsers.Select(u => new {
name = u.User.UserName,
id = u.UserId
}).FirstOrDefault(),
lastCalled = crm.CallLogs
.Where(x => x.ContactId == c.Id)
.OrderByDescending(x => x.CallEnd)
.Select(log => new { date = log.CallStart, username = crm.Users.FirstOrDefault(x => x.UserId == log.Caller).UserName })
.FirstOrDefault()
}
)
.AsEnumerable()
.Select(c => new
{
id = c.contact.Id,
firstName = c.contact.FirstName,
lastName = c.contact.LastName,
dateCreated = formatDate(c.contact.DateCreated),
score = c.contact.Score,
companyName = c.view.Company,
phone = c.view.Phone,
email = c.view.Email,
street = c.view.Street,
street2 = c.view.Street2,
city = c.view.City,
state = c.view.State,
zip = c.view.Zip,
assignedUserName = c.userInfo != null ? c.userInfo.name : null,
assignedUserId = c.userInfo != null ? c.userInfo.id : (Guid?)null,
dateLastCalled = c.lastCalled != null ? formatDate(c.lastCalled.date) : null,
lastCalledBy = c.lastCalled != null ? c.lastCalled.username : null
});