在VS2010中,我的MSTest测试运行得很好。
在VS2012中运行时出现错误。该测试使用自定义业务主体设置Csla.ApplicationContext.User。当要求EntityFramework提供新的ObjectContext时,我收到一个SerializationException,表示找不到我的自定义业务主体类型。
使用EntityFramework的所有测试在通过VS2012的测试运行器或Resharper7的测试运行器运行时都会失败。我尝试过NCrunch的测试跑步者,他们都通过了。
如何解决此问题?
答案 0 :(得分:3)
我找到了真正的问题。 VS2012在单独的AppDomain中运行测试,我们的数据访问层通过Reflection加载。仍然不确定为什么EF需要知道委托人,但我们的解决方案是在访问EF之前将我们的委托人重置为GenericPrincipal,然后放回原件。我仍在努力思考IoC容器可能会缓解这个问题
答案 1 :(得分:0)
您还应该注意.net 4.5声明主要方法。
我在VS2012上使用EF5.0,目标是.net4.5目标.net4.5
测试WindowsIdentity.GetCurrent().Name;
之间的差异
和Thread.CurrentPrincipal
我在FORMS身份验证下使用这样的小程序。 所以Windows Auth和Forms Auth可以一起玩。
并非完全相同的情况,但它确实突出了当一切正常时忽略的重要差异 值得快速阅读...... http://msdn.microsoft.com/en-us/library/system.security.claims.claimsprincipal.current
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
namespace BosIdentityManager
{
public class BosPrincipal
{
/// <summary>
/// The current principal is set during FORMS authentication. If WINDOWS auth mode is in use, Windows sets it.
/// </summary>
/// <returns> The Name from Thread.CurrentPrincipal.Identity.Name unless alternate delegate is configured</returns>
public static string GetCurrentUserName()
{
// http://msdn.microsoft.com/en-us/library/system.security.claims.claimsprincipal.current
// with forms auth and windows integrated,ClaimsPrincipal.Current will be set.
var prin = ClaimsPrincipal.Current; //normally this reverts to Thread.CurrentPrincipal, but can chnage !
return prin.Identity.Name;
}
public static string GetCurrentWindowsUserName()
{
return WindowsIdentity.GetCurrent().Name;
}
public static void SetPrincipal(BosMasterModel.Membership memb)
{
var claims = new List<Claim>(){ new Claim(ClaimTypes.Name, memb.SystemUser.UserName),
new Claim(ClaimTypes.NameIdentifier,memb.UserId.ToString()),
new Claim(ClaimTypes.Role, "SystemUser") };
var ClaimsId = new ClaimsIdentity(claims,"Forms");
var prin = new ClaimsPrincipal(ClaimsId);
Thread.CurrentPrincipal = prin;
}
}
}