title: Raven DB JsonSerializationException Could not load assembly
body: <p>When I try to retrieve a document containing a list of entities whose types are found in a dynamically loaded DLL, I get a <code>JSONSerialisation</code> exception, which states that it cannot find the type in the dynamically loaded DLL: <code>Could not load assembly 'Sandbox_One'</code></p>
  
  <p>I am using RavenDB (build 2681) to store entities that are created using <a href="http://msdn.microsoft.com/en-us/vstudio/roslyn.aspx" rel="nofollow">Roslyn</a>, although I don't think that is an issue here. Each of the entities inherit a non-dynamic interface <code>IEntity</code>. The Raven Session is being created either side of a .NET MVC 4 Action (as per the <a href="http://ravendb.net/kb/3/using-ravendb-in-an-asp-net-mvc-website" rel="nofollow">Raven example</a>) and I am able to save the dynamic entity into a list of <code>IEntity</code>.</p>
  
  <p>When I try to load the list of entities back out of the document, I get the serialisation error. At the moment, I only have a single instance (Blue Eyes) of a single entity type (Customer).</p>
  
  <h1>Observations</h1>
  
  <ul>
  <li>In the database, the entity looks exactly as I would expect:</li>
  </ul>
  
  <pre>
  {
    "Entities": [
      {
        "$type": "Prometheus.Dynamic.Sandbox_One.Customer, Sandbox_One",
        "Name": "Customer",
        "Eye_colour": "Blue",
        "Age": 9.0,
        "Id": "b0937393-b1bf-4bcb-97d7-1aea7a96e881"
      }
    ]
  }
  </pre>
  
  <ul>
  <li><p>At the point of Loading the entity, I check the <code>AppDomain.CurrentDomain</code> for the loaded assemblies and <code>Prometheus.Dynamic.Sandbox_One</code> with the <code>Customer</code> type is there. </p></li>
  <li><p>The <code>Realm</code> object has a reference to the dynamically loaded Assembly and I am able to reflect types from it like so:</p>
  
  <p><code>var customer = Activator.CreateInstance(realm.Assembly.GetTypes().First());</code></p></li>
  <li><p>The Assembly is loaded inside the MVC action, after the RavenDB session was started. Would that make a difference? I imagine not as I expect the JSON.net serializer is looking in the <code>AppDomain</code>.</p></li>
  </ul>
  
  <hr>
  
  <h1>Code and Exception</h1>
  
  <p>Full stack trace of the Exception:</p>
  
  <pre>[JsonSerializationException: Could not load assembly 'Sandbox_One'.]
     Raven.Imports.Newtonsoft.Json.Serialization.DefaultSerializationBinder.GetTypeFromTypeNameKey(TypeNameKey typeNameKey) in c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\DefaultSerializationBinder.cs:69
     Raven.Imports.Newtonsoft.Json.Utilities.ThreadSafeStore`2.AddValue(TKey key) in 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) in 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) in c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:473
  
  [JsonSerializationException: Error resolving type specified in JSON 'Prometheus.Dynamic.Sandbox_One.Customer, Sandbox_One'. Path 'Entities[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) in 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) in 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) in 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\Serialization\JsonSerializerInternalReader.cs:1132
     Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id) in 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) in 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) in 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\Serialization\JsonSerializerInternalReader.cs:1593
  
  [JsonSerializationException: Could not read value for property: Entities]
     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\Serialization\JsonSerializerInternalReader.cs:1602
     Raven.Imports.Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) in 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) in 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) in c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\Serialization\JsonSerializerInternalReader.cs:164
     Raven.Imports.Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) in c:\Builds\RavenDB-Stable\Imports\Newtonsoft.Json\Src\Newtonsoft.Json\JsonSerializer.cs:565
     Raven.Client.Document.InMemoryDocumentSessionOperations.ConvertToEntity(Type entityType, String id, RavenJObject documentFound, RavenJObject metadata) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:457
     Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity(Type entityType, String key, RavenJObject document, RavenJObject metadata, Boolean noTracking) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:404
     Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity(Type entityType, JsonDocument documentFound) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:388
     Raven.Client.Document.InMemoryDocumentSessionOperations.TrackEntity(JsonDocument documentFound) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\InMemoryDocumentSessionOperations.cs:343
     Raven.Client.Document.SessionOperations.LoadOperation.Complete() in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\SessionOperations\LoadOperation.cs:61
     Raven.Client.Document.DocumentSession.Load(String id) in c:\Builds\RavenDB-Stable\Raven.Client.Lightweight\Document\DocumentSession.cs:230
     Prometheus.Core.DomainServices.DataManager.GetDataForRealm(IDocumentSession session, Realm realm) in c:\TeamProjectsCloud\Prometheus\Prometheus.Core\DomainServices\DataManager.cs:48
     Prometheus.Portal.Controllers.DataController.Index(String entityName) in c:\TeamProjectsCloud\Prometheus\Prometheus.Portal\Controllers\DataController.cs:23
     lambda_method(Closure , ControllerBase , Object[] ) +192
     System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +274
     System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +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</pre>
  
  <p>The controller (I've bolded the call where the Entities are loaded):
  NB <code>RavenSession</code> is inherited from a base controller exactly as in the <a href="http://ravendb.net/kb/3/using-ravendb-in-an-asp-net-mvc-website" rel="nofollow">MVC site example</a></p>
  
  <pre><code>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 =&gt; x.Name == entityName);
       viewModel.Data = dataManager.GetDataForRealm(RavenSession, realm); // Call to loading method
  
       return View(viewModel);
  }
  </code></pre>
  
  <p>The Load call that is throwing the exception:</p>
  
  <pre><code>    public List&lt;IEntity&gt; GetDataForRealm(IDocumentSession session, Realm realm)
      {
          var realmData = session.Load&lt;RealmData&gt;(realm.RealmDataId); // Exception thrown
          return realmData.Entities;
      }
  </code></pre>
  
  <p>The Realm Data class:</p>
  
  <pre><code>public class RealmData
  {
      public RealmData()
      {
          Entities = new List&lt;IEntity&gt;();
      }
      public List&lt;IEntity&gt; Entities { get; set; }
  }
  </code></pre>
  
  <p>Thank you in advance for your help!</p>