类型中的方法'ExecuteAsync'没有实现

时间:2018-04-27 19:16:49

标签: c# asp.net-web-api2

我正在尝试创建自己的IHttpActionResult类型以从我的控件返回一些结果,但在执行我的控制器操作之前,会引发以下异常:

  

程序集'Kevsoft.TestResult'中的方法'ExecuteAsync'来自程序集'Kevsoft,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'没有实现。

堆栈跟踪:

  

System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1.Start[TStateMachine](TStateMachine& stateMachine) at Kevsoft.Controllers.TestController.Get(Int32 customerId, Int32 attachmentId, Guid fileId) at lambda_method(Closure , Object , Object[] ) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_3.<GetExecutor>b__2(Object instance, Object[] methodParameters) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary中的Kevsoft.Controllers.TestController.d__5.MoveNext()2个参数,CancellationToken cancellationToken)---从抛出异常的上一个位置开始的堆栈跟踪---在System.Web.Http.Controllers.ApiControllerActionInvoker.d__1.MoveNext()的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处---栈尾从抛出异常的先前位置追踪---在System.Web.Http.Controllers.ActionFilterResult的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处。 d__5.MoveNext()---抛出异常的前一个位置的堆栈跟踪结束---在System.Runtime.CompilerServices.T的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()

的askAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

自定义IHttpActionResult

public class TestResult : IHttpActionResult
{
    private readonly Stream _content;
    private readonly string _contentType;
    private readonly string _fileName;

    public AttachmentResult(Stream content, string contentType, string fileName)
    {
        _content = content;
        _contentType = contentType;
        _fileName = fileName;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        var byteArrayContent = new StreamContent(_content);
        byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse(_contentType);

        byteArrayContent.Headers.ContentDisposition = ContentDispositionHeaderValue.Parse($@"attachment; filename=""{_fileName}""");

        return Task.FromResult(new HttpResponseMessage()
        {
            Content = byteArrayContent,
        });
    }
}

似乎所有内容都引用了Microsoft.AspNet.WebApi.Core.5.2.4 nuget包。

0 个答案:

没有答案