替换WCF 4默认JSON序列来JSON.NET [英] Replace default JSON serializer in WCF 4 to JSON.NET
问题描述
我想JSON.NET替换默认的WCF JSON(所有数据类型)序列化。
我已经找遍了网上,找不到工作的解决方案
I want to replace the default WCF JSON (for all data types) serialization with JSON.NET. I've searched all over the net and couldn't find a working solution.
这是我的目标:
[JsonObject]
public class TestObject
{
[JsonProperty("JsonNetName")]
public string Name = "John";
[JsonProperty]
public DateTime Date = DateTime.Now;
}
这是我的WCF功能:
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<TestObject> Get();
这是在Global.asax中的代码:
This is the code in Global.asax:
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)));
}
这是Web.Config中:
This is Web.Config:
<endpointBehaviors>
<behavior name="Behavior_Brands">
<webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Bare" />
</behavior>
</endpointBehaviors>
和服务部分:
<service name="TestApp.CoreWCF.Brands">
<endpoint address="" behaviorConfiguration="Behavior_Brands" binding="webHttpBinding" contract="TestApp.CoreWCF.IBrands">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
最后,这就是我得到启动URL时:
And finally, this is what I'm getting when launching the URL:
HTTP://本地主机:30000 /品牌/获取
"http://localhost:30000/Brands/Get"
[{"Date":"\/Date(1354364412708+0200)\/","Name":"John"}, {"Date":"\/Date(1354364412708+0200)\/","Name":"John"}]
的
JSON答案显然忽略了JSON.NET属性。
The JSON answer obviously ignores the JSON.NET attributes.
我不熟悉与WCF和有很多的麻烦找好工作的答案。
I am not that familiar with WCF and having a lot of trouble finding a good working answer.
谢谢!
推荐答案
无论如何,我想出了一个方法来使用不同的串行器,手动,因为它没有通过微软似乎它更加高效,快速串行,虽然代码明智它是一个有点混乱。
Anyway, I figured out a way to use a different serializer, manually, seems its more efficient and faster because it doesn't pass through Microsoft's serializer, although code wise it's a bit messier.
-
将所有返回类型为System.ServiceModel.Channels.Message中你的接口和实现这些类。
Set all return types as "System.ServiceModel.Channels.Message" in your Interfaces and classes implementing them.
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
System.ServiceModel.Channels.Message GetData();
创建一个扩展方法,所以你可以很容易地建立一个内存流出来的对象,使用在JSON.NET串行器(或任何你想使用)。
Create an extension method so you could easily build a memory stream out of an object, using the JSON.NET serializer (or whichever you want to use).
public static System.ServiceModel.Channels.Message GetJsonStream(this object obj)
{
//Serialize JSON.NET
string jsonSerialized = JsonConvert.SerializeObject(obj);
//Create memory stream
MemoryStream memoryStream = new MemoryStream(new UTF8Encoding().GetBytes(jsonSerialized));
//Set position to 0
memoryStream.Position = 0;
//return Message
return WebOperationContext.Current.CreateStreamResponse(memoryStream, "application/json");
}
在方法体,直接返回序列化到流对象
In the method's body, return the object serialized directly to the stream
return yourObject.GetJsonStream();
这篇关于替换WCF 4默认JSON序列来JSON.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!