This documentation is archived and is not being maintained.

ServiceBehaviorAttribute Class

Specifies the internal execution behavior of a service contract implementation.

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public ref class ServiceBehaviorAttribute sealed : public Attribute, 

Apply the ServiceBehaviorAttribute attribute to a service implementation to specify service-wide execution behavior. (To specify execution behavior at the method level, use the OperationBehaviorAttribute attribute.) This attribute can be applied only to service implementations. For working examples, see the Service Behaviors Samples.

ServiceBehaviorAttribute properties are a Windows Communication Foundation (WCF) programming model feature that enables common features that developers otherwise have to implement. For more information about these and other behaviors, see Specifying Runtime Behavior. For more information about the underlying runtime properties that some of the following properties set, see Extending ServiceHost and the Dispatcher.

  • The AddressFilterMode property specifies the type of filter that the dispatcher system uses to locate the endpoint that handles requests.

  • The AutomaticSessionShutdown property automatically closes the session when the channel is closed and the service has finished processing any remaining messages.

  • The ConcurrencyMode property controls the internal threading model, enabling support for reentrant or multithreaded services.

  • The ConfigurationName property is used to declare a name for use in the name attribute of the <service> element in a configuration file.

  • The IgnoreExtensionDataObject property enables the run time to ignore extra serialization information that is not required to process the message.

  • The IncludeExceptionDetailInFaults property specifies whether unhandled exceptions in a service are returned as SOAP faults. This is for debugging purposes only.

  • The InstanceContextMode property specifies whether and when services and their service objects are to be recycled during an exchange with a client.

  • The MaxItemsInObjectGraph property to limit on the number of items in an object graph that are serialized.

  • The Name and Namespace properties control the name and namespace for the WSDL expression of the service element.

  • The ReleaseServiceInstanceOnTransactionComplete property specifies whether the service object is recycled when a transaction completes.

  • The TransactionAutoCompleteOnSessionClose property specifies whether outstanding transactions are completed when the session closes.

  • The TransactionIsolationLevel property specifies the transaction isolation level that the contract supports.

  • The TransactionTimeout property specifies the time period within which a transaction must complete or it aborts.

  • The UseSynchronizationContext property indicates whether to synchronize inbound method calls with the user interface thread automatically.

  • The ValidateMustUnderstand property informs the system whether it should confirm that SOAP headers marked as MustUnderstand have, in fact, been understood.

The IncludeExceptionDetailInFaults property can also be set using an application configuration file. For details, see IncludeExceptionDetailInFaults.

The following code example demonstrates the ServiceBehaviorAttribute properties. The BehaviorService class uses the ServiceBehaviorAttribute attribute to indicate that:

  • The service object is recycled when the transaction completes.

  • There is one service object for each session.

  • The service is single-threaded and does not support reentrant calls.

Furthermore, at the operation level, the OperationBehaviorAttribute values indicate that the TxWork method automatically enlists in flowed transactions or creates a new transaction to do the work, and that the transaction is committed automatically if an unhandled exception does not occur.

using System;
using System.ServiceModel;
using System.Transactions;

namespace Microsoft.WCF.Documentation
  public interface IBehaviorService
    string TxWork(string message);

  // Note: To use the TransactionIsolationLevel property, you  
  // must add a reference to the System.Transactions.dll assembly. 
  /* The following service implementation:
   *   -- Processes messages on one thread at a time
   *   -- Creates one service object per session
   *   -- Releases the service object when the transaction commits
  public class BehaviorService : IBehaviorService, IDisposable
    Guid myID;

    public BehaviorService()
      myID = Guid.NewGuid();
        "Object "
        + myID.ToString()
        + " created.");

     * The following operation-level behaviors are specified:
     *   -- The executing transaction is committed when
     *        the operation completes without an 
     *        unhandled exception
     *   -- Always executes under a flowed transaction.
      TransactionAutoComplete = true,
      TransactionScopeRequired = true
    public string TxWork(string message)
      // Do some transactable work.
      Console.WriteLine("TxWork called with: " + message);
      // Display transaction information.

      TransactionInformation info = Transaction.Current.TransactionInformation;
      Console.WriteLine("The distributed tx ID: {0}.", info.DistributedIdentifier);
      Console.WriteLine("The tx status: {0}.", info.Status);
      return String.Format("Hello. This was object {0}.",myID.ToString()) ;

    public void Dispose()
        "Service "
        + myID.ToString()
        + " is being recycled."

The underlying binding must support flowed transactions for the following code example to execute properly. To support flowed transactions using the WSHttpBinding, for example, set the TransactionFlow property to true in code or in an application configuration file. The following code example shows the configuration file for the preceding sample.

            <add baseAddress="http://localhost:8080/SampleService"/>
            This example code uses the WSHttpBinding to support transactions using the 
            WS-AtomicTransactions (WS-AT) protocol. WSHttpBinding is configured to use the  
            protocol, but the protocol is not enabled on some computers. Use the xws_reg -wsat+ 
            command to enable the WS-AtomicTransactions protocol in the MSDTC service.          
        <behavior name="metadataAndDebugEnabled">
    <!-- binding configuration - configures a WSHttpBinding to require transaction flow -->
        <binding name="wsHttpBindingWithTXFlow" transactionFlow="true" />
        <binding name="netTcpBindingWithTXFlow" transactionFlow="true" />


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0