WCF休息服务,服务器没有提供有意义的回复

时间:2012-11-30 19:31:47

标签: vb.net wcf rest .net-3.5 wcf-rest

我正在使用WCF创建一个restful服务,我一直收到错误:

服务器未提供有意义的回复;这可能是由于合同不匹配,过早的会话关闭或内部服务器错误造成的。

这是一个时钟应用程序,它接收用户名和当前时间并将其存储在数据库中以便登录/注销。

我是REST新手,任何人都可以帮助我吗?

我的服务界面:

ServiceContract(Namespace:="WCFRESTService")> _
Public Interface IService1

<OperationContract()> _
<WebInvoke(UriTemplate:="/login", Method:="PUT")> _
Function InsertUserDetails(ByVal username As String, ByVal time As DateTime) As String
End Interface

服务代码:

<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Required)> _
<ServiceBehavior(Namespace:="WCFRESTService")> _
Public Class Service1
    Implements IService1

    Private con As New SqlConnection("Data Source=TE-LAPTOP-001\SQL2008R2;Initial Catalog=timeClock;Integrated Security=True")

    Public Function InsertUserDetails(ByVal username As String, ByVal time As DateTime) As String Implements IService1.InsertUserDetails

        Dim strMessage As String = String.Empty
        Dim errorMessage As String = String.Empty
        Dim numcount As Integer = 0

        numcount = getusercount(username)
        If (numcount = 0) Then

            Try


                con.Open()
                Dim cmd As New SqlCommand("spInsertLog", con)
                cmd.CommandType = CommandType.StoredProcedure

                cmd.Parameters.AddWithValue("@username", username)
                cmd.Parameters.AddWithValue("@timein", time)


                cmd.ExecuteNonQuery()


            Catch ex As Exception
                errorMessage = ex.ToString
            Finally
                con.Close()
            End Try

            strMessage = "You have Signed In at: " + time
        ElseIf (numcount = 1) Then

            strMessage = "Error: You need to SignOut before you can SignIn"
        End If


        Return errorMessage + strMessage
    End Function

    Public Function getusercount(ByVal username As String) As Integer
        Dim count As Int32 = 0
        Try
            con.Open()
            Dim cmd As New SqlCommand("spgetcount", con)
            cmd.CommandType = CommandType.StoredProcedure

            cmd.Parameters.AddWithValue("@username", username)
            count = Convert.ToInt32(cmd.ExecuteScalar())


        Catch ex As Exception
        Finally
            con.Close()
        End Try


        Return count
    End Function
End Class

我的客户代码

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim objervice As New ServiceReference1.Service1Client()
    Dim result As String = objervice.InsertUserDetails("User1", DateTime.Now)
    MsgBox(result)
End Sub

服务webconfig:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
  <service name="WcfRESTService1.Service1" behaviorConfiguration="WcfRESTService1.Service1Behavior">
    <!-- Service Endpoints -->
    <endpoint address="http://localhost:62131/Service1.svc" binding="webHttpBinding" contract="WcfRESTService1.IService1" behaviorConfiguration="web">
      <!--
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>


<behaviors>
  <serviceBehaviors>
    <behavior name="WcfRESTService1.Service1Behavior">
      <!-- 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>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp/>
    </behavior>
  </endpointBehaviors>
</behaviors>
</system.serviceModel>

客户端配置:

<system.serviceModel>
            <bindings>
              <customBinding>
                <binding name="WebHttpBinding_IService1">
                  <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                    messageVersion="Soap12" writeEncoding="utf-8">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                      maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                  </textMessageEncoding>
                  <httpTransport/>
                </binding>
              </customBinding>
            </bindings>
            <client>

              <endpoint address="http://localhost:62131/Service1.svc" binding="customBinding" bindingConfiguration="WebHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="WebHttpBinding_IService1" />
            </client>
              <behaviors>
                <endpointBehaviors>
                  <behavior name="test">
                    <webHttp />
                  </behavior>
                </endpointBehaviors>
              </behaviors>

</system.serviceModel>

1 个答案:

答案 0 :(得分:0)

这里不需要REST(对于这种类型的客户端)。 但是如果你想 - 尝试使用来自REST方法的WebGet响应流:

[OperationContract, WebGet(UriTemplate = "/SendMessage?login={login}&password={password}&phoneNum={phoneNum}&message={message}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]    
System.IO.Stream SendMessage(string login, string password, string phoneNum, string message, TimeSpan timeout);
//..

public Stream SendMessage(string login, string password, string phoneNum, string message, TimeSpan timeout)
{
//..
return new MemoryStream(Encoding.Default.GetBytes(jsonString)); 
}