【问题标题】:WCF rest service for list - json not working列表的 WCF 休息服务 - json 不起作用
【发布时间】:2014-08-16 18:43:06
【问题描述】:

我的 wcf 重置服务只有 2 个合同,一个一直在工作,另一个没有工作。

服务代码:

Public Class BasicServ
     Implements IBasicServ

        Public Function DoWork() Implements IBasicServ.DoWork
            Return "Working"
        End Function

        Function Authorize(ByVal id As String, ByVal pw As String) Implements IBasicServ.Authorize
            Dim c As New List(Of Guid)
            For i = 0 To 10
                c.Add(Guid.NewGuid)
            Next
            Return c
            'Return Guid.NewGuid
        End Function
    End Class

合同文件代码:

<ServiceContract()>
Public Interface IBasicServ

    <OperationContract()>
    <WebGet(UriTemplate:="test/", BodyStyle:=WebMessageBodyStyle.Wrapped, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)>
    Function DoWork()

    <OperationContract()>
    <WebGet(UriTemplate:="Authorize/{id}/{pw}", BodyStyle:=WebMessageBodyStyle.Wrapped, RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Json)>
    Function Authorize(ByVal id As String, ByVal pw As String)


End Interface

网页配置文件:

<?xml version="1.0"?>
<configuration>
  <system.diagnostics> 
   <sources>
      <source name="System.ServiceModel.MessageLogging">
        <listeners>
          <add name="messagelistener"
               type="System.Diagnostics.XmlWriterTraceListener" 
                      initializeData="d:\logs\myMessages.svclog"></add>
        </listeners>
      </source>
    </sources>
  <trace autoflush="true"/>
  </system.diagnostics> 

    <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>
     <diagnostics>
       <messageLogging logEntireMessage="true"
                        logMessagesAtServiceLevel="false"
                        logMessagesAtTransportLevel="false"
                        logMalformedMessages="true"
                        maxMessagesToLog="5000"
                        maxSizeOfMessageToLog="2000">         
       </messageLogging>
     </diagnostics>

    <services>
      <service behaviorConfiguration="ServBehav" name="AssistantWcf.BasicServ">
        <endpoint address="auth" behaviorConfiguration="EndBehav" binding="webHttpBinding" name="endpointname" contract="AssistantWcf.IBasicServ" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="EndBehav">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServBehav">
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpGetBinding="webHttpBinding" httpGetBindingConfiguration="" />
          <!-- 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>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

在调试/测试时,我发现,如果我在 Authoriza 方法中只返回 1 个 GUID,它就可以工作,但是当我发送 GUID 列表时,它就不能工作,并且没有创建 SVClog 文件。您能否帮助我更好地了解 WCF REST 服务。谢谢。

【问题讨论】:

  • 你在使用WebHttpBinding吗?你能发布你的配置吗?
  • 我的错,添加了webconfig文件代码。谢谢。
  • 我不知道你是否可以返回一个 GUID,所以为了测试目的,也许确保它作为一个字符串返回,但你的接口和函数不应该定义你想要一个列表?即 Function Authorize(ByVal id As String, ByVal pw As String) As List (Of String)。
  • 是的,这是有效的,但是当我将它作为仅 GUID 而不是列表发送时,虽然我没有指定返回类型,但它有效,请您作为答案发布并尽可能澄清。谢谢。

标签: json wcf rest


【解决方案1】:

从上面的cmets和几个实验来看,问题的解决方案是返回类型如果是string则不是必须的,但如果是其他的,则必须指定返回类型。如果总是提供返回类型会更好。

【讨论】:

    猜你喜欢
    • 2013-05-08
    • 2013-03-25
    • 2014-04-25
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2011-03-29
    相关资源
    最近更新 更多