我正在创建一个通过JSON进行通信的WCF Web服务。我得到了服务,它正在工作,我正在尝试设置帮助页面,以便使用该服务的开发人员可以使用一些文档。
我遇到的问题是,当我确实启动并运行帮助页面时,我的服务发出的所有响应都从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>
如果有人对此行为发生的原因有任何了解,或者我的代码中有任何其他重大错误,我会喜欢任何输入。
答案 0 :(得分:2)
您的客户端说它接受XML(application/xml
),这就是WCF返回的内容。这与自动格式化规则一致(请参阅http://msdn.microsoft.com/en-us/library/ee476510.aspx处的详细信息)。如果您不想要这种行为,请在配置中将autoFormatSelectionEnabled
设置为false。