It's a WCF service & i am trying to create a CustomException. Yes it is to report errors on the submitted data from a web page & need to throw an exception when a particular condition meets.
I created a serializable class & inherited Exception class, in the constructor i passed error message that need to be displayed. Along with that i also need to send List back to UI & i am trying to figure that out
Hi,
If we just want to capture the server custom errors on the client-side, we could use FaultException class.
Server-side.
public string Test(string name)
{
if (name.Length<2)
{
FaultReasonText text = new FaultReasonText("name string length should greater than two ");
FaultReason fr = new FaultReason(text);
throw new FaultException(fr);
}
return DateTime.Now.ToString();
}
Client-side.
ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
try
{
var result = client.Test("A");
Console.WriteLine(result);
}
catch (FaultException e)
{
Console.WriteLine(e.Reason.GetMatchingTranslation().Text);
}
We could also use the custom class. Server-side(Console application).
class Program
{
static void Main(string[] args)
{
using (ServiceHost sh = new ServiceHost(typeof(MyService)))
{
sh.Open();
Console.WriteLine("serivce is ready....");
Console.ReadLine();
sh.Close();
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
[FaultContract(typeof(ErrorDetail))]
void Test(string username,string password,string password2,int age);
}
public class MyService : IService
{
public void Test(string username,string password,string password2,int age)
{
ErrorDetail detail = new ErrorDetail();
if (username.Length<2)
{
detail.UserNameFaultInfo = "\nusername string length should greater than 2";
}
if (password.Length<6)
{
detail.PasswordFaultInfo = "\npassword string length should greater than 2";
}
if (password2!=password)
{
detail.PasswordFaultInfo += "\npassword is not matched";
}
if (age<0||age>120)
{
detail.AgeFaultInfo = "\nage is not valid";
}
if (detail.UserNameFaultInfo!=null||detail.PasswordFaultInfo!=null||detail.AgeFaultInfo!=null)
{
throw new FaultException<ErrorDetail>(detail, "there is something wrong with the input information");
}
}
}
[DataContract]
public class ErrorDetail
{
[DataMember]
public string UserNameFaultInfo { get; set; } = null;
[DataMember]
public string PasswordFaultInfo { get; set; } = null;
[DataMember]
public string AgeFaultInfo { get; set; } = null;
}
Feel free to let me know if there is anything I can help with.
Best Regards
Abraham
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
36 Points
125 Posts
CustomError class
Aug 09, 2019 02:50 PM|PraveenAmar|LINK
Hi,
I need to create a CustomError class & send a list to the client How do i approach?
I tried to follow this tutorial but couldn't figure out how to send a list
https://www.tutorialsteacher.com/csharp/custom-exception-csharp
Thanks,
Praveen
All-Star
48710 Points
18182 Posts
Re: CustomError class
Aug 09, 2019 03:56 PM|PatriceSc|LINK
Hi,
Some more context could help. You have an existing service of which kind ? This is to report errors on submitted data ?
If possible you could model your approach based rather on https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api ie if you return a https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400 status code to tell the request sends invalid data all along with a json payload describing the errors.
Member
36 Points
125 Posts
Re: CustomError class
Aug 09, 2019 04:41 PM|PraveenAmar|LINK
It's a WCF service & i am trying to create a CustomException. Yes it is to report errors on the submitted data from a web page & need to throw an exception when a particular condition meets.
I created a serializable class & inherited Exception class, in the constructor i passed error message that need to be displayed. Along with that i also need to send List back to UI & i am trying to figure that out
All-Star
53661 Points
24019 Posts
Re: CustomError class
Aug 09, 2019 05:06 PM|mgebhard|LINK
The common approach is returning a custom soap exception. The official WCF documentation covers the details.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/sending-and-receiving-faults
https://docs.microsoft.com/en-us/dotnet/framework/wcf/specifying-and-handling-faults-in-contracts-and-services
Member
740 Points
321 Posts
Re: CustomError class
Aug 27, 2019 09:04 AM|Abraham Qian|LINK
Hi,
If we just want to capture the server custom errors on the client-side, we could use FaultException class.
Server-side.
Client-side.
We could also use the custom class.
Server-side(Console application).
App.config
Client(Console application) call by adding service reference.
Result.
Feel free to let me know if there is anything I can help with.
Best Regards
Abraham