HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Web service getting value using LINQ from queue table in SQL database

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linqsqlgettingvaluedatabaseserviceusingwebfromqueue

Problem

As a test of my C# skills, I have been asked to create a simple web service which will take a message from a queue table in a SQL database and send it to a web application when a button is pressed on the application. I have never written a web service before so I am going in a little blind here so was after some advise on whether I had done this correct or not.

The stored procedure usp_dequeueTestProject gets a value from the top of the list in a table and then deletes the row in that table. At the moment I do not have this being archived anywhere, would it be better practice to instead of delete this, to just mark it as sent instead?

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public string GetDataLINQ()
    {
        try
        {
            TestProjectLinqSQLDataContext dc = new TestProjectLinqSQLDataContext();
            var command = dc.usp_dequeueTestProject();
            string value = command.Select(c => c.Command).SingleOrDefault();
            return value;
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
    }
}


I've opted for using LINQ for starters, I am not sure if this is the best way to do it or not? I am only passing through the one string as well... In reality I guess you would normally want to send more than one field, such as datetime sent, message type etc, but wasn't sure what data type to use for this? I have seen this done using a Strut, but wasn't sure if this was correct? Any guidance would be greatly appreciated.

Solution

LINQ is a good choice. You're querying something, so it lends itself to the task.

That being said, the exception handling is atrocious. Making your return values null, some command's value from the database or some big honkin' exception text is abusing the string data type. I'd remove the try..catch altogether and let your client app handle the exception if it knows what to do. In the best case, your catch could log the error somewhere then re-throw.

Context

StackExchange Code Review Q#51321, answer score: 5

Revisions (0)

No revisions yet.