मैं async कार्यों के साथ काम कर रहा हूँ और इससही रास्ते का इंतजार
public class UserService : IUserService
{
public ILocPrincipal Current
{
get { return HttpContext.Current.User as ILocPrincipal; }
}
}
public class ChannelService : IDisposable
{
// In the service layer
public ChannelService()
: this(new Entities.LocDbContext(), new UserService())
{
}
public ChannelService(Entities.LocDbContext locDbContext, IUserService userService)
{
this.LocDbContext = locDbContext;
this.UserService = userService;
}
public async Task<ViewModels.DisplayChannel> FindOrDefaultAsync(long id)
{
var currentMemberId = this.UserService.Current.Id;
// do some async EF request …
}
}
// In the controller
[Authorize]
[RoutePrefix("channel")]
public class ChannelController : BaseController
{
public ChannelController()
: this(new ChannelService()
{
}
public ChannelController(ChannelService channelService)
{
this.ChannelService = channelService;
}
// …
[HttpGet, Route("~/api/channels/{id}/messages")]
public async Task<ActionResult> GetMessages(long id)
{
var channel = await this.ChannelService
.FindOrDefaultAsync(id);
return PartialView("_Messages", channel);
}
// …
}
मैं है कोड हाल ही में पुनर्संशोधित तरह HttpContext.Current.User उपयोग करते हैं, जैसा कि पहले मैं देने के लिए किया था सेवा के लिए प्रत्येक कॉल पर उपयोगकर्ता। अब मैं इस आलेख को http://trycatchfail.com/blog/post/Using-HttpContext-Safely-After-Async-in-ASPNET-MVC-Applications.aspx पढ़ता हूं और मुझे यकीन नहीं है कि मेरा कोड अभी भी काम करता है या नहीं। क्या कोई इसे संभालने के लिए बेहतर दृष्टिकोण है? मैं उपयोगकर्ता को सेवा के हर अनुरोध पर नहीं देना चाहता हूं।
यदि आप जानना चाहते हैं कि आपका एएसपी.NET एप्लिकेशन किस रनटाइम के नीचे चल रहा है तो ब्रेक पॉइंट दबाएं और स्थिर संपत्ति का उपयोग करें: 'HttpRuntime.TargetFramework'। –