0

我的应用程序有 10 个 WCFService(平台 .NET Framework 3.5 上的 WCFService 应用程序)具有相同的软件和硬件,但只有 1 个接受此例外:

当用户登录时调用此方法:

  public IService Select(SelectServiceRequest request)
        {
            IAxxxService anagServ = IoC.Container.Resolve<IAxxxService>(request.GetRegisteredService().ToString());
            return xxxServ;
        }
  • 使用 GetRegisteredService() 实现,第一次执行时 ClientIdentifier = 0

    public RegisteredServices GetRegisteredService()
        {
            RegisteredServices res = RegisteredServices.Estxxx;
            if (ClientIdentifier == 0)
            {
                res = RegisteredServices.Anaxxx;
            }
            else if (ClientIdentifier == 1)
            {
                res = RegisteredServices.Prixxx;
            }
            else if (ClientIdentifier == 2)
            {
                res = RegisteredServices.Estrxxx;
            }
    
    
            else if (ClientIdentifier == 3)
            {
                res = RegisteredServices.LixxAnagrxx;
            }
    
            return res;
        }
    
    • 使用 IOC 代码实现和初始化:

      internal class IoC{
        private static IUnityContainer container = new UnityContainer();
        private static bool isInitialized = false;
      
      public static IUnityContainer Container
      {
          get
          {
              if (!isInitialized)
              {
                  lock (container)
                  {
                      if (!isInitialized)
      
      
                          container.RegisterType<IService, EstxxService>(RegisteredServices.Esxxxxx.ToString());
                          container.RegisterType<IService, StaxxxService>(RegisteredServices.Anaxxxx.ToString());
                          container.RegisterType<IService, PrixxxService>(RegisteredServices.Prixxxx.ToString()); 
                          container.RegisterType<IService, LixxxAxxxService>(RegisteredServices.LixxxAnagxx.ToString()); 
                                      isInitialized = true;
      
                      }
                  }
              }
              return container;
          }
      
      }
      

      }

我在执行方法“解决”上有这个例外:

System.Reflection.TargetInvocationException:调用的目标已引发异常。---> Microsoft.Practices.Unity.ResolutionFailedException:依赖项解析失败,类型 = “ApCon.IService”,名称 = “Anagxxx”。异常消息是:当前构建操作(构建密钥 Build Key[ApCon.StandardService, Anaxxxx])失败:当前构建操作(构建密钥 Build Key[ApCon.StandardService, Anaxxxx])失败:索引超出了数组的范围. (策略类型 DynamicMethodConstructorStrategy,索引 0)(策略类型 BuildPlanStrategy,索引 3)---> Microsoft.Practices.ObjectBuilder2.BuildFailedException:当前构建操作(构建密钥 Build Key[ApCon.StandardService,Anagrafe])失败:当前的构建操作(构建键 Build Key[ApCon.StandardService, Anagrafe])失败:索引超出了数组的范围。(策略类型 DynamicMethodConstructorStrategy,索引 0)(策略类型 BuildPlanStrategy,索引 3)---> Microsoft.Practices.ObjectBuilder2.BuildFailedException:当前构建操作(构建密钥 Build Key[ApCon.StandardService,Anagxxx])失败:索引在外部数组的边界。(策略类型 DynamicMethodConstructorStrategy,索引 0)---> System.IndexOutOfRangeException:索引超出了数组的范围。在 System.Collections.Generic.List1.Add(T item) 在 Microsoft.Practices.ObjectBuilder2.DependencyResolverTrackerPolicy.AddResolverKey(Object key) 在 Microsoft.Practices.ObjectBuilder2。

会不会是锁定指令?似乎类型没有注册并且 isInitialized 变为 true

4

2 回答 2

0

我认为这与未注册的服务无关。这通常会引发此异常:

当前类型 IFoo 是一个接口,无法构造。您是否缺少类型映射?

这可能是锁定的结果。你可以试试这个模式:

public static class IocContainer
{
    private static readonly Lazy<IUnityContainer> Container = new Lazy<IUnityContainer>(() =>
    {
        var container = new UnityContainer();

        // Do your registrations.
        container.RegisterType<IService, EstattoriService>(RegisteredServices.Estrattori.ToString());
        container.RegisterType<IService, StandardService>(RegisteredServices.Anagrafica.ToString());
        container.RegisterType<IService, PrivacyService>(RegisteredServices.Privacy.ToString()); 
        container.RegisterType<IService, ListAnagService>(RegisteredServices.ListaAnagrafica.ToString()); 

        return container;
    });

    public static IUnityContainer Instance
    {
        get { return Container.Value; }
    }
}

如果这不是一个选项,您可以尝试不锁定容器本身:

internal class IoC{
  private static IUnityContainer container = new UnityContainer();
  private static bool isInitialized = false;
  private static readonly object padlock = new object(); // lock object.

public static IUnityContainer Container
{
    get
    {
        if (!isInitialized)
        {
            lock (padlock) // Lock on padlock instead.
            {
                if (!isInitialized)


                    container.RegisterType<IService, EstattoriService>(RegisteredServices.Estrattori.ToString());
                    container.RegisterType<IService, StandardService>(RegisteredServices.Anagrafica.ToString());
                    container.RegisterType<IService, PrivacyService>(RegisteredServices.Privacy.ToString()); 
                    container.RegisterType<IService, ListAnagService>(RegisteredServices.ListaAnagrafica.ToString()); 
                                isInitialized = true;

                }
            }
        }
        return container;
    }
}

我发现这是单例模式的一个很好的来源。

http://cshapindepth.com/Articles/General/Singleton.aspx

于 2016-04-21T05:32:04.863 回答
0

据我所知,您正在注册 的命名实例IAnagrafeService,但试图解析 . 的命名实例IService

我的猜测是第一个来自第二个,所以这样的事情应该有效:

string name = request.GetRegisteredService().ToString();
IService anagServ = IoC.Container.Resolve<IAnagrafeService>(name);

(当然,您也可以更改 Unity 注册。)

于 2016-04-19T15:54:49.547 回答