简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM