这是我的代码: (IIS Express,ASP.NET MVC5,调试)
类:
public class DOCUMENT_TYPE_GROUP : BaseEntityDms
{
[DataMember]
public int ID { get; set; }
[StringLength(100)]
[DataMember]
public string NAME { get; set; }
[DataMember]
public int? SUBJECT_ID { get; set; }
}
客户端:
[HttpPost]
public JsonResult Create(DOCUMENT_TYPE_GROUP entity)
{
try
{
if (!ModelState.IsValid)
{
return Json(new { Result = "ERROR", Message = "Form is not valid! Please correct it and try again." });
}
IGenericRepository _repository = new Dms.WcfService.GenericRepositoryService();
var serializer = new JavaScriptSerializer();
string prm = serializer.Serialize(entity);
var result = _repository.Add(prm, entity.GetType().FullName);
DOCUMENT_TYPE_GROUP addedDocumentTypeGroup = (DOCUMENT_TYPE_GROUP)serializer.Deserialize(result, entity.GetType());
return Json(new { Result = "OK", Record = addedDocumentTypeGroup });
}
catch (Exception ex)
{
return Json(new { Result = "ERROR", Message = ex.Message });
}
}
服务:
public class GenericRepositoryService : IGenericRepository
{
public string Add(string entity, string fullClassName)
{
object result;
var serializer = new JavaScriptSerializer();
var assembly = Assembly.LoadFrom(@"Dms.Library.dll");
Type type = assembly.GetType(fullClassName);
object instance = serializer.Deserialize(entity, type);
using (DmsUOW uow = new DmsUOW("DB"))
{
object rep = GetRepInstance("Dms.Data." + instance.GetType().Name + "_Repository", uow);
MethodInfo mInfo = rep.GetType().GetMethod("Add");
result = mInfo.Invoke(rep, new object[] { instance });
uow.Flush();
uow.Commit();
}
return serializer.Serialize(result);
}
private object GetRepInstance(string strFullyQualifiedName, object prm)
{
var assembly = Assembly.LoadFrom(@"Dms.Data.dll");
return assembly.CreateInstance(strFullyQualifiedName, false, BindingFlags.CreateInstance, null, new object[] { prm }, null, null);
}
}
附加:
存储库:
public class DOCUMENT_TYPE_GROUP_Repository : RepositoryEf<DOCUMENT_TYPE_GROUP>
{
public DOCUMENT_TYPE_GROUP_Repository(IUnityOfWork UnityOfWork) : base(UnityOfWork) { }
}
RepositoryEF:
public class RepositoryEf<TEntity> : IRepository<TEntity> where TEntity : class
{
protected IUnityOfWork uow;
protected DbSet<TEntity> objectSet;
protected DbContext context;
public RepositoryEf(IUnityOfWork UnitOfWork)
{
this.uow = UnitOfWork;
this.context = UnitOfWork.Context;
this.objectSet = context.Set<TEntity>();
}
public TEntity Add(TEntity entity)
{
return objectSet.Add(entity);
}
}
JavaScriptSerializer无法反序列化实体参数。我无法在调试时观看实例.-&gt; object instance = serializer.Deserialize(entity, type);
我收到错误&#34;对象的类型&#39; X&#39;无法转换为&#39; X&#39;&#34;。
,但
如果我从consoleApplication调用它可以正常工作。
控制台:
static void Main(string[] args)
{
DOCUMENT_TYPE_GROUP d = new DOCUMENT_TYPE_GROUP();
d.NAME = "Generic2";
d.SUBJECT_ID = 12;
GenericRepositoryService service = new GenericRepositoryService();
var serializer = new JavaScriptSerializer();
string prm = serializer.Serialize(d);
string s = service.Add(prm, d.GetType().FullName);
var d2 = (DOCUMENT_TYPE_GROUP)serializer.Deserialize(s, d.GetType());
}
DOCUMENT_TYPE_GROUP位于Dms.Library.dll中。所以我检查了它的版本。我检查&#34; C:\ Program Files(x86)\ IIS Express&#34; 我重建了解决方案。但它不起作用。 我也使用Newton.Json。但我得到同样的错误。 (对不起我的英文)
编辑: 如果我尝试投入服务:
public string Add(string entity, string fullClassName)
{
...
object instance = serializer.Deserialize(entity, type);
var t = (DOCUMENT_TYPE_GROUP)instance;
..
}
我收到此错误:
[A] Dms.Library.DOCUMENT_TYPE_GROUP无法转换为[B] Dms.Library.DOCUMENT_TYPE_GROUP。类型A源自&#39; Dms.Library,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null&#39;在上下文中'LoadFrom&#39;在位置&#39; C:\ Program Files(x86)\ IIS Express \ Dms.Library.dll&#39;。类型B源自&#39; Dms.Library,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null&#39;在上下文中&#39;默认&#39;在位置&#39; C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Temporary ASP.NET Files \ root \ d33982ca \ 57fe47a5 \ assembly \ dl3 \ 4ae52973 \ 571dcfde_92b5d001 \ Dms.Library.dll&#39;。