C# 并非所有代码路径都返回值WCF Service
嗨,我正忙着将我的SQL数据库连接到我的WCF RESTful服务这是我的问题:C# 并非所有代码路径都返回值WCF Service,c#,web-services,wcf,rest,C#,Web Services,Wcf,Rest,嗨,我正忙着将我的SQL数据库连接到我的WCF RESTful服务这是我的问题:WebService.Service1.GetAllTrucks(string)':并非所有代码路径都返回值。每个报价都有一个或多个与外键连接的卡车,我希望能够查看每个报价中的每个卡车(我正在尝试使用以下方法:“GetAllTrucks”) IService1.cs [OperationContract] [WebInvoke(Method = "GET", ResponseForma
WebService.Service1.GetAllTrucks(string)':并非所有代码路径都返回值
。每个报价都有一个或多个与外键连接的卡车,我希望能够查看每个报价中的每个卡车(我正在尝试使用以下方法:“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);
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;
}
}
[DataContract]
public class wsTrucks
{
[DataMember]
public int TruckID { get; set; }
[DataMember]
public string TrucksName { get; set; }
}
卡车
[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);
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;
}
}
[DataContract]
public class wsTrucks
{
[DataMember]
public int TruckID { get; set; }
[DataMember]
public string TrucksName { get; set; }
}
谢谢。您应该能够通过将GetAllTrucks(string truckID)方法中的
返回结果
移动到循环之后来解决此问题:
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.
}
公共列表GetAllTrucks(字符串truckID)
{
NorthwindDataContext dc=新的NorthwindDataContext();
列表结果=新列表();
System.Globalization.CultureInfo ci=System.Globalization.CultureInfo.GetCultureInfo(“en-US”);
foreach(dc.tblQuotes.Where中的tblQuote(s=>s.ID.ToString()==truckID))
{
结果。添加(新wsQuote()
{
QuoteID=quote.ID,
QuoteNumber=quote.QuoteNumber
});
//返回结果;foreach
可能不会发生,并且在这种情况下没有return
。将返回结果放入;移出foreachmovereturn结果;
移出foreach循环谢谢!太愚蠢了!@推断我不知道你有我的投票权谢谢@MattheWatson@RenaldoGeldenhuis干杯。有时候我们会变得不知所措ng.:)还有我的支持票……因为这就是我要说的