Gemäß der JSON spec ist die korrekte Art, einen Nullwert darzustellen, das Literal null
.Warum gibt WCF/JSON für einen Null-Rückgabewert nicht null zurück?
Wenn das der Fall ist, warum gibt WCF eine leere Antwort statt null
zurück? Ist das ein Fehler oder ist dieses Verhalten irgendwo dokumentiert?
komplettes Repro Beispiel:
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
[ServiceContract()]
public class Service1
{
[OperationContract(), WebGet(ResponseFormat = WebMessageFormat.Json)]
public string GetSomeString() { return "SomeString"; }
[OperationContract(), WebGet(ResponseFormat = WebMessageFormat.Json)]
public string GetNull() { return null; }
}
public class Host
{
public static void Main()
{
// Very simple WCF server
var host = new WebServiceHost(typeof(Service1), new Uri("http://localhost:8000/"));
host.AddServiceEndpoint(typeof(Service1), new WebHttpBinding() {
HostNameComparisonMode = HostNameComparisonMode.Exact
}, "");
host.Open();
Console.WriteLine("Service is running, press enter to quit...");
Console.ReadLine();
host.Close();
}
}
Erwartetes Ergebnis:
$ curl http://localhost:8000/GetSomeString && echo
"SomeString"
$ curl http://localhost:8000/GetNull && echo
null
$
Tatsächliches Ergebnis:
$ curl http://localhost:8000/GetSomeString && echo
"SomeString"
$ curl http://localhost:8000/GetNull && echo
$
Weil ein Nullwert auf viele verschiedene Arten dargestellt werden kann. Siehe diesen Beitrag für mehr: http://StackOverflow.com/Questions/21120999/representting-null-in-json –
@TomRedfern: Die akzeptierte Antwort dieses Beitrags behauptet auch, dass "null" ist die Art und Weise Null sollte entsprechend dargestellt werden die JSON-Spezifikation Meine Frage ist * warum * WCF von der Spezifikation abweicht. – Heinzi
In Anbetracht der [letzten Antwort] (http://stackoverflow.com/a/39933043/11683), frage ich mich, ob Ihr Dienst tatsächlich mit "Content-Type: application/json" antwortet? – GSerg