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 7927441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:18:33+00:00 2026-06-03T19:18:33+00:00

I have a service hosted in a WPF application with an async method with

  • 0

I have a service hosted in a WPF application with an async method with the Begin/end methods, and when I catch an exception in the service, I want to throw a faultException to warn to the client.

However, when I try to throw the faultException, the host application crash, shutdown suddenly.

In my repository, I catch the UpdateException, then, I create a custom exception, UniqueKeyException, that is throw to the caller. The caller is an auxiliar method that is called in the Begin method.

This auxiliar method, catch the UniqyeKeyException and only do a “throw”, that is capture in the try/catch block of my end method. Here there is something that I don’t understand, why in the end mehod this exception is catched in the block of AgregateException instead of the UniqueKeyException.

Well, anyway, in the catch block of the end method, in the AgregateException block, I check if the innerException is UniqueKeyException, if it is true, I create an object UniqueKeyArgs (a custom class with the information to send to the client), create a FaultException and finally do the throw FaultException. It is in this step, the throw, where the host application crash.

I think that I have all configure correctly, because my custom class UniqueKeyArgs is decorate as Datacontract and its properties as DataMember, in the app.config of my host application I configure the behavior to include exception details and in the contract I decorate it with faultContract.

Why the application crash?

My code is the following:

REPOSITORY

public List<Usuers> updateUsers(List<Users> paramUsers)
{
....

catch(UpdateException ex)
{
SqlException innerEx = (SqlException)ex.InnerException;

                //Code 2627 is Unique Key exception from SQL Server.
                if (innerEx != null && innerEx.Number == 2627)
                {
                    //I create the conditions of searching
                    ConditionsUsers conditions = new conditions();

                    conditions.UserName = (Users)ex.StateEntries[0].Entity).userName;

                    //Search for the existing user
                    Users myUser = getUser(conditions);
                    string message = "the user " + conditions.userName + " exists.";
                    throw new UniqueKeyException(message, myUser);
                }
                throw;
}

SERVICE IMPLEMENTATION

//This is my auxiliar method, called in the Begin method.
private submitUpdates()
{
     ....

     catch(UniqueKeyException ex)
     {
          //The code enter here
          throw;
     }
}



public IAsyncResult BeginUpdateUsers(List<users> paramUsers, AsyncCallback callback, object state)
{
     Task<List<Users>> myTask= Task<List<Users>>.Factory.StartNew(p => sumbmitUpdates(paramUsers), state);
     return myTask.ContinueWith(res => callback(myTask));
}


public List<Users> EndUpdateusers(IAsyncResult result)
     {
          try
          {
               return ((Task<List<Users>>)result).Result;
          }
          //Why agregateException and not is catched in  the UniqueKeyException ???
          catch(AgregateException ex)
          {
               if (innerExceptions[0] is UsuariosValorUnicoException)
                    {
                        //I assign manually the data to debug, to discard other problems.
                        Users myUser = new Users();
                        myUser.UserName = "Jhon";
                        myUser.Password = "pass123";
                        UniqueKeyArgs myArgs = new UniqueUserArgs("unique key error", myUser);
                        FaultException<UniqueKeyArgs> myException = new FaultException<UniqueKeyArgs>(myArgs);

                        //Crash here, in the throw myException
                        throw myException;                        
                    }
                }
                throw;
          }

MY CONTRACT

[FaultContract(typeof(UniqueKeyArgs))]
IAsyncResult BeginUpdateUsers(List<Users> paramUser, AsyncCallback callback, object state);
List<Users> EndUpdateUsers(IAsyncResult result);

Crash when I throw myException in the End method.

I see in this post that the solution is catch the exception in the host application too, not only in the service object. However, this solution uses Application.ThreadException, that belong to System.Windows.Forms namespace, and I am using a WPF application.

How could I send the exception to the client from a service hosted in a WPF application?

Thanks.

EDIT1: well, I am use a try/catch block in the line where I throw the exception and I see that the error is that I have not indicated a reason, so when I create my FaultException I do:

FaultException<UniqueKeyArgs> myException = new FaultException<UniqueKeyArgs>(myArgs, new FaultReason("DummyReason");

In this case, the exception message is “DummyReason”, the message that I set in the FaultReason, so it says me nothing. The FaultException is not throw, and throw the generic exception to the client.

In this case the host application does not shutdown, but close the connection with the client and I have to reconnect.

It seems that the problem is the creaton of the FaultException, but I don’t see the problem.

@Roeal suggests that perhaps is only possible to use faultException with synch methods, but in this link I can see an example in which is used with async methods. I have seen others examples, is not the unique.

Thanks.

EDIT2: I solve my problem. My problem is that in the FaultException, T is an object that have a property that was a self tracking entity, and this is a problem, if I am not wrong, I only can use basic types as properties of the exception.

Then, in the exception, I have implmemented ISerialize. It’s needed to be able to send the information to the client, without this, the client receives an exception.Detail with null properties.

  • 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-03T19:18:40+00:00Added an answer on June 3, 2026 at 7:18 pm

    I solve my problem. My problem is that in the FaultException, T is an object that have a property that was a self tracking entity, and this is a problem, if I am not wrong, I only can use basic types as properties of the exception.

    Then, in the exception, I have implmemented ISerialize. It’s needed to be able to send the information to the client, without this, the client receives an exception.Detail with null properties.

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

Sidebar

Related Questions

I have a WCF service hosted in IIS and want to gain access to
I have a WCF service hosted in a Windows Forms application (.NET C# 4).
I have WPF client consuming WCF service hosted in IIS. For authentication I am
I have a WCF service hosted at IIS7 web application. It's created by a
I have a WCF service hosted in a Windows Service. I want a website
I have a WCF service hosted within a Windows service. There is s method
We have a service hosted behind our firewall that receives request forwarded through to
I have a WCF service hosted in IIS 7. It takes some minutes until
We have created a WCF service hosted in a windows service that handles Authentication
I have an wcf service that is hosted in II6. The service uses the

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.