ASP.NET中的WCF服务方法POST和GET

时间:2013-06-28 09:37:08

标签: asp.net vb.net wcf web-services

我有两个程序,第一个是asp.net,第二个是WCF服务。 以下代码在asp.net程序上。

Public Function GetDataSource(ByVal prmModule As Object, ByVal prmEndPointName As String)
    Dim strEndPoint As String = GetEndPoint(prmEndPointName)
    Dim wr As WebRequest = WebRequest.Create(strEndPoint & prmModule)
    Dim ws As WebResponse = wr.GetResponse
    Dim enc As Encoding = System.Text.Encoding.GetEncoding(1252)
    Dim rs As New StreamReader(ws.GetResponseStream)
    Dim response As String = rs.ReadToEnd
    rs.Close()
    Return response
End Function

Public Sub SendClientData(ByVal prmEndPoint As String, ByVal prmModule As String, ByVal prmSource As String)
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse = Nothing
    Dim address As Uri
    Dim data As StringBuilder
    Dim byteData() As Byte
    Dim postStream As Stream = Nothing

    ' Set the REST API URL
    address = New Uri(GetEndPoint(prmEndPoint) & prmModule)
    ' Create the web request
    request = DirectCast(WebRequest.Create(address), HttpWebRequest)

    ' Set type to POST  
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"

    ' Create the data we want to send (each data.Append is for specific paramater data) 
    data = New StringBuilder()
    data.Append(prmSource)
    ' Create a byte array of the data we want to send  
    byteData = UTF8Encoding.UTF8.GetBytes(data.ToString())

    ' Set the content length in the request headers  
    request.ContentLength = byteData.Length

    Try
        postStream = request.GetRequestStream()
        postStream.Write(byteData, 0, byteData.Length)
    Finally
        If Not postStream Is Nothing Then postStream.Close()
    End Try
End Sub

上面的代码是函数,GetDataSource用于GET方法,SendClientData用于POST方法。

<OperationContract()> _
<WebInvoke(Method:="POST", _
       ResponseFormat:=WebMessageFormat.Json, _
       BodyStyle:=WebMessageBodyStyle.Wrapped, _
       UriTemplate:="applicant/post")> _
Sub postApplicant(ByVal request As Stream)

<OperationContract()> _
<WebInvoke(Method:="GET", _
       ResponseFormat:=WebMessageFormat.Json, _
       BodyStyle:=WebMessageBodyStyle.Wrapped, _
       UriTemplate:="applicant/search/{firstname}/{lastname}/{dateofbirth}/{placeofbirth}")> _
Function getApplicantHaermes(ByVal firstname As String, ByVal lastname As String, ByVal dateofbirth As String, ByVal placeofbirth As String) As String

上面的代码在WCF服务上,它用于调用实现的函数。

这是主要问题:

        Dim arr As New ArrayList : arr.Add("Id")
        Dim dt As New DataTable
        Dim tempDT As New DataTable
        tempDT = (AddPhoneNumberToDataTable(obj))
        If Session("EditPhoneNumber") IsNot Nothing Then
            SendClientData(EMPLOYEEENDPOINT, "phonenumber/postupdate/" &                   obj.HaermesOid, GetJsonFromDataTable(tempDT))
        Else
            SendClientData(EMPLOYEEENDPOINT, "phonenumber/post", GetJsonFromDataTable(tempDT))

            dt = AssignToDataTable(GetDataSource("phonenumber/search/" & obj.Party.Oid.ToString & "/" & obj.PhoneType.ToString & _
                                             "/" & obj.Number.ToString, EMPLOYEEENDPOINT), arr)

            With obj
                .HaermesOid = dt(0)("Id")
                .Save()
            End With
        End If

当我从ASP.Net调用SendClientData来调用WCF服务发布一些数据时,下面的代码(Function GetDataSoucre)用于从WCF获取数据到ASP.Net,但是当我调用SendClientData时,实现的函数是运行,它甚至没有完成,然后GET功能已经运行,它使两个功能同时运行。 GET方法从POST方法获取数据,因此最终GET方法没有获取数据。

0 个答案:

没有答案