首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >T4MVC AsyncController

T4MVC AsyncController
EN

Stack Overflow用户
提问于 2011-03-24 12:35:13
回答 1查看 2.3K关注 0票数 4

我最近使用Nuget安装了T4MVC。在此之前,我有一个运行良好的旧版本,但是我有一个新的需求,需要一个异步控制器。创建异步控制器之后,由于与t4有关,我的项目将不会编译。所以我用Nuget更新了。

我现在遇到的问题是,直到最近我刷新了t4mvc,我的控制器才正常工作。刷新代码之后编译,但是当我调用异步操作时,它会在很长一段时间后返回,并返回大量不正确的数据。此外,至关重要的是,如果我在控制器中设置断点,它就不会再击中它们了?!如果我将我的DocumentController.generated.cs恢复到上一个工作版本,一切都很好。

下面是我的旧DocumentController.generated.cs的工作和新的不起作用。

有人能帮我弄清楚这是怎么回事吗?我已经依赖T4MVC了,因为它非常好,但是我真的无法避免这些异步操作。

年长的

代码语言:javascript
复制
// <auto-generated />
// This file was generated by a T4 template.
// Don't change it directly as your change would get overwritten.  Instead, make changes
// to the .tt file (i.e. the T4 template) and save it to regenerate this file.

// Make sure the compiler doesn't complain about missing Xml comments
#pragma warning disable 1591
#region T4MVC

using System;
using System.Diagnostics;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Mvc.Html;
using System.Web.Routing;
using T4MVC;
namespace WebUI.Client.Controllers {
    public partial class DocumentController {
        [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
        public DocumentController() { }

        [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
        protected DocumentController(Dummy d) { }

        [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
        protected RedirectToRouteResult RedirectToAction(ActionResult result) {
            var callInfo = result.GetT4MVCResult();
            return RedirectToRoute(callInfo.RouteValueDictionary);
        }


        [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
        public DocumentController Actions { get { return MVC.Document; } }
        [GeneratedCode("T4MVC", "2.0")]
        public readonly string Area = "";
        [GeneratedCode("T4MVC", "2.0")]
        public readonly string Name = "Document";

        static readonly ActionNamesClass s_actions = new ActionNamesClass();
        [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
        public ActionNamesClass ActionNames { get { return s_actions; } }
        [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
        public class ActionNamesClass {
            public readonly string Index = "Index";
        }


        static readonly ViewNames s_views = new ViewNames();
        [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
        public ViewNames Views { get { return s_views; } }
        [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
        public class ViewNames {
            public readonly string Index = "~/Views/Document/Index.aspx";
        }
    }

    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public class T4MVC_DocumentController: WebUI.Client.Controllers.DocumentController         

    {
        public T4MVC_DocumentController() : base(Dummy.Instance) { }

        public override System.Web.Mvc.ActionResult Index() {
            var callInfo = new T4MVC_ActionResult(Area, Name, ActionNames.Index);
            return callInfo;
        }

    }
}

#endregion T4MVC
#pragma warning restore 1591

新的

代码语言:javascript
复制
// <auto-generated />
// This file was generated by a T4 template.
// Don't change it directly as your change would get overwritten.  Instead, make changes
// to the .tt file (i.e. the T4 template) and save it to regenerate this file.

// Make sure the compiler doesn't complain about missing Xml comments
#pragma warning disable 1591
#region T4MVC

using System;
using System.Diagnostics;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using System.Web.Hosting;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Mvc.Html;
using System.Web.Routing;
using T4MVC;
namespace WebUI.Client.Controllers {
    public partial class DocumentController {
    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public DocumentController() { }

    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    protected DocumentController(Dummy d) { }

    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    protected RedirectToRouteResult RedirectToAction(ActionResult result) {
        var callInfo = result.GetT4MVCResult();
        return RedirectToRoute(callInfo.RouteValueDictionary);
    }

    [NonAction]
    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public System.Web.Mvc.JsonResult GetDocumentListCompleted() {
        return new T4MVC_JsonResult(Area, Name, ActionNames.GetDocumentListCompleted);
    }
    [NonAction]
    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public System.Web.Mvc.ActionResult GetDocumentThumbnailCompleted() {
        return new T4MVC_ActionResult(Area, Name, ActionNames.GetDocumentThumbnailCompleted);
    }

    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public DocumentController Actions { get { return MVC.Document; } }
    [GeneratedCode("T4MVC", "2.0")]
    public readonly string Area = "";
    [GeneratedCode("T4MVC", "2.0")]
    public readonly string Name = "Document";

    static readonly ActionNamesClass s_actions = new ActionNamesClass();
    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public ActionNamesClass ActionNames { get { return s_actions; } }
    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public class ActionNamesClass {
        public readonly string Index = "Index";
        public readonly string GetDocumentListCompleted = "GetDocumentListCompleted";
        public readonly string GetDocumentThumbnailCompleted = "GetDocumentThumbnailCompleted";
    }


    static readonly ViewNames s_views = new ViewNames();
    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public ViewNames Views { get { return s_views; } }
    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public class ViewNames {
        public readonly string Index = "~/Views/Document/Index.aspx";
    }
}

[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
public class T4MVC_DocumentController: WebUI.Client.Controllers.DocumentController {
    public T4MVC_DocumentController() : base(Dummy.Instance) { }

    public override System.Web.Mvc.ActionResult Index() {
        var callInfo = new T4MVC_ActionResult(Area, Name, ActionNames.Index);
        return callInfo;
    }

    public override System.Web.Mvc.JsonResult GetDocumentListCompleted(Web.Gateway.DocumentMetaDataCollection clientDocuments) {
        var callInfo = new T4MVC_JsonResult(Area, Name, ActionNames.GetDocumentListCompleted);
        callInfo.RouteValueDictionary.Add("clientDocuments", clientDocuments);
        return callInfo;
    }

    public override System.Web.Mvc.ActionResult GetDocumentThumbnailCompleted(System.IO.Stream document, string type) {
        var callInfo = new T4MVC_ActionResult(Area, Name,    ActionNames.GetDocumentThumbnailCompleted);
            callInfo.RouteValueDictionary.Add("document", document);
            callInfo.RouteValueDictionary.Add("type", type);
            return callInfo;
        }

    }
}

#endregion T4MVC
#pragma warning restore 1591

按照要求,这里是我的DocumentController类的存根;

代码语言:javascript
复制
namespace WebUI.Client.Controllers
{
    [Activated]
    [ConcurrentSessionDisabled]
    public partial class DocumentController : AsyncController
    {
    public readonly HashSet<string> ImageTypes = new HashSet<string>(new[] { "jpeg", "jpg", "png", "tif", "tiff", "bmp" });
    public readonly HashSet<string> IgnoredImageTypes = new HashSet<string>(new[] { "tif", "tiff" });

    public virtual ActionResult Index()
    {
        return RedirectToAction("List");
    }

    public virtual ActionResult List()
    {
        return View();
    }

    public virtual ActionResult Thumbnails(int id)
    {
        ViewData["documentId"] = id;
        return View();
    }

    public void GetDocumentListAsync()
    {
        AsyncManager.Timeout = 30000;
        AsyncManager.OutstandingOperations.Increment();

        Task.Factory.StartNew((state) => {
            Tuple<int, int> clientDetails = (Tuple<int, int>)state;

            DocumentMetaDataCollection clientDocuments = DocumentHelper.GetClientDocuments(clientDetails.Item1, clientDetails.Item2);
            AsyncManager.Parameters["clientDocuments"] = clientDocuments;

            AsyncManager.OutstandingOperations.Decrement();

        }, new Tuple<int, int>(Profile.User().ClientId, Profile.User().CrmAccountId));
    }

    public virtual JsonResult GetDocumentListCompleted(DocumentMetaDataCollection clientDocuments)
    {
        return Json(from document in clientDocuments.Documents
                    select new
                    {
                        Id = document.Id,
                        Values = document.Values,

                    }, JsonRequestBehavior.AllowGet);
    }

    public void GetDocumentFilenamesAsync(int id)
    {
        AsyncManager.Timeout = 5000;
        AsyncManager.OutstandingOperations.Increment();

        Task.Factory.StartNew((state) =>
        {
            int documentId = (int)state;

            List<string> urls = DocumentHelper.GetUrlsForDocument(documentId);

            AsyncManager.Parameters["documentId"] = documentId;
            AsyncManager.Parameters["urls"] = urls;

            AsyncManager.OutstandingOperations.Decrement();
        }, id);
    }

    public JsonResult GetDocumentFilenamesCompleted(int documentId, List<string> urls)
    {
        IDictionary<int, string> filenameToUrl = new Dictionary<int, string>();
        Regex illegalCharacters = new Regex("[^A-Za-z0-9_[-]:]");
        int idCounter = 0;

        foreach(string url in urls)
        {
            filenameToUrl.Add(++idCounter, url);
        }

        session_HoldDocumentUrls(documentId, filenameToUrl);

        return Json(from file in filenameToUrl select new {
            Name = filenameFrom(file.Value), 
            Id = file.Key
        });
    }

    private string filenameFrom(string url)
    {
        return url.Substring(url.LastIndexOf('/') + 1, url.Length - (url.LastIndexOf('/') + 1));
    }

    public void GetThumbnailAsync(int documentId, int imageId)
    {
        AsyncManager.Timeout = 5000;
        AsyncManager.OutstandingOperations.Increment();

        string url = session_GetDocumentUrls(documentId).First(f => f.Key == imageId).Value;

        Task.Factory.StartNew((state) => {
            string fileUrl = (string)state;
            thumbnailLoader(fileUrl);
        }, url);
    }

    private void thumbnailLoader(string fileUrl)
    {
        Stream document = DocumentHelper.GetDocument(fileUrl);
        string type = fileUrl.Substring(fileUrl.LastIndexOf('.') + 1, fileUrl.Length - (fileUrl.LastIndexOf('.') + 1)).ToLower();

        if (ImageTypes.Contains(type))
        {
            document = ImageHelper.ResizeImage(document, 210);
            type = "png";
        }

        AsyncManager.Parameters["document"] = document;
        AsyncManager.Parameters["type"] = type;

        AsyncManager.OutstandingOperations.Decrement();
    }

    public virtual ActionResult GetThumbnailCompleted(Stream document, string type)
    {
        switch (type)
        {
            case "png":
                return new FileStreamResult(document, "image/png");
            case "pdf":
                return File(Links.Content.Images.pdf_256_png, "image/png");
            default:
                return File(Links.Content.Images.document_256_png, "image/png");
        }
    }

    public void FileAsync(int documentId, int imageId)
    {
        if (session_GetDocumentUrls(documentId) == null)
        {
            return;
        }

        string url = session_GetDocumentUrls(documentId).First(f => f.Key == imageId).Value;

        AsyncManager.Timeout = 5000;
        AsyncManager.OutstandingOperations.Increment();

        Task.Factory.StartNew((state) =>
        {
            string fileUrl = (string)state;

            Stream document = DocumentHelper.GetDocument(fileUrl);

            string type = fileUrl.Substring(fileUrl.LastIndexOf('.') + 1, fileUrl.Length - (fileUrl.LastIndexOf('.') + 1)).ToLower();

            if (ImageTypes.Contains(type) && !IgnoredImageTypes.Contains(type))
            {
                document = ImageHelper.ConvertToPng(document);
                type = "png";
            }

            AsyncManager.Parameters["document"] = document;
            AsyncManager.Parameters["type"] = type;

            AsyncManager.OutstandingOperations.Decrement();
        }, url);
    }

    public virtual ActionResult FileCompleted(Stream document, string type)
    {
        if (document == null || type == null)
        {
            return RedirectToAction("List");
        }

        return new FileStreamResult(document, MimeHelper.Lookup("." + type));
    }
}

}

更新7/4/11

嗨,大卫,很抱歉我花了这么长时间才和你联系,我一直在研究其他的工作。

我已经成功地找到了这个问题,希望您能够了解它,并可能创建一个修复程序。

如果您查看上面的第二个代码转储,这就是问题所在。问题是你创造了;

代码语言:javascript
复制
[NonAction]
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
public System.Web.Mvc.JsonResult GetDocumentListCompleted() {
    return new T4MVC_JsonResult(Area, Name, ActionNames.GetDocumentListCompleted);
}

我不认为MVC抛出了一个错误,但是它是错误的;

2011-04-07 15:59:38,921 41致命MvcApplication (null) -一个未察觉的异常发生了System.Reflection.AmbiguousMatchException:在控制器类型'DocumentController‘上查找方法'GetDocumentListCompleted’失败了,原因是以下方法之间存在歧义: System.Web.Mvc.Async.AsyncActionMethodSelector.GetMethodByName(String methodName上的WebUI.Client.Controllers.DocumentController System.Web.Mvc.JsonResult GetDocumentListCompleted()类型的System.Web.Mvc.JsonResult System.Web.Mvc.JsonResult GetDocumentListCompleted()

因此,据我所知,MVC系统与我的代码和您创建的代码有一个模糊的匹配。

如果我注释掉您的上述代码,那么一切都将再次正常运行。

你现在了解大自然还是问题了吗?能修好吗?

干杯,

莱恩。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-03-24 18:06:14

更新(4/12/2011):好的,我刚刚发布了一个新构建的T4MVC (2.6.51),它忽略了异步完成方法。这应该能帮你解决这个问题!

T4MVC不支持异步操作。也就是说,您当然可以在控制器中包含异步操作,但是不能使用T4MVC引用它们。

请参阅前面关于该主题的问题:AsyncController in MVC2 and T4MVC: can they work together?

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5419173

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档