Web 服务方法只读取列表中的最后一个对象

Web Service method only reads the last object in a list

我必须制作一个 WCF 服务应用程序,它有一些存储在列表中的静态信息,以及查询该信息的方法。该服务托管在控制台应用程序中,稍后将通过 entity framework.

变成一个真实的数据库

我有以下 类 DoctorPatientUserAccount 和一个 IService1 接口,其中包含供那些人使用的方法类.

到目前为止我有 3 个问题

  1. 登录方法总是returns false。
  2. 方法DoctorByID,仅returns已创建的最后一个Doctor对象
  3. 方法 PatientByIDDoctorByID 方法相同。

cs文件在同一个项目中,项目构建和web服务都更新了。当我将列表放在手表上时,它们会按预期填满。我不知道哪里出了问题。

//The interface and the classes 
[ServiceContract]
public interface IService1 
{ 
    [OperationContract]
    string DoctorById(string ID);
    [OperationContract]
    bool Login(string name, string ID)
} 

[DataContract]
public class Doctor 
{ 
    [DataMember]
    public static List<Doctor> ListOfDoctors = new List<Doctor>();
    [DataMember]
    public string Name { get; set; } 
    [DataMember]
    public string LastName { get; set; } 
    [DataMember]
    public string ID { get; set; } 
    [DataMember]
    public DateTime DateOfBirth { get; set; }

    public Doctor(string name, string lastName, string id, DateTime dateOfBirth) 
    {
        Name = name;
        LastName = lastName;
        ID = id;
        DateOfBirth = dateOfBirth;
        //Adds the created doctors to the list
        ListOfDoctors.Add(this);
    }
}
//The DataContracts for the patients and UserAccounts look almost the same.

//Object instantiation and method implemenation

public class Service1: IService1 
{
    public static Doctor doctor1 = new Doctor("Simon", "Doe", "doc1", new DateTime(1996, 08, 08));
    public static Doctor doctor2 = new Doctor("John", "Doe", "doc2", new DateTime(1966, 12, 10));
    public static Doctor doctor3 = new Doctor("Mike", "Ray", "doc3", new DateTime(1990, 02, 18));

    public static UserAccount user1 = new UserAccount("Edd", "password", true);
    public static UserAccount user2 = new UserAccount("Tony", "password", true);
    public static UserAccount user3 = new UserAccount("John", "1234", false);

    //METHOD FROM ISSUE NO.1
    public bool Login(string userName, string pass) 
    {
        bool answer = "";
        foreach(UserAccount acc in UserAccount.ListOfUsers) 
        {
            if (acc.UserName == userName && acc.Password == pass) 
            {
                if (acc.AdminRights == true) 
                {
                    Console.WriteLine("You have Admin Rights");
                    answer = true;
                }
                else Console.WriteLine("You do not have Admin rights");
                answer = false;
            }
        }
        else Console.WriteLine("Account does not exist");
        answer = false;
    }
    return answer ;
}

//METHOD FROM ISSUE NO.2
public string PatientById(string ID) 
{
    string answer = "";
    foreach(Patient p in Patient.ListOfPatients) 
    {
        if (p.ID == ID) 
        {
            answer = "patient ID :" + p.ID + " Name: " + p.name + "  LastName: " + p.lastName;
        }
        else 
        {
            answer = "The patient with this ID does not exist";
        }
    }
    return answer;
}

这里不能post图片,我把截图贴在img上了。加载它们需要几秒钟

以下是我通过 Wcf 测试客户端获得的输出的链接:

  1. https://ibb.co/h1ghZ7y - 登录函数 returns false,尽管对象的布尔值为 true。
  2. https://ibb.co/BqJWqQC - DoctorByID 函数 returns "The doctor ID doesnt exist",但是带有 ID 的对象在列表中。
  3. https://ibb.co/SQkJRcy - DoctorByID 函数 returns 有关对象的数据,但仅当我将最后一个对象作为参数放入列表中时才会这样做。

好的,我正在尝试寻找一个开始。

//METHOD FROM ISSUE NO.1
public bool Login(string userName, string pass)
{
 bool answer="";
 foreach(UserAccount acc in UserAccount.ListOfUsers)
  {
   if(acc.UserName==userName && acc.Password == pass)
   { 
     if(acc.AdminRights==true)
     {
      Console.WriteLine("You have Admin Rights");
      answer=true;
     }
     else
     Console.WriteLine("You do not have Admin rights");
     answer= false;
     }
   }
  else
  Console.WriteLine("Account does not exist");
  answer= false;
 }
 answer odgovor;
}

这东西永远不会编译。为什么要用 "" 启动一个布尔变量?什么是 answer odgovor?为什么你的方法没有返回任何东西?

但是因为方法 returns false 的原因是因为你有两个这样的地方:

  if
  {
    ...
  }
  else
  Console.WriteLine("Account does not exist");
  answer= false;

... 转换为 ...

  if
  {
    ...
  }
  else
  {
    Console.WriteLine("Account does not exist");
  }
  answer= false;

...因为您在 else 之后遗漏了 {}。所以无论登录成功与否,代码总会到达answer = false

您可以通过使用调试器单步调试代码轻松找到它。


第二个……嗯……

//METHOD FROM ISSUE NO.2
public string PatientById(string ID)
{
    string answer= "";
    foreach(Patient p in Patient.ListOfPatients)
    {
        if (p.ID == ID)
        {
            answer = "patient ID :" + p.ID + " Name: " + p.name + "  LastName: " +                             p.lastName; 
        }
        else
        {
            answer = "The patient with this ID does not exist";
        }
    }
    return answer;
}

这里发生的事情是,您可能会通过给定的 ID 找到许多患者,但即使您找到了,您也总是会用新的字符串覆盖之前的字符串。这就是为什么只返回最后一场比赛的原因。