[英]Compiling transformation: The type 'Object' is defined in an assembly that is not referenced
[英]WCF Returning Object of a type from a Referenced Assembly
我看了一些关于这个主题的类似帖子,并没有找到我想要的东西所以我会解释我在做什么以及我正在运行的问题。
我有一个MVC3应用程序和一个保存我的模型数据的类库。 例如,我的域模型项目在这里有一个名为CLUser的类:
public class CLUser
{
public int ID { get; set; }
[Display(Name = "User Name")]
[StringLength(50, ErrorMessage = Util.ERRORMESSAGE_STRING_LENGTH_50)]
public string UserName { get; set; }
public string Password { get; set; }
public DateTime PasswordExpiration { get; set; }
public bool LockedOut { get; set; }
public string LockedOutReason { get; set; }
[Display(Name = "Security Question")]
public string SecurityQuestion { get; set; }
[Display(Name = "Security Answer")]
public string SecurityAnswer { get; set; }
[Display(Name = "Current Status:")]
public int Standing { get; set; }
public int MerchantID { get; set; }
public int PartnerID { get; set; }
public DateTime CreatedDtTm { get; set; }
public DateTime UpdatedDtTm { get; set; }
public List<CLPermission> UserPerms { get; set; }
}
然后,我有一个WCF服务,它引用具有模型CLUser的相同项目。 所以在我的WCF中我有一个叫做的操作合同
[OperationContract]
GetUser(string userName, string password);
因此暴露在MVC项目中。
所以这就是问题所在。 我想将CLUser对象设置为等于我的GetUser的WCF服务调用。 看起来像这样:
ServiceClient MyService = new ServiceClient();
CLUser Usr = MyService.GetUser(userName, password);
但是,当我这样做时,我得到一个错误:
无法将Auth.CLUser类型隐式转换为Auth.CLUser [C:\\ .... \\ Auth.DLL]
如果WCF和MVC应用程序都引用相同的DLL,它们是否能够使用相同的对象?
如果您从不同位置加载相同的程序集,则根据使用的加载上下文,类型可能不会被视为相等。 身份有时不仅仅是一个强大的名字。
正如@JohnSaunders在问题评论中所提到的那样,请确保您引用了相同版本的通用程序集。 如果模型不同,则反序列化WCF消息传递将不起作用。
看起来你正在使用ServiceClient
的生成代理,它将创建一个结构相同但不同类型的CLUser
(该类型将存在于不同的命名空间中),并尝试使用其返回值作为共享程序集的引用键入CLUser
。 如果您的项目中有ServiceReference到ServiceClient,则会导致此问题。 您可以使用Automapper之类的内容将生成的代理类型中的内容映射到首选共享类型,或使用ChannelFactory创建客户端连接,然后返回共享程序集CLUser类型。 我个人更喜欢ChannelFactory
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.