从自托管的网络API控制台应用程序返回JSONP [英] Return jsonp from self hosted WEB API Console app

查看:258
本文介绍了从自托管的网络API控制台应用程序返回JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用这个博客帖子描述的JSONP格式:<一个href=\"http://www.west-wind.com/weblog/posts/2012/Apr/02/Creating-a-JSONP-Formatter-for-ASPNET-Web-API\" rel=\"nofollow\">http://www.west-wind.com/weblog/posts/2012/Apr/02/Creating-a-JSONP-Formatter-for-ASPNET-Web-API

I've use the jsonp formatter described in this blog post: http://www.west-wind.com/weblog/posts/2012/Apr/02/Creating-a-JSONP-Formatter-for-ASPNET-Web-API

具有使用格式化使用自托管控制台应用程序试图人?

Has anybody tried using the formatter with a self hosted console application?

我试过在一个普通MVC 4项目格式化,并立即奏效。不过,我想在一个自托管控制台应用程序来使用它,我有很多的麻烦得到它的工作。

I've tried the formatter in a regular MVC 4 project, and it worked immediately. However, I would like to use it in a self hosted console application, and I've had a lot of trouble getting it to work.

我已经注册的格式,并验证它补充说:

I've registered the formatter, and verified that it is added:

var config = new HttpSelfHostConfiguration(serviceUrl);

config.Formatters.Insert(0, new JsonpMediaTypeFormatter());

我核实,当我提出用下面的code的请求格式化被调用:

I have verified that the formatter is being called when I make a request with the following code:

$("#TestButton").click(function () {         
    $.ajax({
        url: 'http://localhost:8082/Api/Test',
        type: 'GET',
        dataType: 'jsonp',
        success: function(data) {
            alert(data.TestProperty);
        }
    }); 
})

我在提琴手检查,我得到的回应是:

I've checked in Fiddler, and the response I get is:

HTTP/1.1 504 Fiddler - Receive Failure
Content-Type: text/html; charset=UTF-8
Connection: close
Timestamp: 09:30:51.813

[Fiddler] ReadResponse() failed: The server did not return a response for this request.

我会非常感激,如果任何人都可以阐明这是怎么回事一些轻!

I'd be really grateful if anybody can shed some light on what is going on!

谢谢,

弗朗西斯

推荐答案

我怀疑的处置这里的StreamWriter引起的问题。尝试适应 WriteToStreamAsync 方法:

I suspect that Disposing the StreamWriter causes issues here. Try adapting the WriteToStreamAsync method:

public override Task WriteToStreamAsync(
    Type type, 
    object value,
    Stream stream,
    HttpContent content,
    TransportContext transportContext
)
{
    if (string.IsNullOrEmpty(JsonpCallbackFunction))
    {
        return base.WriteToStreamAsync(type, value, stream, content, transportContext);
    }

    // write the JSONP pre-amble
    var preamble = Encoding.ASCII.GetBytes(JsonpCallbackFunction + "(");
    stream.Write(preamble, 0, preamble.Length);

    return base.WriteToStreamAsync(type, value, stream, content, transportContext).ContinueWith((innerTask, state) =>
    {
        if (innerTask.Status == TaskStatus.RanToCompletion)
        {
            // write the JSONP suffix
            var responseStream = (Stream)state;
            var suffix = Encoding.ASCII.GetBytes(")");
            responseStream.Write(suffix, 0, suffix.Length);
        }
    }, stream, TaskContinuationOptions.ExecuteSynchronously);
}

这篇关于从自托管的网络API控制台应用程序返回JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆