使用JSON.net作为ASP.net服务(.svc)的默认序列化程序和反序列化程序
我找了几个小时都没找到 我正在尝试使用JSON.net作为ASP.net网站上WCF web服务(.svc)的默认序列化程序和反序列化程序。要明确的是,这不是一个MVC站点。只是普通的旧ASP.NET 4.0托管在IIS 7中 我最初的动机是使用JSON.net优越的日期处理,但现在我只想看看是否可以做到 下面是一个可以返回日期或获取日期的简单Web服务。使用jqueryajax从页面调用此服务 我想做的是以某种方式配置ASP.NET,使其始终使用JSON.NET反序列化例程,而不是内置的DataContractJsonSerializer或实际调用的任何东西。我知道我可以将字符串传递并返回给我的服务方法,手动处理序列化,但如果可能的话,我希望避免这种情况 据我所知,这可能是不可能的,因为.svc将序列化例程内部化。这也是一个可以接受的答案 谢谢使用JSON.net作为ASP.net服务(.svc)的默认序列化程序和反序列化程序,asp.net,json,web-services,json.net,svc,Asp.net,Json,Web Services,Json.net,Svc,我找了几个小时都没找到 我正在尝试使用JSON.net作为ASP.net网站上WCF web服务(.svc)的默认序列化程序和反序列化程序。要明确的是,这不是一个MVC站点。只是普通的旧ASP.NET 4.0托管在IIS 7中 我最初的动机是使用JSON.net优越的日期处理,但现在我只想看看是否可以做到 下面是一个可以返回日期或获取日期的简单Web服务。使用jqueryajax从页面调用此服务 我想做的是以某种方式配置ASP.NET,使其始终使用JSON.NET反序列化例程,而不是内置的Dat
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using System.Text;
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
DateTime GetDateTime();
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
void SendDate(DateTime dt);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using AutoMapper;
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IService
{
public DateTime GetDateTime()
{
DateTime dt = new DateTime(2012, 01, 02, 03, 04, 05);
return dt;
}
public void SendDate(DateTime dt)
{
DateTime d = dt;
//Do something with the date
}
}
Service.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
Web.config relevant lines:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用System.ServiceModel.Activation;
使用系统文本;
[服务合同]
公共接口设备
{
[经营合同]
[WebInvoke(Method=“POST”,BodyStyle=WebMessageBodyStyle.WrappedRequest,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
DateTime GetDateTime();
[经营合同]
[WebInvoke(Method=“POST”,BodyStyle=WebMessageBodyStyle.WrappedRequest,RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json)]
无效发送日期(日期时间dt);
}
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Activation;
使用系统文本;
使用自动制版机;
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
公共课服务:IService
{
公共日期时间GetDateTime()
{
DateTime dt=新的日期时间(2012,01,02,03,04,05);
返回dt;
}
公共无效发送日期(日期时间dt)
{
日期时间d=dt;
//对日期做点什么
}
}
Service.svc
Web.config相关行: