Pregunta

I created a webservice that returnes some categories from database. If i test with the client that WCF is offering everything is perfect. I started building a client. I added a service reference to my service http://localhost/Transaction/transaction.svc. I create a new instance of the client webservice

  TransactionClient tc = new TransactionClient("BasicHttpEndpoint");
 Category[] availableCategories = tc.GetAllCategories();

I get Object reference not set to an instance of an object on the second line of code. The endpoint name is correct.

Any idea why the error ?

PS : if you need more code please let me know what to post. Thanks in advance.

Edit :

  [OperationContract]
  List<Category> GetAllCategories();

  Implementation : 
  public List<Category> GetAllCategories()
   { return db.GetAllCategories()}

The service is working is i test with WCFClient, so the rest of my code must be corect.

This is the code that gets me the items from database. I try with the solution posted but the app did not stop.

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;
¿Fue útil?

Solución

FaultException is exception transfered from other side of WCF channel. Meaning, that exception did not happen on line you call tc.GetAllCategories();, but on server side, in processing of that method.

FaultException wraps exception which occurred on server side. From what we can see in what you pasted, it's NullReferenceException. To find exact place where it occurs, set breakpoint in GetAllCategories method and step through it until it fails. Since this is a WCF service, exception in handling method call does not crash the service, but wraps the exception and sends it back to client.

Another way to find where error occurs would be to debug service, open Debug -> Exceptions in Visual Studio and tick check box in Thrown column next to Common Language Runtime Exceptions. This tells VS debugger to stop execution when error occurs, even though exception will be caught by WCF.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top