2012-01-12 47 views
2

我正在创建一个通过JSON进行通信的WCF Web服务。我得到的服务,它的工作,我正在设置帮助页面,以便使用该服务的开发人员可以有一些文件的工作。启用WCF帮助页面将响应从JSON更改为XML

我遇到的问题是,当我得到帮助页面并运行时,我的服务发出的所有响应都从JSON更改为XML。

我会第一个承认我对此很陌生。我的服务结构可能存在一些根本性的缺陷,或者可能像我在web.config中错过的标志一样简单......在这一点上,我真的很茫然。

我发现了什么,通过基本上只是试错和打我的头靠在墙上,就是如果我改变在Web.config以下行的name属性:

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

为空字符串:

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

帮助页面奇迹般地显示出来,但现在我的服务都吐出而不是XML JSON的。

我认为过度分享可能比低分享更好,因为这是我认为的设置的相关位。我对单音代码表示歉意,如果我弄明白怎么做,我可以将其编辑为更易读。

服务接口:

[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 2012-01-12 22:36:56

+0

接受:text/html,application/xhtml + xml,application/xml; q = 0.9,*/*; q = 0.8 – zeonic 2012-01-12 22:43:20

+0

所以它当然似乎...但是为什么? o.O – zeonic 2012-01-12 22:44:27

回答

2

你的客户说它接受XML(application/xml),所以这就是WCF返回的内容。这与自动格式化规则一致(详见http://msdn.microsoft.com/en-us/library/ee476510.aspx)。如果您不想要这种行为,则在您的配置中将autoFormatSelectionEnabled设置为false。

+0

将autoFormatSelection设置为false就像魅力一样。我不确定为什么当名称为空时接受标题似乎得到了尊重,但是当名称被指定时,却没有。但是我很高兴它在这一点上工作。 – zeonic 2012-01-12 23:11:48

+0

“空的”指定的标准端点是“WebServiceHostFactory”使用的端点,这就是您看到该行为的原因。 – carlosfigueira 2012-01-13 00:19:16