1

私は何時間も無駄に探していました。

ASP.NET Web サイトで、JSON.net を WCF Web サービス (.svc) の既定のシリアライザーおよびデシリアライザーとして使用しようとしています。明確にするために、これは MVC サイトではありません。IIS 7 内でホストされている単純な古い ASP.NET 4.0 です。

私の当初の動機は、JSON.net の優れた日付処理を使用することでしたが、今はそれができるかどうかを確認したいだけです。

以下は、日付を返したり、日付を取得したりできる単純な Web サービスです。このサービスは、jQuery ajax を使用してページから呼び出されます。

私がやりたいことは、組み込みの DataContractJsonSerializer または実際に呼び出されているものの代わりに、常に JSON.net デ/シリアル化ルーチンを使用するように ASP.NET を構成することです。サービスメソッドに文字列を渡したり返したりして、シリアル化を手動で処理できることは承知していますが、可能であればそれを避けたいと思います。

.svc はシリアライゼーション ルーチンを内部化するため、これが不可能になる可能性があることはわかっています。それも納得の答えでしょう。

ありがとう!

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>
4

0 に答える 0