none
IsInitiating property of OperationContract throwing unhandle InvalidOperationException RRS feed

  • Question

  • Operation contract methods:
            [OperationContract(IsInitiating = false)]
            DataModels.TransactionStatus GetStatusByTransactionID(int TransactionID);

            [OperationContract(Name="Authenticate", IsInitiating = true)]
            bool Authenticate();

    When I am trying to call GetStatusByTransactionID method first its throwing unhandled InvalidOperationException .

    Where to handle this exception in service.

    I want to return custom message to client

    Thanks

    Friday, January 17, 2014 12:30 PM

All replies

  • Hi,

    You can definitely catch and handle all exceptions that happen on your service class and turn them into a FaultException or FaultException exception.

    That way, you won't "fault" (or tear down) the communications channel between your client and server.

    Even better approach would be to implement the IErrorHandler interface on your service class that provides a way to globally catch all exceptions as they happen and provide a FaultException instead, that's SOAP compliant.

    You can even turn your IErrorHandler into a configurable behavior that can be turned on or off in config.

    Please see these articles and blog posts for more details:
    #WCF Exception Handling:
    http://www.codeproject.com/Articles/636843/WCF-Exception-Handling .

    #Implementing IErrorHandler:
    http://www.neovolve.com/post/2008/04/07/implementing-ierrorhandler.aspx .

    #Useful WCF Behaviors - IErrorHandler:
    http://www.steverb.com/post/2008/11/24/Useful-WCF-Behaviors-IErrorHandler.aspx .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, January 20, 2014 9:43 AM
    Moderator
  • Hi Amy,

    Thanks for reply.

    I have tried IrrorHandler to catch global exceptions. Event its showing "InvalidOperationexception"

    The operation 'GetStatusByTransactionID' cannot be the first operation to be called because IsInitiating is false.

    I am unable to catch this exception in IErrorHandler.

    IErrorHandler:

     
    public class ServiceErrorHandler : IErrorHandler    {        public ServiceErrorHandler(ServiceFaultBehavior behaviour)        {    
    
       }      
      public bool HandleError(Exception ex)      
      {          
      if (ex != null)        
        {     
               return true;  
              }          
      return false;     
       }     
       public void ProvideFault(System.Exception error,            System.ServiceModel.Channels.MessageVersion version,            ref System.ServiceModel.Channels.Message fault)     
       {           
     // If error is already a FaultException, then let it pass through          
      if (!(error is FaultException))     
           {           
    
         // Create custom fault         
           MyCustomFault customFault = new MyCustomFault() { FaultDetails = "Something gone wrong" };   
                 // Create the fault exception of the type of fault             
                   FaultException<MyCustomFault> Constants= new FaultException<MyCustomFault>(                  customFault, error.Message,                   FaultCode.CreateSenderFaultCode("MyCustomFault", "http://Sample.com/code/wcf/"));            
        // Create the message fault         
            MessageFault mf = Constants.CreateMessageFault();                // Update reference to point to the message                fault = Message.CreateMessage(version, mf, Constants.Action);          
      }       
     }


    ServiceFaultBehavior Implements IServiceBehaviour

    public class ServiceFaultBehavior : Attribute, IServiceBehavior 
       {     
       public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)        {    
            return;     
       } 
       public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)        {           
     IErrorHandler errorHandler;            
    errorHandler = new ServiceErrorHandler(this);            foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)            
    {                
    ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;                channelDispatcher.ErrorHandlers.Add(errorHandler);            }        
    }        
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)        
    {            
    foreach (var svcEndPoint in serviceDescription.Endpoints)            {               
     // Don't check mex                
    if (svcEndPoint.Contract.Name != "IMetaDataExchange")                {                    
    foreach (var opDesc in svcEndPoint.Contract.Operations)                    {                        
    // Operation contract has no faults associated with them                        if (opDesc.Faults.Count == 0)                        {                            
    string msg = string.Format("MyCustomEventHandlerBehvior requires a FaultContract(typeof(MyCustomFault))" +                                " on each operation contract. The {0} contains no FaultContracts.",                               opDesc.Name);                            
    throw new InvalidOperationException(msg);                        }                       
     // Operation contract has faults                        
    var fcExists = from fc in opDesc.Faults                                       where fc.DetailType == typeof(MyCustomFault)                                       select fc;                       
     // but not of our custom fault type                        if (fcExists.Count() == 0)                        
    {                            
    string msg = "MyCustomEventHandlerBehvior requires a FaultContract(typeof(MyCustomFault)) " +                      " on each operation contract.";                        
     throw new InvalidOperationException(msg);                          }                   
     }                
    }           
     }       
     }    }

    DATACONTRACT:

     [DataContract(Namespace = "http://7Write.com/code/wcf/")]    public class MyCustomFault    
    {        
    [DataMember]        
    public string FaultDetails        
    {            
    get;            
    set;        
    }    
    }

    WEB.CONFIG:

      <extensions>
          <behaviorExtensions>
            <add
            name="ServiceErrorHandler"
            type=" WcfServiceAuthorizationSample.ServiceFaultBehaviorExtension,WcfServiceAuthorizationSample, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null"/>
          </behaviorExtensions>
        </extensions>

    I followed http://www.codeproject.com/Articles/636843/WCF-Exception-Handling .

    How to handle InValidOperationException getting from Service.

    Help me..

    Thursday, January 23, 2014 11:11 AM