Asked by:
CustomError class

Question
-
User136806914 posted
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
Friday, August 9, 2019 2:50 PM
All replies
-
User753101303 posted
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.
Friday, August 9, 2019 3:56 PM -
User136806914 posted
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
Friday, August 9, 2019 4:41 PM -
User475983607 posted
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
Friday, August 9, 2019 5:06 PM -
User-330142929 posted
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; }
App.config<system.serviceModel> <services> <service name="VM1.MyService"> <endpoint address="" binding="basicHttpBinding" contract="VM1.IService" > </endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" ></endpoint> <host> <baseAddresses> <add baseAddress="http://localhost:5566"/> </baseAddresses> </host> </service> </services> <behaviors> <serviceBehaviors> <behavior> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
Client(Console application) call by adding service reference.ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient(); try { client.Test("a", "124", "234", 134); } catch (FaultException<ErrorDetail> e) { Console.WriteLine(e.Reason.GetMatchingTranslation().Text); ErrorDetail ed = e.Detail; StringBuilder sb = new StringBuilder(); sb.Append(ed.UserNameFaultInfo); sb.Append(ed.PasswordFaultInfo); sb.Append(ed.AgeFaultInfo); Console.WriteLine(sb.ToString()); }
Result.
Feel free to let me know if there is anything I can help with.
Best Regards
Abraham
Tuesday, August 27, 2019 9:04 AM