0

我正在使用 WCF 创建一个安静的服务,但我不断收到错误消息:

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

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

我是 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

服务网络配置:

<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>
4

1 回答 1

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)); 
}       
于 2012-11-30T20:04:53.213 回答