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

  • Home
  • SEARCH
  • 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 3322322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:10:05+00:00 2026-05-17T23:10:05+00:00

I’m trying to create an AppDomain and attach to its UnhandledException event from F#,

  • 0

I’m trying to create an AppDomain and attach to its UnhandledException event from F#, and I’m not having much luck. In my event handler I need a reference to the domain that fired the event, as well as the event arguments.

To reproduce this issue, the following code must be run in a compiled application. Running it in F# Interactive produces a different, probably-not-related error.

open System
open System.Reflection

let domainError (domain:AppDomain) (args:UnhandledExceptionEventArgs) =
    ()

let domain = AppDomain.CreateDomain("test")
domain.UnhandledException
|> Event.add (domainError domain)

This compiles just fine, but at runtime I get the following error:

SerializationException: Type ‘Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers+h@720’ in assembly ‘FSharp.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ is not marked as serializable.

The stack trace that goes along with this error is as follows:

at System.AppDomain.add_UnhandledException(UnhandledExceptionEventHandler value)
at Program.clo@9.Invoke(UnhandledExceptionEventHandler eventDelegate) in c:\users\joel\documents\visual studio 2010\Projects\EventTest\Program.fs:line 9
at Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers.CreateEvent@716.Subscribe(IObserver`1 observer)
at Microsoft.FSharp.Control.CommonExtensions.SubscribeToObservable[T](IObservable`1 x, FSharpFunc`2 callback)
at Microsoft.FSharp.Control.EventModule.Add[T,TDel](FSharpFunc`2 callback, IEvent`2 sourceEvent)
at <StartupCode$EventTest>.$Program.main@() in c:\users\joel\documents\visual studio 2010\Projects\EventTest\Program.fs:line 9

Now I obviously can’t make Microsoft.FSharp.Core.CompilerServices.RuntimeHelpers+h@720 serializable, so can anyone suggest a workable way of attaching to this particular event? Thanks for any suggestions.

Update

Thanks to a suggestion from ChaosPandion, this version works:

open System
open System.Reflection

let domainError (sender:obj) (args:UnhandledExceptionEventArgs) =
    let problemDomain = sender :?> AppDomain
    printfn "Unhandled exception in app domain: %s" problemDomain.FriendlyName
    ()

let domain = AppDomain.CreateDomain("test")
domain.UnhandledException.AddHandler(UnhandledExceptionEventHandler(domainError))
  • 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-05-17T23:10:06+00:00Added an answer on May 17, 2026 at 11:10 pm

    After a bit more tweaking I found this example will fail:

    let domainError (domain:AppDomain) =
            let handler (sender:obj) (e:UnhandledExceptionEventArgs) = 
                let msg = (e.ExceptionObject :?> Exception).Message
                printfn "An exception was unhandled in %s\nMessage:%s" domain.FriendlyName msg
            new UnhandledExceptionEventHandler(handler)
    
    let main() =
        let domain = AppDomain.CreateDomain("test")
        let handler = domainError domain
        domain.UnhandledException.AddHandler handler
    

    I broke open Reflector and found the root cause. (Which is actually quite obvious now that I think about it.)

    internal class handler@28 : OptimizedClosures.FSharpFunc<object, UnhandledExceptionEventArgs, Unit>
    {
        // Fields
        public AppDomain domain;
    
        // Methods
        internal handler@28(AppDomain domain);
        public override Unit Invoke(object sender, UnhandledExceptionEventArgs e);
    }
    

    This pretty much means that the only way you can get this to work is to not create the closure around the domain object. I am not sure if this will work for you but you may want to try using the following from within the handler.

    AppDomain.CurrentDomain
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.