2011-11-02 119 views
0

我正在使用Microsoft提供的JSONP示例。 http://msdn.microsoft.com/en-us/library/cc716898(v=vs.90).aspx當返回NULL時,WCF JSONP返回空白/空文檔

JSONPEncoderFactory JSONPBindingExtension JSONPBindingElement JSONPBehavior

一切工作正常,返回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 

我試圖找到爲HMAC標準在JSONP類型請求,但找不到任何東西。這是我能想到的最乾淨的。 Params用作簽名。

UPDATE:

嘗試以下解決方案時,我失蹤的事情,你必須確保在你的web.config您構建了crossDomainScriptAccessEnabled =「真」一個的WebHttpBinding。您還必須確保不是使用該行爲。也不要使用WebScriptServiceHostFactory。 可以使用WebServiceHostFactory,但你必須在一個web.config有這個(或做它的單一服務,通過綁定):

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

您是否使用.NET Framework 3.5或4.0?使用4.0時,JSONP支持內置在類中,因此您不需要如示例中所示的編碼器。 – carlosfigueira

+0

我正在使用4.0。我解釋了當您嘗試使用JSONP和內置類時發生的情況。你不能使用TemplateUri。除非你有辦法允許它? – BradLaney

回答

1

的4.0框架具有本地JSONP支持,它支持URI模板,如下面的示例所示。您需要啓用WebHttpBinding類的支持(或者如果您願意,可以在config中啓用)。

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);。正如我在評論中提到的那樣,如果您沒有使用ASP.NET AJAX框架,請不要使用WebScriptEnablingBehavior,因爲它不打算用於其他框架。

+0

您是否在使用該服務的工廠? – BradLaney

+0

我仍然收到同樣的錯誤:使用'UriTemplate'的端點不能與'System.ServiceModel.Description.WebScriptEnablingBehavior'一起使用。 – BradLaney

+0

如果我離開工廠,我只能得到400個不好的請求 – BradLaney