user2413453
user2413453

Reputation:

Not all code paths return a value WCF Service

Hi I am busy connecting my SQL Database to my WCF RESTful Service. This is my issue: WebService.Service1.GetAllTrucks(string)': not all code paths return a value. Each quote has a truck or multiple trucks connected with foreign keys and I want to be able to view each truck in every quote (That I am trying to do in the Method: 'GetAllTrucks').

IService1.cs

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getTrucks")]
        List<wsTrucks> GetTrucks();

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "getAllTrucks/{truckID}")]
        List<wsQuote> GetAllTrucks(string truckID);

Service1.svc.cs

  public List<wsTrucks> GetTrucks()
        {
            NorthwindDataContext dc = new NorthwindDataContext();
            List<wsTrucks> results = new List<wsTrucks>();

            foreach (tblTruck truck in dc.tblTrucks)
            {
                results.Add(new wsTrucks()
                    {
                        TruckID = truck.ID,
                        TrucksName = truck.TRUCKNAME
                    });
            }
            return results;
        }

        public List<wsQuote> GetAllTrucks(string truckID)
        {
            NorthwindDataContext dc = new NorthwindDataContext();
            List<wsQuote> results = new List<wsQuote>();
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("en-US");

            foreach (tblQuote quote in dc.tblQuotes.Where(s => s.ID.ToString() == truckID))
            {
                results.Add(new wsQuote()
                    {
                        QuoteID = quote.ID,
                        QuoteNumber = quote.QUOTENUMBER
                    });
                return results;
            }
        }

Quote

[DataContract]
    public class wsQuote
    {
        [DataMember]
        public int QuoteID { get; set; }

        [DataMember]
        public string QuoteNumber { get; set; }
    }

Trucks

[DataContract]
    public class wsTrucks
    {
        [DataMember]
        public int TruckID { get; set; }

        [DataMember]
        public string TrucksName { get; set; }
    }

Thanks.

Upvotes: 1

Views: 435

Answers (1)

Matthew Watson
Matthew Watson

Reputation: 109772

You should be able to fix this by moving the return results in the GetAllTrucks(string truckID) method to after the loop:

    public List<wsQuote> GetAllTrucks(string truckID)
    {
        NorthwindDataContext dc = new NorthwindDataContext();
        List<wsQuote> results = new List<wsQuote>();
        System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.GetCultureInfo("en-US");

        foreach (tblQuote quote in dc.tblQuotes.Where(s => s.ID.ToString() == truckID))
        {
            results.Add(new wsQuote()
                {
                    QuoteID = quote.ID,
                    QuoteNumber = quote.QUOTENUMBER
                });
            // return results; <----- HERE! Move this to after the loop
        }

        return results; // Move it here.
    }

The issue is that if the list is empty, the body of the foreach loop will be skipped, and there is no return statement to be executed after the loop.

Upvotes: 1

Related Questions