简体   繁体   中英

Exception during dictionary initialization

I'm getting a type initialization error when trying to invoke a method from WCF service.

The type initializer for 'XYZAuditService' threw an exception.

However, I cannot see anything in the code which would cause the exception.

public enum AuditProcessType
{
    Facility,
    Patient,
    Report,
}


[ServiceBehavior(Name = "AuditService",
    Namespace = "http://xyz/services/2008/08",
    InstanceContextMode = InstanceContextMode.PerSession,
    ConcurrencyMode = ConcurrencyMode.Single)]
public class AuditService : IAuditServiceContract
{
    private static Dictionary<AuditProcessType, Func<IAuditor>> auditServiceFactories;

    static AuditService()
    {
        auditServiceFactories = new Dictionary<AuditProcessType, Func<IAuditor>>()
        {
            { AuditProcessType.Facility, () => Auditor<Facility>.Instance },
            { AuditProcessType.Patient, () => Auditor<Patient>.Instance },
            { AuditProcessType.Report, () => Auditor<Report>.Instance },
        };
    }

    // ...

    private static IAuditor GetAuditor(AuditProcessType process)
    {
        Func<IAuditor> factory;
        if (!auditServiceFactories.TryGetValue(process, out factory) || factory == null)
        {
            throw new ArgumentException(..., "process");
        }

        IAuditor auditor = null;
        try
        {
            auditor = factory();
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException(..., ex);
        }

        if (auditor == null)
        {
            throw new InvalidOperationException(...);
        }

        return auditor;
    }
}

public class Auditor<T> : IAuditor
{
    private static Auditor<T> _instance = new Auditor<T>();

    public static Auditor<T> Instance
    {
        get
        {
            return _instance;
        }
    }

    private Auditor()
    {
    }

    // ...
}

My logging framework does not show any inner exception details, so I can't get more information than this. No matter what I've tried, I can't seem to get the VS debugger to stop inside the type initializer. Still, I don't see how this code is raising an exception. My code by itself does nothing but initialize the factories dictionary, it doesn't even invoke AuditService<T>.Instance . What else could be causing this?

In order for a type initializer to fail it must execute some code - and that code must fail. Looking at what you have posted, the only piece of executable code is here:

{ ... , () => Auditor<Facility>.Instance },
{ ... , () => Auditor<Patient>.Instance },
{ ... , () => Auditor<Report>.Instance },

Change your lambdas to

() => { 
    try { return Auditor<Facility>.Instance;  } 
        catch (Exception ex) 
        { System.Diagnostics.WriteLine(ex.Message); } 
}

and see if that doesn't catch some sort of error.

I tracked this down to an assembly load error. IAuditor is defined in a separate assembly from the AuditService , and because of issues during deployment, the IAuditor interface's assembly properly wasn't included in the application's bin directory. It was giving me an error during type initialization, because that was the point where the assembly load was occurring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM