Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';t从我的WCF应用程序发送异常_C#_Winforms_Wcf - Fatal编程技术网

C# Can';t从我的WCF应用程序发送异常

C# Can';t从我的WCF应用程序发送异常,c#,winforms,wcf,C#,Winforms,Wcf,下面是教程 我最终得到了以下代码: 接口: [ServiceContract] public interface IAuthenticator { [OperationContract] [FaultContract(typeof(AuthenticationException))] Account authenticateApplication(string userName, string Password); } 例外情况: [DataContract] pub

下面是教程

我最终得到了以下代码:

接口:

[ServiceContract]
public interface IAuthenticator
{
    [OperationContract]
    [FaultContract(typeof(AuthenticationException))]
    Account authenticateApplication(string userName, string Password);
}
例外情况:

[DataContract]
public class AuthenticationException
{
    private string validationError;

    [DataMember]
    public string ValidationError
    {
        set { validationError = value; }
        get { return validationError; }
    }

    public AuthenticationException(string valError)
    {
        validationError = valError;
    }
}
最后,我是如何在authenticateApplication的实现中抛出错误的:

catch (InvalidUsernameException)
{
    throw new FaultException<AuthenticationException>(new AuthenticationException("The username you entered could not be found in our database."), new FaultReason("Error"));
}
编辑:

以下是完整的try-catch块:

try
{
    using (myConnection)
    {
        using (myCommand)
        {
            //Tell it to execute the stored procedure on the database
            myCommand.CommandText = "findUsername";
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add("@userName", SqlDbType.NVarChar, 20);
            myCommand.Parameters["@userName"].Value = userName;

            //If the reader returns 0 rows, that means the username doesn't exist in the database, so step there and return an exception
            using (myReader)
            {
                myReader = myCommand.ExecuteReader();
                if (myReader.HasRows == false)
                {
                    InvalidUsernameException iue = new InvalidUsernameException();
                    throw iue;
                }
                else //Else we store the fields
                {
                    myAcc.Password = myReader[1].ToString();
                    isActive = Convert.ToBoolean(myReader[2]);
                    myAcc.Key = myReader[3].ToString();
                    myAcc.ExpiryDate = myReader[4].ToString();
                }
            }
        }
    }
}
catch (SqlException)
{
    throw new FaultException<AuthenticationException>(new AuthenticationException("There was an error while connecting the database, please contact support."), new FaultReason("Error"));
}
catch (InvalidOperationException)
{
    throw new FaultException<AuthenticationException>(new AuthenticationException("An error in the program while connecting to the database."), new FaultReason("Error"));
}
catch (InvalidUsernameException)
{
    throw new FaultException<AuthenticationException>(new AuthenticationException("The username you entered could not be found in our database."), new FaultReason("Error"));
}
catch (Exception)
{
    throw new FaultException<AuthenticationException>(new AuthenticationException("There was a general error during the process."), new FaultReason("Error"));
}
试试看
{
使用(myConnection)
{
使用(myCommand)
{
//告诉它在数据库上执行存储过程
myCommand.CommandText=“findUsername”;
myCommand.CommandType=CommandType.StoredProcess;
添加(“@userName”,SqlDbType.NVarChar,20);
myCommand.Parameters[“@userName”].Value=userName;
//如果读取器返回0行,这意味着该用户名在数据库中不存在,因此请执行步骤并返回异常
使用(myReader)
{
myReader=myCommand.ExecuteReader();
if(myReader.HasRows==false)
{
InvalidUsernameException iue=新的InvalidUsernameException();
投掷iue;
}
else//else我们存储字段
{
myAcc.Password=myReader[1].ToString();
isActive=Convert.ToBoolean(myReader[2]);
myAcc.Key=myReader[3].ToString();
myAcc.ExpiryDate=myReader[4].ToString();
}
}
}
}
}
捕获(SqlException)
{
抛出新的FaultException(新的AuthenticationException(“连接数据库时出错,请与支持人员联系”)、新的FaultReason(“错误”);
}
捕获(无效操作异常)
{
抛出新的FaultException(新的AuthenticationException(“连接到数据库时程序出错”)、新的FaultReason(“错误”);
}
捕获(InvalidUsernameException)
{
抛出新的FaultException(新的AuthenticationException(“您输入的用户名在我们的数据库中找不到”)、新的FaultReason(“错误”);
}
捕获(例外)
{
抛出新的FaultException(新的AuthenticationException(“过程中出现了一般错误”)、新的FaultReason(“错误”);
}

尝试将无参数构造函数添加到
AuthenticationException
类中。或:

[DataContract]
public class AuthenticationException
{
    [DataMember]
    public string ValidationError { get; set; }
}
为您服务:

throw new FaultException<AuthenticationException>(
    new AuthenticationException 
    { 
        ValidationError = "The username you entered could not be found in our database."
    }, 
    new FaultReason("Error")
);

抛出将传播错误的新FaultException,而不是在代码周围放置try/catch块。

尝试向
AuthenticationException
类添加无参数构造函数。或:

[DataContract]
public class AuthenticationException
{
    [DataMember]
    public string ValidationError { get; set; }
}
为您服务:

throw new FaultException<AuthenticationException>(
    new AuthenticationException 
    { 
        ValidationError = "The username you entered could not be found in our database."
    }, 
    new FaultReason("Error")
);

抛出新的FaultException,它将传播错误,而不是在代码周围放置try/catch块。

我在本页找到了问题的答案:

您需要禁用以下visual studio选项:


我在本页找到了问题的答案:

您需要禁用以下visual studio选项:


看起来您需要调用myReader.Read,然后再访问其字段。

看起来您需要调用myReader.Read,然后再访问其字段。

您能解释一下AuthenticationException和我的实现之间的区别吗?还有,为什么我需要一个空的构造函数呢?@gatekeeper,你的和我的不同之处在于我的构造函数是无参数的。为了正确地序列化/反序列化对象,它是必需的。此外,添加一个空构造函数对我没有任何帮助。对,那么可能在服务的其他部分发生了其他异常。尝试查看异常堆栈跟踪和内部异常。我将堆栈跟踪添加到原始问题中。我真的不知道它为什么会崩溃!你能解释一下你的AuthenticationException实现和我的有什么不同吗?还有,为什么我需要一个空的构造函数呢?@gatekeeper,你的和我的不同之处在于我的构造函数是无参数的。为了正确地序列化/反序列化对象,它是必需的。此外,添加一个空构造函数对我没有任何帮助。对,那么可能在服务的其他部分发生了其他异常。尝试查看异常堆栈跟踪和内部异常。我将堆栈跟踪添加到原始问题中。我真的不知道它为什么会崩溃!是的,这是另一个无关的错误,但谢谢你的提醒!是的,这是另一个无关的错误,但谢谢你的提醒!