ChannelFactory`1[BLLService], cannot be modified while it is in the Opening state

可紊 提交于 2021-02-10 23:41:06

问题


Im in showstopper state because of an exception in WCF. The problem is that i am randomly getting this exception in windows service in WCF. Now since i cannot debug production server so i am using log4net for logging. The exception is random in time and in different functions. it Occurs few in a day with almost 400 to 1000 db entries. Now here is my proxy class code where iam checking the client open state.

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;
            }
        }

Which means if client is not in open state then open it or else return client

The exception is

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) 

Tried to search but couldn't find modified type exception

edite: after using @Agalo answer now the exception has changed to

   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)

回答1:


You can try either of 2 solutions listed below

  1. In your particular case, it might be possible that the inner ChannelFactory is already in Created/Opened state and when you are trying to Open the Proxy, it is calling the Open method of channel Factory which is throwing the invalid operation exception. To Resolve this, you need to check the State of Channel Factory, before opening the Proxy. So you need to validate if the channel factory is already created, then you need not open the proxy again.

    _bLLServiceClient.ChannelFactory.State != CommunicationState.Created
    

Modified function code

    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;
            }
        }
  1. One of the solution, is to avoid using the Open method. When you call any WCF Service method Open call would be made internally. So It will be invoked automatically.


来源:https://stackoverflow.com/questions/34548570/channelfactory1bllservice-cannot-be-modified-while-it-is-in-the-opening-stat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!