当我尝试检索包含在动态加载的 DLL 中找到其类型的实体列表的文档时,我得到一个JSONSerialisation
异常,指出它在动态加载的 DLL 中找不到类型:Could not load assembly 'Sandbox_One'
我正在使用 RavenDB (build 2681) 来存储使用Roslyn创建的实体,尽管我认为这不是问题。每个实体都继承一个非动态接口IEntity
。Raven Session 是在 .NET MVC 4 Action 的任一侧创建的(根据Raven 示例),我能够将动态实体保存到IEntity
.
当我尝试从文档中加载实体列表时,出现序列化错误。目前,我只有一个实体类型(客户)的实例(蓝眼睛)。
观察
- 在数据库中,实体看起来完全符合我的预期:
{ “实体”:[ { "$type": "Prometheus.Dynamic.Sandbox_One.Customer, Sandbox_One", “名称”:“客户”, "Eye_colour": "蓝色", “年龄”:9.0, “ID”:“b0937393-b1bf-4bcb-97d7-1aea7a96e881” } ] }
在加载实体时,我检查了
AppDomain.CurrentDomain
加载的程序集并且Prometheus.Dynamic.Sandbox_One
类型Customer
在那里。该
Realm
对象具有对动态加载的程序集的引用,我可以像这样反映它的类型:var customer = Activator.CreateInstance(realm.Assembly.GetTypes().First());
在启动 RavenDB 会话后,程序集在 MVC 操作中加载。那会有什么不同吗?我想不像我期望的 JSON.net 序列化程序正在查找
AppDomain
.
代码和异常
异常的完整堆栈跟踪:
[JsonSerializationException:无法加载程序集“Sandbox_One”。] Raven.Imports.Newtonsoft.Json.Serialization.DefaultSerializationBinder.GetTypeFromTypeNameKey(TypeNameKey typeNameKey) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\DefaultSerializationBinder.cs:69 Raven.Imports.Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Utilities\ThreadSafeStore.cs:62 Raven.Imports.Newtonsoft.Json.Serialization.DefaultSerializationBinder.BindToType(String assemblyName, String typeName) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\DefaultSerializationBinder.cs:119 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadSpecialProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id) 在 c:\Builds\RavenDB-Stable \Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:473 [JsonSerializationException:解析 JSON 'Prometheus.Dynamic.Sandbox_One.Customer,Sandbox_One' 中指定的类型时出错。路径“实体 [0].$type”。] Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadSpecialProperties(JsonReader reader, Type& objectType, JsonContract& contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue, Object& newValue, String& id) 在 c:\Builds\RavenDB-Stable \Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:526 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json \Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:344 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json \Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:238 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IWrappedCollection WrappedList, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id) in c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json \序列化\JsonSerializerInternalReader.cs:1132 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\ Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:572 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json \Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:240 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\ Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:692 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) in c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json \序列化\JsonSerializerInternalReader.cs:1593 [JsonSerializationException:无法读取属性值:实体] Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) in c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json \序列化\JsonSerializerInternalReader.cs:1602 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json \Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:368 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json \Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:238 Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs :164 Raven.Imports.Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) 在 c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\JsonSerializer.cs:565 c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:457 中的 Raven.Client.Document.InMemoryDocumentSessionOperations.ConvertToEntity(类型 entityType,字符串 id,RavenJObject documentFound,RavenJObject 元数据) c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:404 中的 Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity(类型 entityType,字符串键,RavenJObject 文档,RavenJObject 元数据,布尔 noTracking) Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity(Type entityType, JsonDocument documentFound) 在 c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:388 Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity(JsonDocument documentFound) 在 c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:343 c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\SessionOperations\LoadOperation.cs:61 中的 Raven.Client.Document.SessionOperations.LoadOperation.Complete() c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\DocumentSession.cs:230 中的 Raven.Client.Document.DocumentSession.Load(String id) Prometheus.Core.DomainServices.DataManager.GetDataForRealm(IDocumentSession session, Realm realm) 在 c:\TeamProjectsCloud\Prometheus\Prometheus.Core\DomainServices\DataManager.cs:48 c:\TeamProjectsCloud\Prometheus\Prometheus.Portal\Controllers\DataController.cs:23 中的 Prometheus.Portal.Controllers.DataController.Index(String entityName) lambda_method(闭包,ControllerBase,对象[])+192 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 参数) +274 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 参数) +39 System.Web.Mvc.Async.c__DisplayClass39.b__33() +120 System.Web.Mvc.Async.c__DisplayClass4f.b__49() +452 System.Web.Mvc.Async.c__DisplayClass37.b__36(IAsyncResult asyncResult) +15 System.Web.Mvc.Async.c__DisplayClass2a.b__20() +33 System.Web.Mvc.Async.c__DisplayClass25.b__22(IAsyncResult asyncResult) +240 System.Web.Mvc.c__DisplayClass1d.b__18(IAsyncResult asyncResult) +28 System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.Mvc.c__DisplayClass8.b__3(IAsyncResult asyncResult) +42 System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +15 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288
控制器(我已将加载实体的调用加粗):NBRavenSession
继承自基本控制器,与MVC 站点示例中的完全一样
public ActionResult Index(string entityName)
{
var viewModel = new DataViewModel();
var realm = realmManager.GetRealm(RavenSession, viewModel.SelectedRealm);
viewModel.Entities = realmManager.GetEntities(realm);
viewModel.CurrentEntity = viewModel.Entities.SingleOrDefault(x => x.Name == entityName);
viewModel.Data = dataManager.GetDataForRealm(RavenSession, realm); // Call to loading method
return View(viewModel);
}
引发异常的 Load 调用:
public List<IEntity> GetDataForRealm(IDocumentSession session, Realm realm)
{
var realmData = session.Load<RealmData>(realm.RealmDataId); // Exception thrown
return realmData.Entities;
}
领域数据类:
public class RealmData
{
public RealmData()
{
Entities = new List<IEntity>();
}
public List<IEntity> Entities { get; set; }
}
预先感谢您的帮助!