/// <summary>
/// Registers the user.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="email">The email.</param>
public void RegisterUser( string username, string password, string email)
{
if ( /* username*/ )
throw new InvalidUsernameException();
if ( /* password*/ )
throw new InvalidPasswordException();
if ( /* email*/ )
throw new InvalidEmailException();
...
} * This source code was highlighted with Source Code Highlighter .
/// <summary>
/// Handles the Click event of the btnRegister control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnRegister_Click( object sender, EventArgs e)
{
try
{
RegisterUser(txtUsernamt.Text, txtPassword.Text, txtEmail.Text);
}
catch (InvalidUsernameException usernameException)
{
// Username
}
catch (InvalidPasswordException passwordException)
{
// Password
}
catch (InvalidEmailException emailException)
{
// Email
}
... * This source code was highlighted with Source Code Highlighter .
This means that the SOAP fault message is used to send the exception and you need to work with them a little differently.
In all managed objects, processing. In SOAP-based applications such as WCF applications. It’s not a problem. In addition, because of the XML form, it is a very interoperable approach.
/// <summary>
/// Username
/// </summary>
[DataContract]
class InvalidUsernameFault
{
[DataMember]
public string CustomError;
public InvalidUsernameFault()
{
}
public InvalidUsernameFault( string error)
{
CustomError = error;
}
}
/// <summary>
/// Password
/// </summary> [DataContract]
class InvalidPasswordFault
{
[DataMember]
public string CustomError;
public InvalidPasswordFault()
{
}
public InvalidPasswordFault( string error)
{
CustomError = error;
}
}
/// <summary>
/// Email
/// </summary>
[DataContract]
class InvalidEmailFault
{
[DataMember]
public string CustomError;
public InvalidEmailFault()
{
}
public InvalidEmailFault( string error)
{
CustomError = error;
}
} * This source code was highlighted with Source Code Highlighter .
[OperationContract]
[FaultContract( typeof (InvalidUsernameFault))]
[FaultContract( typeof (InvalidPasswordFault))]
[FaultContract( typeof (InvalidEmailFault))]
void RegisterUser( string username, string password, string email);
* This source code was highlighted with Source Code Highlighter .
/// <summary>
/// Registers the user.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="email">The email.</param>
public void RegisterUser( string username, string password, string email)
{
if ( /* username*/ )
throw new FaultException<InvalidUsernameFault>( new InvalidUsernameFault());
if ( /* password*/ )
throw new FaultException<InvalidPasswordFault>( new InvalidPasswordFault());
if ( /* email*/ )
throw new FaultException<InvalidEmailFault>( new InvalidEmailFault());
...
} FaultException . , : /// <summary>
/// Registers the user.
/// </summary>
/// <param name="username">The username.</param>
/// <param name="password">The password.</param>
/// <param name="email">The email.</param>
public void RegisterUser( string username, string password, string email)
{
if ( /* username*/ )
throw new FaultException<InvalidUsernameFault>( new InvalidUsernameFault(“ Medved ”));
if ( /* password*/ )
throw new FaultException<InvalidPasswordFault>( new InvalidPasswordFault(“ '12345' ”));
if ( /* email*/ )
throw new FaultException<InvalidEmailFault>( new InvalidEmailFault(“ ya@krasafcheg.ru”));
...
} * This source code was highlighted with Source Code Highlighter .
/// <summary>
/// Handles the Click event of the btnRegister control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
protected void btnRegister_Click( object sender, EventArgs e)
{
try
{
…
wcfclient.RegisterUser(txtUsernamt.Text, txtPassword.Text, txtEmail.Text);
}
catch (FaultException<InvalidUsernameFault> usernameException)
{
// Username
}
catch (FaultException<InvalidPasswordFault> passwordException)
{
// Password
}
catch (FaultException<InvalidEmailFault> emailException)
{
// Email
}
catch (FaultException faultEx)
{
// ,
// WCF
} ...
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/41638/
All Articles