【问题标题】:WCF JSONP returns blank/empty document when NULL is returned当返回 NULL 时,WCF JSONP 返回空白/空文档
【发布时间】:2011-12-19 22:33:46
【问题描述】:

我正在使用 Microsoft 提供的 JSONP 示例。 http://msdn.microsoft.com/en-us/library/cc716898(v=vs.90).aspx

JSONPEncoderFactory JSONPBindingExtension JSONPBindingElement JSONP行为

一切正常,除了返回 null 时。 而不是我想要的,那就是:callback(null); 它返回一个空白文档,这会导致大多数 JSONP 框架出现错误,例如

jQuery.ajax({jsonp:"callback"}); 

我在自定义类中的每个方法调用以及完成所有工作的方法调用上都设置了断点:

public override ArraySegment<byte> WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset)

我找到解决方案的最接近的方法只是猜测。我想也许 WebServiceHost 类可能有一个我可以重写的方法来构建我期望的功能,但到目前为止我空手而归。

我还想提一下,由于以下问题,我没有使用 WebScriptServiceHostFactory 类:

Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'.

如果我使用这个工厂,返回 null 会导致我正在寻找的功能。但是,我不能使用 TemplateUri,而我正在使用它们进行 HMAC 加密。由于我不能在 JSONP 请求中使用标头,因此格式为

/Service/Action/publicKey-signature?params

我尝试通过 JSONP 类型请求找到 HMAC 标准,但找不到任何东西。这是我能想到的最干净的。使用参数作为签名。

更新:

我在尝试以下解决方案时缺少的东西是您必须确保在您的 web.config 上构建一个具有 crossDomainScriptAccessEnabled="true" 的 webHttpBinding。此外,您必须确保使用该行为。也不要使用 WebScriptServiceHostFactory。 您可以使用 WebServiceHostFactory,但您必须在 web.config 中使用它(或通过绑定为单个服务执行此操作):

<system.serviceModel>
    <standardEndpoints>
        <webHttpEndpoint>
            <standardEndpoint crossDomainScriptAccessEnabled="true" />
        </webHttpEndpoint>
    </standardEndpoints>
</system.serviceModel>

【问题讨论】:

  • 您使用的是 .NET Framework 3.5 还是 4.0?在 4.0 中,JSONP 支持内置在类中,因此您不需要像示例中所示的编码器。
  • 我使用的是 4.0.我解释了当您尝试将 JSONP 与内置类一起使用时会发生什么。您不能使用 TemplateUri。除非你有办法允许吗?

标签: .net wcf null jsonp


【解决方案1】:

4.0 框架具有原生 JSONP 支持,并且确实支持 URI 模板,如下例所示。您需要启用对 WebHttpBinding 类的支持(或在配置中,如果您愿意)。

public class StackOverflow_7974435
{
    [ServiceContract]
    public class Service
    {
        [WebGet(UriTemplate = "/Sum?x={x}&y={y}")]
        public int Add(int x, int y)
        {
            return x + y;
        }
        [WebGet(UriTemplate = "/Data?isNull={isNull}")]
        public string GetData(bool isNull)
        {
            return isNull ? null : "Hello world";
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        WebHttpBinding binding = new WebHttpBinding { CrossDomainScriptAccessEnabled = true };
        WebHttpBehavior behavior = new WebHttpBehavior { DefaultOutgoingResponseFormat = WebMessageFormat.Json };
        host.AddServiceEndpoint(typeof(Service), binding, "").Behaviors.Add(behavior);
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine("Not a JSONP call");
        Console.WriteLine(c.DownloadString(baseAddress + "/Sum?x=6&y=8"));

        Console.WriteLine("A JSONP call");
        Console.WriteLine(c.DownloadString(baseAddress + "/Sum?x=6&y=8&callback=MyFunction"));

        Console.WriteLine("A JSONP call returning string");
        Console.WriteLine(c.DownloadString(baseAddress + "/Data?isNull=false&callback=MyFunction"));

        Console.WriteLine("A JSONP call returning null");
        Console.WriteLine(c.DownloadString(baseAddress + "/Data?isNull=true&callback=MyFunction"));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

更新:还添加了另一个返回 null 的操作,以及对它的调用。在第一次调用(返回字符串)中,服务返回MyFunction("Hello world");,而在第二种情况下,它正确返回MyFunction(null);。正如我在 cmets 中提到的,如果您不使用 ASP.NET AJAX 框架,请不要使用WebScriptEnablingBehavior,因为它不适合用于其他框架。

【讨论】:

  • 你在使用工厂服务吗?
  • 我仍然遇到同样的错误:使用“UriTemplate”的端点不能与“System.ServiceModel.Description.WebScriptEnablingBehavior”一起使用。
  • 如果我关闭工厂,我只会收到 400 个错误请求
  • 即使示例来自:msdn.microsoft.com/en-us/library/ee834511.aspx 尝试使用 UriTemplate 时出错。
  • blogs.msdn.com/b/justinjsmith/archive/2008/02/15/… "然后,不要使用 enableWebScript 行为,而是使用 WebHttpBehavior。您将失去与 ASP.NET AJAX 客户端堆栈(和 JS 代理)的兼容性,但您有 URI你正在寻找。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-06
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多