我在这一行得到了StackOverflowException。我是WCF的新手,但据我所知,它不是在无限循环中调用的。好吧,我希望有人可以解决这个问题。
get { return LevStad; }
代码。
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value);
[OperationContract]
Kund GetDataUsingDataContract(Kund kund);
// TODO: Add your service operations here
}
[DataContract]
public class Kund
{
int iD;
string fornamn = "";
string efterNamn = "";
string telefon = "";
string boAdress = "";
string boPostnummer = "";
string boStad = "";
string boLand = "";
string levAdress = "";
string levPostnummer = "";
string levStad = "";
string levLand = "";
[DataMember]
public int ID
{
get { return iD; }
set { iD = value; }
}
[DataMember]
public string Fornamn
{
get { return fornamn; }
set { fornamn = value; }
}
[DataMember]
public string EfterNamn
{
get { return efterNamn; }
set { efterNamn = value; }
}
[DataMember]
public string Telefon
{
get { return telefon; }
set { telefon = value; }
}
[DataMember]
public string BoAdress
{
get { return boAdress; }
set { boAdress = value; }
}
[DataMember]
public string BoPostnummer
{
get { return boPostnummer; }
set { boPostnummer = value; }
}
[DataMember]
public string BoStad
{
get { return boStad; }
set { boStad = value; }
}
[DataMember]
public string BoLand
{
get { return boLand; }
set { BoLand = value; }
}
[DataMember]
public string LevAdress
{
get { return levAdress; }
set { levAdress = value; }
}
[DataMember]
public string LevPostnummer
{
get { return levPostnummer; }
set { levPostnummer = value; }
}
[DataMember]
public string LevStad
{
get { return LevStad; }
set { levStad = value; }
}
[DataMember]
public string LevLand
{
get { return levLand; }
set { levLand = value; }
}
}
public class Service : SQLBase, IService
{
public Service() : base()
{
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public Kund GetDataUsingDataContract(Kund kund)
{
kund = new Kund();
SqlCommand SQLCom = new SqlCommand(@"SELECT Kund.ID, Förnamn, Efternamn, Telefon, Adress, Postnummer, Stad, Land
FROM Webbshop.dbo.Kund INNER JOIN Webbshop.dbo.Adress ON Kund.BoAdress = Adress.ID");
SQLCom.Connection = DatabaseConnection;
SQLCom.Connection.Open();
SqlDataReader rdr = SQLCom.ExecuteReader();
while (rdr.Read())
{
kund.ID = (int) rdr[0];
kund.Fornamn = rdr[1].ToString().Trim();
kund.EfterNamn = rdr[2].ToString().Trim();
kund.Telefon = rdr[3].ToString().Trim();
kund.BoAdress = rdr[4].ToString().Trim();
kund.BoPostnummer = rdr[5].ToString().Trim();
kund.BoStad = rdr[6].ToString().Trim();
//kund.BoLand = rdr[7].ToString().Trim();
//kund.LevAdress = rdr[8].ToString();
//kund.LevPostnummer = rdr[9].ToString();
//kund.LevStad = rdr[10].ToString();
//kund.LevLand = rdr[11].ToString();
}
SQLCom.Connection.Close();
return kund;
}
}
public class SQLBase
{
protected static SqlConnection DatabaseConnection = new SqlConnection();
public SQLBase()
{
DatabaseConnection.ConnectionString =
"Data Source=(local);Initial Catalog=Webbshop;Integrated Security=True";
}
}
Kund kund = null;// = new Kund();
using (ServiceReference.ServiceClient wcfClient = new ServiceReference.ServiceClient())
{
kund = wcfClient.GetDataUsingDataContract(kund);
}
答案 0 :(得分:6)
这是大写。它应该返回变量:
get { return levStad; }
现在,它正在返回它自己,它调用get
,它返回自己...堆栈溢出。
BoLand
也会遇到问题。 set
将设置自己,将再次调用set
...,大写:
public string BoLand
{
get { return boLand; }
set { boLand = value; }
}
答案 1 :(得分:2)
public string LevStad
{
get { return LevStad; }
set { levStad = value; }
}
有你的问题。它在LevStad上递归。
即。你得到LevStad并且它试图获得LevStad属性,它试图获得属性LevStad。所以它一直持续到调用堆栈溢出为止。
只需将其设置为字段(C#区分大小写)。
public string LevStad
{
get { return levStad; }
set { levStad = value; }
}
在BoLand集上你会遇到类似的问题。
public string BoLand
{
get { return boLand; }
set { BoLand = value; }
}