2012-01-12 12 views
2

JSON経由で通信するWCF Webサービスの作成に取り組んでいます。私はそれが動作しているポイントにサービスを持って、私はヘルプページを設定しようとしているので、サービスを使用する開発者は作業するためのいくつかのドキュメントを持つことができます。WCFヘルプページでJSONからXMLへの応答が変更される

私が実行している問題は、ヘルプページを起動して実行すると、サービスから送信されたすべての応答がJSONからXMLに変更されたことです。

私はこれを初めて知っていることは間違いありません。私のサービスをどのように構築したか、Web.configで逃した旗のように単純なものかもしれないという根本的な欠陥があるかもしれません...私は本当にこの時点で迷っています。

<standardEndpoint name="serviceEndpoint" helpEnabled="true" automaticFormatSelectionEnabled="true"> 

は空の文字列であるために:

は、私が見つけたもの、基本的には試行錯誤や壁に頭を叩いて、私はWeb.configファイルに次の行のname属性を変更した場合でした:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"> 

ヘルプページには、魔法の現れが、私のサービスは現在、JSONの代わりにXMLを吐き出しています。

私はおそらく、このような具体的なものについては、共有しすぎるよりも共有するほうがよいと思います。ここでは、セットアップの関連する部分について考えています。私はモノトーンコードをお詫びします。私はそれをどのように理解すれば、より読みやすいように編集できます。

サービスインタフェース:

[OperationContract] 
[Description("DESCRIPTIONATION HAPPENS")] 
[WebInvoke(Method = "GET", 
       RequestFormat = WebMessageFormat.Json, 
       ResponseFormat = WebMessageFormat.Json, 
       UriTemplate = "GetYears")] 
GetYearsReply GetYears(); 
... 

サービスの実装:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MPG : IMPG 
{ 
    public GetYearsReply GetYears() 
    { 
     GetYearsReply reply = new GetYearsReply(); 
     reply.YearList = generateYears(); 
     return reply; 
    } 
... 

のGlobal.asax:

<%@ Application Codebehind="Global.asax.cs" Inherits="MPG_Service.Global" Language="C#" %> 

Global.asax.cs:

namespace MPG_Service 
{ 
    public class Global : System.Web.HttpApplication 
    { 
     void Application_Start(object sender, EventArgs e) 
     { 
      RegisterRoutes(); 

     } 

     private void RegisterRoutes() 
     { 
      RouteTable.Routes.Add(new ServiceRoute("garage", new WebServiceHostFactory(), typeof(MPG))); 
     } 
    } 
} 

のWeb.config:

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
     <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 

    <system.webServer> 
     <modules runAllManagedModulesForAllRequests="true"> 
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> 
     </modules> 
    </system.webServer> 

    <system.serviceModel> 

     <behaviors> 
      <serviceBehaviors> 
       <behavior> 
        <serviceMetadata httpGetEnabled="true"/> 
        <serviceDebug includeExceptionDetailInFaults="false"/> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 

     <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" /> 

     <standardEndpoints> 
      <webHttpEndpoint> 
       <!-- 
        Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
        via the attributes on the <standardEndpoint> element below 
       --> 
       <standardEndpoint name="serviceEndpoint" helpEnabled="true" automaticFormatSelectionEnabled="true"> 
        <!--<security mode="Transport"> 
         <transport clientCredentialType="None"/> 
        </security>--> 
       </standardEndpoint> 
      </webHttpEndpoint> 
     </standardEndpoints> 

    </system.serviceModel> 

</configuration> 

誰もが私の中でこの動作が起こっている理由に任意の洞察力、または任意の他の主要なネジアップを持っている場合コード私はすべての入力が大好きです。

+2

リクエストにXMLメディアタイプを指定する 'Accept'ヘッダがあるかどうか確認できますか? 'autoFormatSelectionEnabled'をtrueにすると、操作中の' ResponseFormat'プロパティよりAcceptヘッダーが選択されます。 – carlosfigueira

+0

受け入れる:text/html、application/xhtml + xml、application/xml; q = 0.9、*/*; q = 0.8 – zeonic

+0

これは確かに...そうだと思われますが、なぜですか? o.O – zeonic

答えて

2

お客様のクライアントは、XML(application/xml)を受け入れると言っているので、WCFが返すものです。これは自動書式設定ルール(http://msdn.microsoft.com/en-us/library/ee476510.aspxの詳細を参照)と一貫しています。その動作を望まない場合は、構成内でautoFormatSelectionEnabledをfalseに設定します。

+0

autoFormatSelectionをfalseに設定すると、魅力的に機能します。名前が空のときに受け入れヘッダーが尊重されるように見える理由はわかりませんが、名前が指定されていないときはわかりませんが、この時点ではうまくいきます。 – zeonic

+0

"空の"標準エンドポイントは、 'WebServiceHostFactory'によって使用されるものです。なぜなら、あなたはその動作を見ているからです。 – carlosfigueira

関連する問題