我有一个托管在Azure Web 角色上的无状态后端服务,它查询实体框架容器等。客户端使用WCF和wsHttpBinding
. 客户端在调用某些方法时从后端收到超时,即使我在某些情况下解决了这个问题,我似乎无法查明根本原因。
到目前为止,这是我所知道的:
- 失败的方法似乎是返回某些类型的集合的方法。这包括
IEnumerable<T>
从 LINQ 查询获得的结果,以及MembershipUserCollection
. - 在某些情况下,解决方法是添加
.ToList()
到要返回的集合中。 - 仅当后端托管在 Azure 中时,这些方法才会失败。
- Azure 负载均衡器似乎不是原因,而是症状:每次方法超时,日志都会显示它实际上早在 1 分钟限制之前就退出了。
这是失败的方法之一:
WCF接口:
[OperationContract(Action = "http://tempuri.org/IBackendService/GetAllUsers", ReplyAction = "http://tempuri.org/IBackendService/GetAllUsersResponse")]
MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords);
WCF 实现(简化):
protected UserRepository Users;
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
// Get all users in the page range specified.
var userEntities = Users.GetAll();
var users = new MembershipUserCollection();
userEntities = userEntities.GetPagedRange(pageIndex, pageSize).ToList();
userEntities.ForEach(ue => users.Add(Mappings.Map<UserEntity, User>(ue)));
totalRecords = userEntities.Count();
return users;
}
Mappings是一个使用 AutoMapper 定义数据和业务对象之间映射规则的类。User
继承自MembershipUser
。以下是它的相关内容:(WIP。请原谅混乱。)
Mapper.CreateMap<UserEntity, User>()
.ConstructUsing(ue => new User
(
ue.UserId,
ue.ProviderName,
ue.UserName,
ue.Email,
ue.PasswordQuestion,
ue.IsApproved,
ue.IsLockedOut,
ue.CreationDate,
ue.LastLoginDate.HasValue ? ue.LastLoginDate.Value : DateTime.MinValue,
ue.LastActivityDate.HasValue ? ue.LastActivityDate.Value : DateTime.MinValue,
ue.LastPasswordChangedDate.HasValue ? ue.LastPasswordChangedDate.Value : DateTime.MinValue,
ue.LastLockoutDate.HasValue ? ue.LastLockoutDate.Value : DateTime.MinValue,
ue.Comment,
ue.Customers.Select(Map<CustomerEntity, Customer>).ToList(),
ue.Roles.Select(Map<RoleEntity, Role>).ToList(),
ue.Roles.SelectMany(r => r.Activities).Select(Map<ActivityEntity, Activity>).ToList()
))
.IgnoreAllNonExisting();
Mapper.CreateMap<User, UserEntity>()
.ForMember(ue => ue.Comment, opt => opt.MapFrom(u => u.Comment))
.ForMember(ue => ue.CreationDate, opt => opt.MapFrom(u => u.CreationDate))
.ForMember(ue => ue.Email, opt => opt.MapFrom(u => u.Email))
.ForMember(ue => ue.IsApproved, opt => opt.MapFrom(u => u.IsApproved))
.ForMember(ue => ue.IsLockedOut, opt => opt.MapFrom(u => u.IsLockedOut))
.ForMember(ue => ue.LastActivityDate, opt => opt.MapFrom(u => u.LastActivityDate.Equals(DateTime.MinValue) ? (DateTime?)null : u.LastActivityDate))
.ForMember(ue => ue.LastLockoutDate, opt => opt.MapFrom(u => u.LastLockoutDate.Equals(DateTime.MinValue) ? (DateTime?)null : u.LastLockoutDate))
.ForMember(ue => ue.LastLoginDate, opt => opt.MapFrom(u => u.LastLoginDate.Equals(DateTime.MinValue) ? (DateTime?)null : u.LastLoginDate))
.ForMember(ue => ue.LastPasswordChangedDate,
opt => opt.MapFrom(u => u.LastPasswordChangedDate.Equals(DateTime.MinValue) ? (DateTime?)null : u.LastPasswordChangedDate))
.ForMember(ue => ue.PasswordQuestion, opt => opt.MapFrom(u => u.PasswordQuestion))
.ForMember(ue => ue.ProviderName, opt => opt.MapFrom(u => u.ProviderName))
.ForMember(ue => ue.UserId, opt => opt.MapFrom(u => (int)u.ProviderUserKey))
.ForMember(ue => ue.UserName, opt => opt.MapFrom(u => u.UserName))
.ForMember(ue => ue.Password, opt => opt.Ignore())
.ForMember(ue => ue.PasswordAnswer, opt => opt.Ignore())
.ForMember(ue => ue.ApplicationName, opt => opt.Ignore())
.ForMember(ue => ue.Roles, opt => opt.Ignore())
.ForMember(ue => ue.Customers, opt => opt.Ignore())
.IgnoreAllNonExisting();
IgnoreAllNonExisting是一种扩展方法:
public static IMappingExpression<TSource, TDestination> IgnoreAllNonExisting<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression)
{
var sourceType = typeof(TSource);
var destinationType = typeof(TDestination);
var existingMaps = Mapper.GetAllTypeMaps().First(x => x.SourceType == sourceType && x.DestinationType == destinationType);
foreach (var property in existingMaps.GetUnmappedPropertyNames())
{
expression.ForMember(property, opt => opt.Ignore());
}
return expression;
}
我最好的猜测是这是 WCF 层中的序列化问题,但我不知道是什么触发了它。除此之外,我完全一无所知。
我还应该检查什么?您需要更多信息吗?
编辑:为澄清添加了 moar 片段。