JSON.NET ile varsayılan WCF JSON (tüm veri türleri için) serileştirmesini değiştirmek istiyorum. Tüm ağda arama yaptım ve bir çalışma çözümü bulamadım. Bu Web.ConfigVarsayılan JSON serializer'ı WCF 4'te JSON.NET'e değiştirin
protected void Application_Start(object sender, EventArgs e)
{
// Create Json.Net formatter serializing DateTime using the ISO 8601 format
var serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new IsoDateTimeConverter());
serializerSettings.Converters.Add(new BinaryConverter());
serializerSettings.Converters.Add(new JavaScriptDateTimeConverter());
serializerSettings.Converters.Add(new BinaryConverter());
serializerSettings.Converters.Add(new StringEnumConverter());
var config = HttpHostConfiguration.Create().Configuration;
Microsoft.ApplicationServer.Http.JsonMediaTypeFormatter jsonFormatter = config.OperationHandlerFactory.Formatters.JsonFormatter;
config.OperationHandlerFactory.Formatters.Remove(jsonFormatter);
config.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter(serializerSettings));
var httpServiceFactory = new HttpServiceHostFactory
{
OperationHandlerFactory = config.OperationHandlerFactory,
MessageHandlerFactory = config.MessageHandlerFactory
};
//Routing
RouteTable.Routes.Add(
new ServiceRoute(
"Brands", httpServiceFactory,
typeof(Brands)));
}
geçerli: Bu Global.asax kodudur
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<TestObject> Get();
: Bu benim WCF fonksiyonudur
[JsonObject]
public class TestObject
{
[JsonProperty("JsonNetName")]
public string Name = "John";
[JsonProperty]
public DateTime Date = DateTime.Now;
}
:
Bu
benim nesnedir :<endpointBehaviors>
<behavior name="Behavior_Brands">
<webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare" />
</behavior>
</endpointBehaviors>
ve hizmetler bölümü:
"http://localhost:30000/Brands/Get"
[{"Date":"\/Date(1354364412708+0200)\/","Name":"John"}, {"Date":"\/Date(1354364412708+0200)\/","Name":"John"}]
JSON cevabı besbelli yok sayar:
<service name="TestApp.CoreWCF.Brands">
<endpoint address="" behaviorConfiguration="Behavior_Brands" binding="webHttpBinding" contract="TestApp.CoreWCF.IBrands">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
Ve nihayet, bu URL'yi başlatırken ben alıyorum budur JSON.NET özellikleri.
doğrudan tefrika nesneyi döndürmek? Ne yazık ki benzer alternatif teknikleri kullanarak benim test durumumda (500KB nesne hiyerarşisi), JSON.NET serileştirme _plus_ byte dizisinin UTF8'e kopyalanmasının bir çift adımı, bir şekilde sadece basit DataContractJsonSerializer'dan daha pahalı olmasını sağlar. :-( – icelava