【问题标题】:Does WCF (dotnet 4) require webHttpBinding to be able to easily return JSON?WCF (dotnet 4) 是否需要 webHttpBinding 才能轻松返回 JSON?
【发布时间】:2011-07-12 10:40:39
【问题描述】:

我的服务将被同一个盒子上的另一个应用程序(Adobe AIR)使用,我无法运行服务器。我目前正在盒子上运行 WCF 服务作为 Windows 服务来实现这一点。没有服务器意味着没有 REST(如果我有任何错误,请纠正我),但我仍然希望我的服务能够返回 JSON。

我一直在对此进行研究,发现很多人使用带有 webHttpBinding 的 REST 服务,然后在配置中设置 JSON 行为,但是由于上述原因,我相信我不能使用 REST。

因此,以此为背景,我的问题是:作为 basicHttpBinding 或 WSHttpBinding 运行的 WCF 服务能否返回 JSON 而无需我手动处理?

如果是这样,有人可以解释一下吗?

这是我的 app.config 服务的当前详细信息

<configuration>
<system.serviceModel>
<services>
  <service name="WcfProjectLibrary.ProjectService">
    <endpoint address="" binding="wsHttpBinding" contract="WcfProjectLibrary.IProjectService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfProjectLibrary/ProjectService/" />
      </baseAddresses>
    </host>
  </service>
</services>
<!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>-->
<behaviors>      
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, 
      set the value below to false and remove the metadata endpoint above before deployment -->
      <serviceMetadata httpGetEnabled="True"/>
      <!-- To receive exception details in faults for debugging purposes, 
      set the value below to true.  Set to false before deployment 
      to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="False" />          
    </behavior>
  </serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

谢谢

【问题讨论】:

    标签: asp.net wcf air wcf-binding


    【解决方案1】:

    JSON 只能通过 webHttpBinding 或使用与 webHttpBinding 相同的绑定元素的自定义绑定返回,并且您仍然可以在 Windows 服务中托管 REST(使用 webHttpBinding)服务。 WebHttpBindingWebHttpBehavior 一起负责正确处理非 SOAP 消息。

    我不明白你说的没有服务器是什么意思。暴露服务的进程在通信方面是“服务器”。您只需要完整的 .NET 框架 4(客户端配置文件还不够)和安装 http.sys(您将需要它与任何基于 HTTP 的绑定)。

    【讨论】:

    • 感谢您的回复。我的理解是 REST 端点只能通过 Web 服务器交付,而我的 WCF 服务作为 Windows 服务运行。您已经为我解决了这个问题,非常感谢!
    • 它在 Web 服务器上提供了更多功能,因为某些功能与 ASP.NET 相关,但核心功能仍然可以在任何 .NET 进程中通过自托管来交付。
    • 我无法访问 REST 端点,我已编辑我的问题以显示我当前的 app.config。我没有 svc 文件,因为 WCF 项目没有为我创建一个。我是否需要自己创建它才能拥有可联系的端点?
    • 您说您不想将其托管在 Web 服务器中,但您的服务正在测试 Web 服务器中运行,并且您显然正在为 Web 服务器托管开发它。 msdn.microsoft.com/en-us/library/ms733069.aspx
    • 不,我已将此服务安装为 Windows 服务。我可以浏览到指定为基地址(默认为 SOAP)的端点,我的测试客户端可以有效地使用该服务。我正在使用 installutil 来安装服务。我和你一样感到惊讶,这在 Windows 服务中工作。
    猜你喜欢
    • 1970-01-01
    • 2013-12-24
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    相关资源
    最近更新 更多