我做错了什么,打电话给休息api

时间:2016-11-21 11:44:55

标签: vb.net rest http-headers httpclient

我是.Net的新手(任何帮助将不胜感激),我正在尝试实现这个类来写一个休息API。它所做的就是传递一些数据并对服务进行postAsJsonAsync。 它是用C#编写的,并转换为VB.net,以添加到VB.net应用程序解决方案中。 它工作一次,然后在第二次调用时失败,当它初始化另一个Uri实例时。  抛出异常:System.Net.Http.dllequest中的“System.InvalidOperationException”。“} {”此实例已经启动了一个或多个请求。只能在发送第一个请求之前修改属性。“} 代码在这里:

Imports System.Collections.Generic
Imports System.Configuration
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Net.Http
Imports System.Net.Http.Headers

Namespace Logging
    Public Class LogMessage
        Public Property Application As String
        Public Property Area As String
        Public Property Activity As String
        Public Property Description As String
        Public Property User As String
    End Class

    Public Class ActionLogging

        Public Shared Client As New HttpClient()

        Public Shared Function AddToActionLog(Activity As String, Description As String, User As String)
            Dim message = New LogMessage() With { _
                .Activity = Activity, _
                .Description = Description, _
                .User = User, _
                .Application = ConfigurationManager.AppSettings("Application"), _
                .Area = ConfigurationManager.AppSettings("Area") _
            }

            If Run(message) IsNot Nothing
                Run(message).Wait()           
            End If   

        End Function

        Public Shared Function CreateMessage(message As LogMessage) As Uri
            Dim task = Client.PostAsJsonAsync("Action", message)
            task.Wait()
            Dim response As HttpResponseMessage = task.Result
            response.EnsureSuccessStatusCode()
            Return response.Headers.Location          
        End Function


        Public Shared Function Run(message As LogMessage) As Task
            Client.BaseAddress = new Uri(ConfigurationManager.AppSettings("RestAPIServer"))
            Client.DefaultRequestHeaders.Accept.Clear()
            Client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json")) 

            CreateMessage(message)  

        End Function

    End Class
End Namespace

1 个答案:

答案 0 :(得分:0)

Imports System.Configuration
Imports System.Threading.Tasks
Imports System.Net.Http
Imports System.Net.Http.Headers

Namespace Logging
    Public Class LogMessage
        Public Property Application As String
        Public Property Area As String
        Public Property Activity As String
        Public Property Description As String
        Public Property User As String
    End Class

    Public Class ActionLogging

        Private ReadOnly Shared Client As New HttpClient() With {.BaseAddress = new Uri(ConfigurationManager.AppSettings("RestAPIServer"))}

        public Sub New()
            Client.DefaultRequestHeaders.Accept.Clear()
            Client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))       
        End Sub

        Public Shared Function AddToActionLog(activity As String, description As String, user As String)
            Dim message = New LogMessage() With { _
                .Activity = Activity, _
                .Description = Description, _
                .User = User, _
                .Application = ConfigurationManager.AppSettings("Application"), _
                .Area = ConfigurationManager.AppSettings("Area") _
            }

            SendMessage(message)

        End Function

        Private Shared Sub SendMessage(message As LogMessage)              
            Dim task = Client.PostAsJsonAsync("Action", message)
            task.Wait()
            Dim response As HttpResponseMessage = task.Result
            response.EnsureSuccessStatusCode()            
        End Sub

    End Class
End Namespace