Visual studio 2010 ChannelFactory`1[BLLService]处于打开状态时无法修改

Visual studio 2010 ChannelFactory`1[BLLService]处于打开状态时无法修改,visual-studio-2010,wcf,exception,c#-4.0,Visual Studio 2010,Wcf,Exception,C# 4.0,由于WCF中出现异常,Im处于showstopper状态。问题是,我在WCF的windows服务中随机获得此异常。现在,由于我无法调试生产服务器,所以我使用log4net进行日志记录。异常在时间和不同功能中是随机的。它在一天中很少发生,几乎有400到1000 db的条目。现在这里是我的代理类代码,iam在其中检查客户端打开状态 public static BLLServiceClient bLLServiceClient { get

由于WCF中出现异常,Im处于showstopper状态。问题是,我在WCF的windows服务中随机获得此异常。现在,由于我无法调试生产服务器,所以我使用log4net进行日志记录。异常在时间和不同功能中是随机的。它在一天中很少发生,几乎有400到1000 db的条目。现在这里是我的代理类代码,iam在其中检查客户端打开状态

public static BLLServiceClient bLLServiceClient
        {
            get
            {
                if (_bLLServiceClient == null || _bLLServiceClient.State != CommunicationState.Opened)
                {
     _bLLServiceClient = new BLLServiceClient(Common.GetBinding(_settings.BLLServiceBinding), new EndpointAddress(_settings.BLLServiceAddress));                        
     _bLLServiceClient.Open();
                }                  
                return _bLLServiceClient;
            }
            set
            {
                _bLLServiceClient = value;
            }
        }
这意味着,若客户端未处于打开状态,则打开它,否则返回客户端

例外是

SetSiteDataStatusInDatabase:  ____ The communication object, System.ServiceModel.ChannelFactory`1[BLLService], cannot be modified while it is in the Opening state.
System.ServiceModel
   at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrImmutable()
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ChannelFactory.EnsureOpened()
   at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannelInternal()
   at System.ServiceModel.ClientBase`1.get_Channel()
   at BLLServiceClient.UpdateData(SiteData eSiteData)
   at DataHelper.SetSiteDataStatusInDatabase(SiteData oSiteData, CurrentTaskStatus newStatus)
Void ThrowIfDisposedOrImmutable()
System.Collections.ListDictionaryInternal
 Full Exception....
System.InvalidOperationException: The communication object, System.ServiceModel.ChannelFactory`1[ BLLService], cannot be modified while it is in the Opening state.
   at System.ServiceModel.Channels.CommunicationObject.ThrowIfDisposedOrImmutable()
   at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
   at System.ServiceModel.ChannelFactory.EnsureOpened()
   at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.ChannelFactory`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannel()
   at System.ServiceModel.ClientBase`1.CreateChannelInternal()
   at System.ServiceModel.ClientBase`1.get_Channel()
   at Proxy.BLLService.BLLServiceClient.UpdateData(SiteData eSiteData)
   at DataHelper.SetSiteDataStatusInDatabase(SiteData oSiteData, CurrentTaskStatus newStatus) 
尝试搜索,但找不到修改的类型异常

伊迪特: 使用@Agalo answer后,异常已更改为

   Exception of type 'System.OutOfMemoryException' was thrown.
System
   at System.Uri.CreateHelper(String uriString, Boolean dontEscape, UriKind uriKind, UriFormatException& e)
   at System.Uri.TryCreate(String uriString, UriKind uriKind, Uri& result)
   at System.ServiceModel.Description.NamingHelper.CheckUriParameter(String ns, String paramName)
   at System.ServiceModel.Description.MessagePartDescription..ctor(String name, String ns)
   at System.ServiceModel.Description.TypeLoader.CreateParameterPartDescription(XmlName defaultName, String defaultNS, Int32 index, ICustomAttributeProvider attrProvider, Type type)
   at System.ServiceModel.Description.TypeLoader.CreateParameterMessageDescription(ParameterInfo[] parameters, Type returnType, ICustomAttributeProvider returnAttrProvider, XmlName returnValueName, String methodName, String defaultNS, String action, XmlName wrapperName, String wrapperNamespace, MessageDirection direction)
   at System.ServiceModel.Description.TypeLoader.CreateMessageDescription(MethodInfo methodInfo, Boolean isAsync, Boolean isTask, Type taskTResult, XmlName returnValueName, String defaultNS, String action, XmlName wrapperName, String wrapperNamespace, MessageDirection direction)
   at System.ServiceModel.Description.TypeLoader.CreateOperationDescription(ContractDescription contractDescription, MethodInfo methodInfo, MessageDirection direction, ContractReflectionInfo reflectionInfo, ContractDescription declaringContract)
   at System.ServiceModel.Description.TypeLoader.CreateOperationDescriptions(ContractDescription contractDescription, ContractReflectionInfo reflectionInfo, Type contractToGetMethodsFrom, ContractDescription declaringContract, MessageDirection direction)
   at System.ServiceModel.Description.TypeLoader.CreateContractDescription(ServiceContractAttribute contractAttr, Type contractType, Type serviceType, ContractReflectionInfo& reflectionInfo, Object serviceImplementation)
   at System.ServiceModel.Description.TypeLoader.LoadContractDescriptionHelper(Type contractType, Type serviceType, Object serviceImplementation)
   at System.ServiceModel.ChannelFactory`1.CreateDescription()
   at System.ServiceModel.ChannelFactory.InitializeEndpoint(Binding binding, EndpointAddress address)
   at System.ServiceModel.ChannelFactory`1..ctor(Binding binding, EndpointAddress remoteAddress)
   at System.ServiceModel.ClientBase`1..ctor(Binding binding, EndpointAddress remoteAddress)
   at BLLServiceClient..ctor(Binding binding, EndpointAddress remoteAddress)
   at ServiceClients.get_bLLServiceClient()
   at SiteDataHelper.get_bLLServiceClient()
   at SiteDataHelper.FetchFilesListToDownload(Int32 siteDataId)
System.Uri CreateHelper(System.String, Boolean, System.UriKind, System.UriFormatException ByRef)
System.Collections.ListDictionaryInternal
 Full Exception....
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Uri.CreateHelper(String uriString, Boolean dontEscape, UriKind uriKind, UriFormatException& e)
   at System.Uri.TryCreate(String uriString, UriKind uriKind, Uri& result)
   at System.ServiceModel.Description.NamingHelper.CheckUriParameter(String ns, String paramName)
   at System.ServiceModel.Description.MessagePartDescription..ctor(String name, String ns)
   at System.ServiceModel.Description.TypeLoader.CreateParameterPartDescription(XmlName defaultName, String defaultNS, Int32 index, ICustomAttributeProvider attrProvider, Type type)
   at System.ServiceModel.Description.TypeLoader.CreateParameterMessageDescription(ParameterInfo[] parameters, Type returnType, ICustomAttributeProvider returnAttrProvider, XmlName returnValueName, String methodName, String defaultNS, String action, XmlName wrapperName, String wrapperNamespace, MessageDirection direction)
   at System.ServiceModel.Description.TypeLoader.CreateMessageDescription(MethodInfo methodInfo, Boolean isAsync, Boolean isTask, Type taskTResult, XmlName returnValueName, String defaultNS, String action, XmlName wrapperName, String wrapperNamespace, MessageDirection direction)
   at System.ServiceModel.Description.TypeLoader.CreateOperationDescription(ContractDescription contractDescription, MethodInfo methodInfo, MessageDirection direction, ContractReflectionInfo reflectionInfo, ContractDescription declaringContract)
   at System.ServiceModel.Description.TypeLoader.CreateOperationDescriptions(ContractDescription contractDescription, ContractReflectionInfo reflectionInfo, Type contractToGetMethodsFrom, ContractDescription declaringContract, MessageDirection direction)
   at System.ServiceModel.Description.TypeLoader.CreateContractDescription(ServiceContractAttribute contractAttr, Type contractType, Type serviceType, ContractReflectionInfo& reflectionInfo, Object serviceImplementation)
   at System.ServiceModel.Description.TypeLoader.LoadContractDescriptionHelper(Type contractType, Type serviceType, Object serviceImplementation)
   at System.ServiceModel.ChannelFactory`1.CreateDescription()
   at System.ServiceModel.ChannelFactory.InitializeEndpoint(Binding binding, EndpointAddress address)
   at System.ServiceModel.ChannelFactory`1..ctor(Binding binding, EndpointAddress remoteAddress)
   at System.ServiceModel.ClientBase`1..ctor(Binding binding, EndpointAddress remoteAddress)
   at BLLServiceClient..ctor(Binding binding, EndpointAddress remoteAddress)
   at ServiceClients.get_bLLServiceClient()
   at SiteDataHelper.get_bLLServiceClient()
   at SiteDataHelper.FetchFilesListToDownload(Int32 siteDataId)

您可以尝试下面列出的两种解决方案之一

  • 在您的特定情况下,inner ChannelFactory可能已经处于创建/打开状态,当您尝试打开代理时,它正在调用channel Factory的Open方法,该方法引发无效操作异常。要解决此问题,您需要检查channel Factory的状态,在打开代理之前。因此,您需要验证通道工厂是否已创建,然后无需再次打开代理

    _bLLServiceClient.ChannelFactory.State != CommunicationState.Created
    
  • 修改的功能代码

        public static BLLServiceClient bLLServiceClient
            {
                get
                {
                    if (_bLLServiceClient == null || _bLLServiceClient.State != CommunicationState.Opened 
                         || _bLLServiceClient.ChannelFactory.State != CommunicationState.Created) 
                    {
                        _bLLServiceClient = new BLLServiceClient(Common.GetBinding(_settings.BLLServiceBinding), new EndpointAddress(_settings.BLLServiceAddress));
                        _bLLServiceClient.Open();
                    }
                    return _bLLServiceClient;
                }
                set
                {
                    _bLLServiceClient = value;
                }
            }
    
  • 解决方案之一是避免使用打开方法。当您调用任何WCF服务方法时,都会在内部进行调用。因此它将被自动调用

  • 我错过的一件事是,我正在打开5个windows服务实例,这些实例正在访问同一个对象。我将尝试锁定该对象,但仍然收到相同的异常。第一个例外,我还使用了“锁定”功能。这有助于解决此问题,但现在出现了内存不足异常“自动内存异常”意味着分配的内存和可用内存出现问题。看看代码,你是如何清理资源的。我不喜欢你的答案。首先,在他的原始代码中,为什么函数不总是第二次和第三次失败?其次,您的答案是基于读入、非锁定状态更改其行为。这很容易受到比赛条件的影响,因此我认为这不是正确的答案。