我正在使用其他人的代码,我尽可能地了解它,因此我会尝试尽可能提供相关且充分的信息。如果这里没有足够的细节,请询问,我会尝试用它来更新我的问题。
因此,在程序启动时,会创建一个实例:
Public Class ServiceProvider
Private ReadOnly serviceClient As IServiceClient
Public Sub New(Optional adress As String = Nothing)
Me.New(New ServiceClient(adress))
End Sub
Public Sub New(serviceClient As IServiceClient)
Me.serviceClient = serviceClient
End Sub
然后发生了一些事情,我到了这里(调用/发送和接收数据的地方正在发生:
Public Function ExecuteSp(spConfig As String) As ExecuteSpResult
Dim result As ExecuteSpResult = serviceClient.PostData(Of ExecuteSpResult)("executesp", spConfig)
End Function
以及在其中创建实例的类中的PostData
函数:
Public Class ServiceClient
Public Function PostData(Of T)(address As String, data As Byte()) As T
SetHeaders("application/octet-stream") 'sets credentials and stuff to the webClient
Try
Dim serviceResult As Byte() = webClient.UploadData(address, data)
Dim result As T = Deserialize(Of T)(serviceResult)
Return result
Catch ex As Exception
Throw ex
End Try
End Function
而WebClient does not support concurrent I/O exception
发生在webClient.UploadData(...)
。
所以我在周围搜索并发现只有在使用客户端的单个实例时才会发生这种情况。所以我对上面提到的前两个函数做了这个:
Public Class ServiceProvider
Private ReadOnly serviceClient As IServiceClient
Private main_address As String = Nothing
Public Sub New(Optional adress As String = Nothing)
Me.New(New ServiceClient(adress))
If main_address Is Nothing Then
main_address = adress
End If
End Sub
Public Function ExecuteSp(spConfig As String) As ExecuteSpResult
Using client As ServiceClient = New ServiceClient(main_address)
Dim result As ExecuteSpResult = serviceClient.PostData(Of ExecuteSpResult)("executesp", spConfig)
Return result
End Using
End Function
假设每次都会使用webClient
的不同实例,我不对吗?好吧,我仍然得到例外,并且无所事事。
更新
这是webClient
中<{1}}初始化的方式:
ServiceClient