【问题标题】:WCF Service returns jsonWCF 服务返回 json
【发布时间】:2013-12-09 00:21:37
【问题描述】:

我希望我的 WCF 服务返回 json。

我在 ASP.NET 开发服务器上对其进行了测试,它在 WCF 测试客户端上运行(方法调用正常)。

但是从浏览器测试失败并出现错误 400 错误请求。

web.config:

<configuration>
   <system.web>
      <compilation debug="true" targetFramework="4.0" />
   </system.web>
   <system.serviceModel>
      <services>
         <service name="WebServiceExample" 
                  behaviorConfiguration="MetadataBehaviour">
            <endpoint 
                address="" 
                behaviorConfiguration="AjaxBehaviour" 
                binding="webHttpBinding" 
                contract="WebService.IWebServiceExample">
               <identity>
                  <dns value="localhost" />
               </identity>
             </endpoint>
          </service>
      </services>
      <behaviors>
          <serviceBehaviors>
             <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
             </behavior>
             <behavior name="MetadataBehaviour">
                 <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
             </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
             <behavior name="AjaxBehaviour">
                 <webHttp/>
             </behavior>
          </endpointBehaviors>
       </behaviors>
       <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
   </system.serviceModel>
   <system.webServer>
      <modules runAllManagedModulesForAllRequests="true"/>
   </system.webServer>
</configuration>

合同

[ServiceContract]
public interface IWebServiceExample
{        

    [OperationContract]
    [WebGet(UriTemplate = "/check/{key}",
       ResponseFormat = WebMessageFormat.Json)]
    DataElement Check(string key);
}


[DataContract]
public class DataElement
{

    [DataMember]
    public string Key { get; set; }

    [DataMember]
    public DateTime DateSince { get; set; }

    [DataMember]
    public DateTime DateTill { get; set; }       
}

SVC 标记:

<%@ ServiceHost Language="C#" Debug="true" 
    Service="WebService.WebServiceExample" 
    CodeBehind="WebServiceExample.svc.cs" %>

我尝试的 URI 是:

http://localhost:16679/WebServiceExample.svc/check/asd

【问题讨论】:

  • 您可以查看服务定义即将浏览器指向http://localhost:16679/WebServiceExample.svc)吗?
  • 银忍者,是的。我可以看到带有客户端示例的页面并链接到 wsdl。
  • marc_s,设置 webHttpBinding 是否不足以通过简单的 http requets 进行连接?
  • 不,当然 - 是的。我只是错过了查看您的配置 - 抱歉
  • 所以没人知道问题出在哪里?

标签: json wcf browser


【解决方案1】:

呃。我懂了。 如果我告诉我我是如何经历的,我认为它对其他人会很有用。

首先,WCF 服务应用程序和 WCF 服务库之间存在差异。我认为如果在服务应用程序中为端点设置完整地址可能会导致冲突,因为它是由服务器(IIS 或 ASP.NET 开发)设置的。

其次,我的错误在于服务名称属性中的missing namespace。我设置了 name="WebServiceExample" 而不是 name="WebService.WebServiceExample"。

我是 WCF 的新手,对我来说,name 属性显然不是服务的完整类名。并且错误文本没有指示它。

我是在读完这个话题后想到的 WCF REST Service returns HTTP 400 Bad Request

解决的方法是在 Global.asax 中定义路由(变态的方式,但它有效)。

第三,我认为我的服务允许我查看wsdl页面是错误的。 RESTful WCF Service 生成 wsdl 很奇怪,而且看起来像错误。但我知道这是正常行为。

如果在配置中设置这样的东西

那么您的 RESTful 服务将允许显示

本地主机上的 WSDL/Service.svc?wsdl

localohost/Service.svc/help 上的 REST 函数

最后,一些有用的链接: http://www.codeproject.com/Articles/167159/How-to-create-a-JSON-WCF-RESTful-Service-in-60-sec --- WCF 服务库。我需要以管理员身份启动 VS 才能运行它。

http://robbincremers.me/2012/01/05/wcf-rest-service-with-xml-json-response-format-according-to-content-type-header/ --- WCF 服务应用程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多