0

I am creating a restful service using WCF, I keep getting the error:

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.

It is a time clock application, which takes in the username and the current time and stores it in a database for logging in/out.

I am new to the REST world can anyone help me?

My service interface:

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

Service code:

<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

My client code

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

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

Client config:

<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>
Ravi Chimmalgi
  • 175
  • 1
  • 12

1 Answers1

0

You don't need REST here (for such kind of client). But if you want - try use stream for WebGet-responding from from REST method:

[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)); 
}       
SalientBrain
  • 2,431
  • 16
  • 18
  • it didnt work. But, what do you mean "for such kind of client"? I never mentioned what client it was, it should call the rest service on a button click. I have done the same using soap, now just trying to use rest. – Ravi Chimmalgi Nov 30 '12 at 20:59