简体   繁体   English

字典初始化期间发生异常

[英]Exception during dictionary initialization

I'm getting a type initialization error when trying to invoke a method from WCF service. 尝试从WCF服务调用方法时出现类型初始化错误。

The type initializer for 'XYZAuditService' threw an exception. “ XYZAuditService”的类型初始值设定项引发了异常。

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. 无论我尝试了什么,我似乎都无法使VS调试器停止在类型初始化器中。 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 . 我的代码本身不执行任何操作,只是初始化工厂字典,甚至不调用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 将您的lambda更改为

() => { 
    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. IAuditor在从一个单独的程序定义AuditService ,而且由于部署过程中的问题,该IAuditor接口的组件正确不包括在应用程序的bin目录。 It was giving me an error during type initialization, because that was the point where the assembly load was occurring. 在类型初始化期间给了我一个错误,因为那是发生组装负载的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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