小编Clo*_*Dev的帖子

使用MVC的Unity 2.0 Web.config设置

我正在尝试将Unity 2.0用于我当前的MVC项目,并且无法在web.config文件中配置参数注入.

这就是我所拥有的:

1)家庭控制器:

public class HomeController : Controller
{
    IRepository repository = null;

    public HomeController()
    {
       // Always calls this constructor. Why? 
       // Should be calling the constructor below that takes IRepository.
    }

    public HomeController(IRepository repository)
    {
        // Should be calling this constructor!!!
        this.repository = repository;
    }
    public ActionResult Index()
    {
        List<int> intList = this.repository.GetInts();

        ViewData["Message"] = "Welcome to ASP.NET MVC!";

        return View();
    }
Run Code Online (Sandbox Code Playgroud)

一个带两个构造函数的基本控制器.第一个不带参数,第二个带IRepository作为参数(应该由Unity注入)

2)SQL存储库

public class SQLRepository : IRepository
{
    private string connectionString = null;

    public …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc web-config unity-container

3
推荐指数
1
解决办法
5955
查看次数

.Wait()导致TaskCanceledException

我有一个发送电子邮件的功能,如下所示:

public async Task SendEmail(string from, string to, string subject, string body, bool isBodyHtml = false)
        {
            await Task.Run(() =>
            {
                using (SmtpClient smtp = new SmtpClient(host, port))
                {
                    smtp.Credentials = new NetworkCredential(userName, password);
                    smtp.EnableSsl = true;
                    smtp.SendCompleted += SmtpOnSendCompleted;
                    MailMessage message = new MailMessage(from, to, subject, body);
                    message.IsBodyHtml = isBodyHtml;
                    smtp.Send(message);
                }
            }).ContinueWith(task =>
            {
                LoggingService.Instance.BusinessLogger.Error(task.Exception.Flatten().InnerException);

            }, TaskContinuationOptions.OnlyOnFaulted);
        }
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,它不是一个"真正的异步",而是一个"deffered execution",所以我可以调用这个方法,它不会阻塞当前的调用线程.

现在,我有时需要一种方法来等待发送电子邮件,然后继续.所以我调用我的SendMail()方法,如下所示:

EmailService.Instance.SendEmail("from@blah.com", "to@blah.com", "Subject", "Body text").Wait();
Run Code Online (Sandbox Code Playgroud)

最后使用.Wait().

出于某种原因使用.Wait() - 试图强制执行同步,导致异常:

System.Threading.Tasks.TaskCanceledException:任务已取消

问题:

1)为什么我得到这个例外?

2)如何强制同步执行此方法?

谢谢

c# wait task-parallel-library async-await

3
推荐指数
1
解决办法
2891
查看次数