C# 在Web服务中调用无限次的方法

C# 在Web服务中调用无限次的方法,c#,service,web,wsdl,svc,C#,Service,Web,Wsdl,Svc,对于我的Web服务课程,我试图创建一个非常简单的登录系统。我遇到了一个问题,即每当调用checkCredentials时,createAccounts()都会得到无限次。知道为什么吗 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.We

对于我的Web服务课程,我试图创建一个非常简单的登录系统。我遇到了一个问题,即每当调用checkCredentials时,createAccounts()都会得到无限次。知道为什么吗

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.IO;

public class Service : IService
{
    static String path = @"PATH REMOVED";
    List<Account> accounts = new List<Account>();
    StreamWriter sw = null;

    private void createAccounts()
    {
        String data = File.ReadAllText(path);
        string[] data2 = data.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
        string[] temp;
        for (int i = 0; i < data2.Length; i++)
        {
            temp = data2[i].Split(',');
            if(!usernameExists(temp[0]) && temp[0] != "")
            {
                accounts.Add(new Account(temp[0],temp[1]));
            }
        }
    }

    public bool CreateAccount(String username, String password)
    {
        createAccounts();
        sw = File.AppendText(path);
        if (!usernameExists(username))
        {
            sw.WriteLine(username + "," + password + "\n");
            sw.Close();
            sw = null;
            return true;
        }
        else
        {
            sw.Close();
            sw = null;
            return false;
        }
    }

    public bool usernameExists(String username)
    {
        createAccounts();
        if(accounts.Exists(a => a.username == username))
            return true;
        else
            return false;
    }

    public bool CheckCredentials(String username, String password)
    {
        createAccounts();
        if (usernameExists(username))
        {
            if(accounts.Find(a => a.username == username).username == username && accounts.Find(a => a.username == username).password == password)
                return true;
            else
                return false;
        }
        else
            return false;

    }

}

class Account
{
    public String username;
    public String password;

    public Account(String u, String p)
    {
        username = u;
        password = p;
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Runtime.Serialization;
使用System.ServiceModel;
使用System.ServiceModel.Web;
使用系统文本;
使用System.IO;
公共课服务:IService
{
静态字符串路径=@“路径已删除”;
列表帐户=新列表();
StreamWriter sw=null;
私人帐户()
{
字符串数据=File.ReadAllText(路径);
string[]data2=data.Split(新字符串[]{Environment.NewLine},StringSplitOptions.None);
字符串[]温度;
for(int i=0;ia.username==username))
返回true;
其他的
返回false;
}
公共bool CheckCredentials(字符串用户名、字符串密码)
{
createAccounts();
如果(用户名存在(用户名))
{
if(accounts.Find(a=>a.username==username)。username==username&&accounts.Find(a=>a.username==username)。password==password)
返回true;
其他的
返回false;
}
其他的
返回false;
}
}
类别帐户
{
公共字符串用户名;
公共字符串密码;
公共帐户(字符串u、字符串p)
{
用户名=u;
密码=p;
}
}

如果将数据保存在文件中,则不应无限次写入数据。当您频繁请求时,它将以多线程方式写入文件,线程将锁定该文件。 我建议您使用try…catch…编写文件以查找问题:

    public bool CreateAccount(String username, String password)
    {
        createAccounts();

        try
        {
            sw = File.AppendText(path);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (!usernameExists(username))
        {
            sw.WriteLine(username + "," + password + "\n");
            sw.Close();
            sw = null;
            return true;
        }
        else
        {
            sw.Close();
            sw = null;
            return false;
        }
    }

createAccounts和usernameExists之间有一个循环。只要data2.Length不为零,您就会无休止地循环。

如果您实际向我们显示调用
CheckCredentials
的代码,这会有所帮助,因为这就是你要说的。我只是使用一个控制台程序来测试这个操作,其他什么都没有。你到底是怎么计算出这个函数被调用的,精确地
1 x无穷大
,而不是简单地调用很多次,比如说pantaloonillion?我想你找到了!谢谢大家!@bam2403-很高兴能为您提供帮助!如果您认为这解决了您的问题,请将此标记为答案。