Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8564247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:09:14+00:00 2026-06-11T17:09:14+00:00

I am trying to implement a FaultException in a WCF service. I referred various

  • 0

I am trying to implement a FaultException in a WCF service. I referred various articles. However it seems that FaultExceptions are never being catched at client.
I have written code like this,

Service Contract –

 [ServiceContract]
    public interface IService2
    {
        [OperationContract]
        [FaultContract(typeof(MyException))]
        [DebuggerStepThrough]
        MyException GetData(int value);
    }

    [DataContract]
    public class MyException 
    {
        [DataMember]
        public bool Result { get; set; }

        [DataMember]
        public string ErrorMessage { get; set; }

        [DataMember]
        public Exception MyInnerException { get; set; }

        [DataMember]
        public string ClientMessage { get; set; }
    }

Service Implementation –

public class Service1 : IService2
    {

        public MyException GetData(int value)
        {
            MyException mx = new MyException();
            try
            {
                double a = 44;
                if (value == 0)
                {
                    throw new DivideByZeroException();
                }
                double res = a / value;
                mx.ClientMessage = "Everything is well";
                mx.Result = true;
                return mx;
            }
            catch (DivideByZeroException dvex)
            {
                mx.Result = true;
                mx.ClientMessage = "Divide by zero";
                mx.MyInnerException = dvex;
                mx.ErrorMessage = dvex.StackTrace;
                throw new FaultException<MyException>(mx, dvex.ToString());
            }                      

        }
    }

Client (aspx.cs page) –

try
            {
                FaultContractsDemo.ServiceReference1.Service2Client o = new ServiceReference1.Service2Client();
                MyException data = o.GetData(0);
                if (data.Result)
                    lblMessage.Text = "All is well";
            }
            catch (FaultException<MyException> ex)
            {
                lblMessage.Text = ex.Detail.ClientMessage;
            }

The service layer throws an exception, but client never recieves any. I am getting unhandled exception like,

The underlying connection was closed: The connection was closed
unexpectedly.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:09:16+00:00Added an answer on June 11, 2026 at 5:09 pm

    I could reproduce this issue (in a different, but very similar situation). The problem is caused by this combination:

    // Fault class:
    [DataMember]
    public Exception MyInnerException { get; set; }
    
    // Just before throwing the FaultException
    mx.MyInnerException = dvex; // <-- DivideByZeroException
    

    WCF does not like serializing the DivideByZeroException, which is necessary to get it across the wire. Comment out that line and things will probably start to work.

    To add to this, normally you wouldn’t want to pass the inner exception (at least: not like this), because that would be considered a security risk. There’s one exception though: this is when you’re debugging. In that case, refer to the article on IncludeExceptionDetailInFaults: this allows you to see exception details on the client side.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to implement a MSMQ-backed WCF PubSub. I understand that net.msmq is one-way; however
I'm trying to implement an IErrorHandler in my WCF service in order to log
Trying to implement google C2DM service. registrationIntent.putExtra(app, PendingIntent.getBroadcast(context,0,new Intent(), 0)); registrationIntent.putExtra(sender,example@gmail.com); context.startService(registrationIntent); Almost every
Trying to implement some nested loops that are spitting out good old nested html
Trying to implement a function that will return a list of ints the represent
SetFocus I'm trying implement the above Se Focus code in a Class Library that
I've a webbrowser control and I'm trying implement IDocHostUIHandler in the container. However since
Im trying to implement inertial scrolling in my GLSurfaceView and to do that i
I am trying to call a WCF service from a Silverlight client using channel
I am currently trying implement a QThread that contains a socket connection. The socket

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.