#vb.net #wcf #class #list #service
#vb.net #wcf #класс #Список #Обслуживание
Вопрос:
Я пытаюсь передать список объектов в свою службу WCF, но, похоже, я не могу передать список объектов из моего консольного тестового приложения. У меня ошибка, которая гласит:
Значение типа ‘System.Коллекции.Generic.List (из ConsoleTestingApp.ServiceReference1.Буквенные переменные)’не могут быть преобразованы в ‘одномерный массив ConsoleTestingApp.ServiceReference1.LetterVariables’.
В этой строке:
Console.WriteLine(client.GetLetterObj("1", "1", "0", lstVariables))
У кого-нибудь есть идеи, что мне нужно сделать?
Спасибо, Джейсон
‘******* Код ***********
Мое тестовое консольное приложение выглядит следующим образом:
Dim Variables As LetterVariables
Dim lstVariables As New List(Of LetterVariables)
Variables = New LetterVariables
Variables._Sort = 10
Variables._key = "Letter Number"
Variables._value = "10"
lstVariables.Add(Variables)
Variables = New LetterVariables
Variables._Sort = 20
Variables._key = "Amount"
Variables._value = "$200"
lstVariables.Add(Variables)
Console.WriteLine(client.GetLetterObj("1", "1", "0", lstVariables))
Console.WriteLine("Finished")
Вот контракт:
<OperationContract()> _
Function GetLetterVariablesObj(ByVal LetterSpecID As Int32) As List(Of LetterVariables)
Вот svc:
Public Function GetLetterObj(ByVal LetterID As String, ByVal StateID As String, ByVal CompID As String, ByVal lstVars As System.Collections.Generic.List(Of LetterVariables)) As String Implements ILetterWriter.GetLetterObj
Dim SQLcon As New SqlClient.SqlConnection
Dim SQLcmd As New SqlClient.SqlCommand
Dim Variables As LetterVariables
Dim tblVars As DataTable
'Load the datatable to be passed to SQL Server
tblVars = New DataTable
tblVars.TableName = "LetterVariables"
tblVars.Columns.Add("Order")
tblVars.Columns.Add("Key")
tblVars.Columns.Add("Value")
For Each Variables In lstVars
tblVars.Rows.Add(Variables.Sort, Variables.Key, Variables.Value)
Next
'Connect to the database
SQLcon.ConnectionString = "Data Source=MySRVR;Initial Catalog=Sears;User ID=me;Password=mypass;"
SQLcon.Open()
'Set the procedure name, type amp; connection
SQLcmd.CommandText = "sp_cmd"
SQLcmd.CommandType = CommandType.StoredProcedure
SQLcmd.Connection = SQLcon
'Pass the parameters
SQLcmd.Parameters.AddWithValue("@LetterID", LetterID)
SQLcmd.Parameters.AddWithValue("@StateID", StateID)
SQLcmd.Parameters.AddWithValue("@CompID", CompID)
SQLcmd.Parameters.AddWithValue("@Vars", tblVars)
'Initialize the function string, execute the stored procedure
GetLetterObj = ""
GetLetterObj = SQLcmd.ExecuteScalar
'Close it all down
SQLcon.Close()
SQLcon.Dispose()
SQLcmd.Dispose()
End Function
Ответ №1:
Это потому, что коллекции по умолчанию представлены как массивы. Вы должны быть в состоянии передать lstVariables.ToArray()
. Смотрите Этот пост для объяснения того, почему это так, как есть.