tags:

views:

270

answers:

2

I added a DBML file with the appropriate connection string and valid credentials. I logged of my VPN hosting the SQL server and I wanted to test my WCF service in terms of what errors would be raised if it could not find the DB.

public List<Users> GetName(strinng UserEmail)
{

  var dbResult = from u in Users
                 where u.email.Equals(UserEmail)
                 select {v.Firstname, v.LastName, v.Zip};

  //Build List<Users>
  return List<users>;
}

Say the above is one of my methods. Upon invoking the method with no access to my DB, I did not see any error being thrown.How do I check if the connection is valid and that the DB exists?

I assumed that the DBML.cs file would have ensured this check in the cstor

A: 

Even though this is not your question you can make your method a lot more simpler with something like this (untested):

  var dbResult = from u in db.Users
                 where u.email.Equals(UserEmail)
                 select new User() 
                 {
                     FirstName = u.Firstname, 
                     LastName = u.LastName, 
                     Zip = u.Zip
                 };

  return dbResult.ToList();

// update: changed typos copied from original question

blu
blu: many thanks for that. It makes it a lot efficient/readable
+1  A: 

When I ran my little test mine failed when I tried to do a ToList() (which is because of the delayed execution of LINQ). I got a SqlException when the connection could not be made...took a little while for the exception to return but it inevitably happened.

here is my little test LINQ code:

TestDataContext con = new TestDataContext();
        var users = from user in con.Users
                    select user;
        //failed on this line...
        IList<User> faUsers = users.ToList();

My test was that I just shut my instance of Sql Server down.

If you are talking about debugging from the client side of this webmethod call, the connection to the webmethod might be timing out before the connection between the webservice and the database server....and that might be your timeout exception.

CSharpAtl
CSharpAtl: I wonder if testing it with the WCFTestClient makes a difference, as WCFTestClient throws a timeout exception rather then a SQL Exception as what you are getting
So you are getting an exception but it is not a SqlException?
CSharpAtl
I believe the service should throw a SoapException and not a SqlException, even if it is a SqlException that is raised in the service.
blu
Well depends on how CustomExceptions are configured on the service.
CSharpAtl