【发布时间】:2011-02-15 16:12:00
【问题描述】:
我一直在尝试作为 .Net 4 的一部分引入的 StandardEndpoints,但遇到了最奇特的错误。
我的代码
[ServiceContract]
public interface IAuthenticator
{
[OperationContract]
[WebInvoke(UriTemplate = "AuthenticateUser", Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
AuthPacket AuthenticateUser(string Username, string Password, string DeviceId);
}
我的 web.config
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment 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="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
让我抓狂的例外!
415 Cannot process the message because the content type 'application/json' was not
the expected type 'text/xml; charset=utf-8'.
我可以通过回到 .Net 3.5 声明每个服务的标准来解决问题,但是,除非我弄错了,WCF 与 .Net 4 的主要升级之一是它能够处理这样的事情.我做错了吗?
【问题讨论】:
-
您是否尝试删除
BodyStyle = WebMessageBodyStyle.WrappedRequest属性?您如何托管 WFC 服务?你使用 .svc 吗?你在 .svc 文件中使用了哪个factory? -
这被包装在一个没有工厂定义的 .svc 文件中。
-
您应该尝试将 .svc 与工厂
"System.ServiceModel.Activation.WebServiceHostFactory"一起使用,或者在 web.config 中包含其他部分,在没有 .svc 文件的情况下执行相同的操作。有关详细信息,请参阅stackoverflow.com/questions/4680162/host-wcf-in-mvc2-site/…。此外,如果您在描述的错误消息之后包含客户端代码(jQuery.ajax 或您使用的代码),它可能会有所帮助。 -
你有解决问题的进展吗?