データベースからいくつかのカテゴリを返す Web サービスを作成しました。WCFが提供していることをクライアントでテストすると、すべてが完璧です。クライアントの構築を開始しました。サービスへのサービス参照を追加しましたhttp://localhost/Transaction/transaction.svc
。クライアント Web サービスの新しいインスタンスを作成します
TransactionClient tc = new TransactionClient("BasicHttpEndpoint");
Category[] availableCategories = tc.GetAllCategories();
Object reference not set to an instance of an object
コードの 2 行目に進みます。エンドポイント名は正しいです。
エラーの理由は何ですか?
PS : さらにコードが必要な場合は、何を投稿すればよいかお知らせください。前もって感謝します。
編集 :
[OperationContract]
List<Category> GetAllCategories();
Implementation :
public List<Category> GetAllCategories()
{ return db.GetAllCategories()}
サービスは機能しており、WCFClient でテストしているため、残りのコードは正しい必要があります。
これは、データベースからアイテムを取得するコードです。投稿された解決策を試してみましたが、アプリは停止しませんでした。
List<Category> response = new List<Category>();
connect();
SqlCommand cmd = new SqlCommand("select id_category, name from tbl_category", conn);
try
{
dr = cmd.ExecuteReader();
while (dr.Read())
{
Category new_category = new Category();
new_category.id_category = int.Parse(dr["id_category"].ToString());
new_category.name = dr["name"].ToString();
response.Add(new_category);
}
}
catch (SqlException ex)
{
Console.Out.Write(ex.ToString());
}
finally
{
dr.Close();
conn.Close();
}
return response;