Редагувати

Поділитися через


OperationBehaviorAttribute.ReleaseInstanceMode Property

Definition

Gets or sets a value that indicates when in the course of an operation invocation to recycle the service object.

public:
 property System::ServiceModel::ReleaseInstanceMode ReleaseInstanceMode { System::ServiceModel::ReleaseInstanceMode get(); void set(System::ServiceModel::ReleaseInstanceMode value); };
public System.ServiceModel.ReleaseInstanceMode ReleaseInstanceMode { get; set; }
member this.ReleaseInstanceMode : System.ServiceModel.ReleaseInstanceMode with get, set
Public Property ReleaseInstanceMode As ReleaseInstanceMode

Property Value

One of the ReleaseInstanceMode values. The default is None.

Exceptions

The value is not one of the ReleaseInstanceMode values.

Examples

The following example code shows the use of ReleaseInstanceMode to recycle service objects both before and after a call.

class SampleService : ISampleService
{
  private Guid id;
  private string session;

  public SampleService()
  {
    id = Guid.NewGuid();
    session = OperationContext.Current.SessionId;
    Console.WriteLine("Object {0} has been created.", id);
    Console.WriteLine("For session {0}", session);
  }
  [OperationBehavior(
          ReleaseInstanceMode=ReleaseInstanceMode.BeforeAndAfterCall
  )]
  public string  SampleMethod(string msg)
  {
    Console.WriteLine("The caller said: \"{0}\"", msg);
    Console.WriteLine("For session {0}", OperationContext.Current.SessionId);
    return "The service greets you: " + msg;
  }

  ~SampleService()
  {
    Console.WriteLine("Object {0} has been destroyed.", id);
    Console.WriteLine("For session {0}", session);
  }
}
Friend Class SampleService
    Implements ISampleService
  Private id As Guid
  Private session As String

  Public Sub New()
    id = Guid.NewGuid()
    session = OperationContext.Current.SessionId
    Console.WriteLine("Object {0} has been created.", id)
    Console.WriteLine("For session {0}", session)
  End Sub
  <OperationBehavior(ReleaseInstanceMode:=ReleaseInstanceMode.BeforeAndAfterCall)> _
  Public Function SampleMethod(ByVal msg As String) As String Implements ISampleService.SampleMethod
    Console.WriteLine("The caller said: ""{0}""", msg)
    Console.WriteLine("For session {0}", OperationContext.Current.SessionId)
    Return "The service greets you: " & msg
  End Function

  Protected Overrides Sub Finalize()
    Console.WriteLine("Object {0} has been destroyed.", id)
    Console.WriteLine("For session {0}", session)
  End Sub
End Class

Remarks

Use the ReleaseInstanceMode property to specify when Windows Communication Foundation (WCF) recycles a service object in the course of executing a method. The default behavior is to recycle a service object according to the InstanceContextMode value. Setting the ReleaseInstanceMode property changes that default behavior.

The ReleaseInstanceMode makes no threading guarantees. If you must have a new, unmodified object when your service runs, set the InstanceContextMode property to PerCall.

In transaction scenarios, the ReleaseInstanceMode property is often used to ensure that old data associated with the service object is cleaned up prior to processing a method call. You can also ensure that service objects associated with transactions are recycled after the transaction successfully completes by setting the ReleaseServiceInstanceOnTransactionComplete property to true.

You can choose the following behaviors:

  • Recycle a service object before an operation is called.

  • Recycle a service object after an operation is called.

  • Recycle a service object both before and after an operation is called.

  • No recycling behavior.

You can also use OperationBehaviorAttribute to configure a callback contract operation in a duplex client application. When used on a callback operation, the ReleaseInstanceMode property must be None or an InvalidOperationException exception is thrown at runtime.

In addition, it is important to realize that if the service is created by passing a service object to the ServiceHost.ServiceHost(Object, Uri[]) constructor, the value of this property is treated as if it were None.

Applies to