2

我有两个程序,第一个是 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 服务以发布一些数据时,下面的代码(函数 GetDataSoucre)用于从 WCF 向 ASP.Net 获取数据,但是当我调用 SendClientData 时,实现的函数正在运行,它的甚至还没有完成然后 GET 函数已经运行,它使两个函数同时运行。GET 方法是从 POST 方法中获取数据,所以最终 GET 方法没有获取到数据。

4

0 回答 0