OperationBehaviorAttribute Kelas
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan perilaku eksekusi lokal metode layanan.
public ref class OperationBehaviorAttribute sealed : Attribute, System::ServiceModel::Description::IOperationBehavior
[System.AttributeUsage(System.AttributeTargets.Method)]
public sealed class OperationBehaviorAttribute : Attribute, System.ServiceModel.Description.IOperationBehavior
[<System.AttributeUsage(System.AttributeTargets.Method)>]
type OperationBehaviorAttribute = class
inherit Attribute
interface IOperationBehavior
Public NotInheritable Class OperationBehaviorAttribute
Inherits Attribute
Implements IOperationBehavior
- Warisan
- Atribut
- Penerapan
Contoh
Contoh kode berikut menunjukkan operasi yang dijalankan dalam transaksi terdistribusi wajib. Properti TransactionScopeRequired menunjukkan bahwa metode dijalankan di bawah transaksi pemanggil dan TransactionAutoComplete properti menunjukkan bahwa jika tidak ada pengecualian yang tidak tertangani, transaksi dilakukan secara otomatis. Jika pengecualian yang tidak tertangani terjadi, transaksi dibatalkan.
using System;
using System.ServiceModel;
using System.Transactions;
namespace Microsoft.WCF.Documentation
{
[ServiceContract(Namespace="http://microsoft.wcf.documentation", SessionMode=SessionMode.Required)]
public interface IBehaviorService
{
[OperationContract]
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
*/
[ServiceBehavior(
ConcurrencyMode=ConcurrencyMode.Single,
InstanceContextMode=InstanceContextMode.PerSession,
ReleaseServiceInstanceOnTransactionComplete=true
)]
public class BehaviorService : IBehaviorService, IDisposable
{
Guid myID;
public BehaviorService()
{
myID = Guid.NewGuid();
Console.WriteLine(
"Object "
+ myID.ToString()
+ " created.");
}
/*
* The following operation-level behaviors are specified:
* -- Always executes under a transaction scope.
* -- The transaction scope is completed when the operation terminates
* without an unhandled exception.
*/
[OperationBehavior(
TransactionAutoComplete = true,
TransactionScopeRequired = true
)]
[TransactionFlow(TransactionFlowOption.Mandatory)]
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()
{
Console.WriteLine(
"Service "
+ myID.ToString()
+ " is being recycled."
);
}
}
}
Imports System.ServiceModel
Imports System.Transactions
Namespace Microsoft.WCF.Documentation
<ServiceContract(Namespace:="http://microsoft.wcf.documentation", SessionMode:=SessionMode.Required)> _
Public Interface IBehaviorService
<OperationContract> _
Function TxWork(ByVal message As String) As String
End Interface
' 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
<ServiceBehavior(ConcurrencyMode:=ConcurrencyMode.Single, InstanceContextMode:=InstanceContextMode.PerSession, _
ReleaseServiceInstanceOnTransactionComplete:=True)> _
Public Class BehaviorService
Implements IBehaviorService, IDisposable
Private myID As Guid
Public Sub New()
myID = Guid.NewGuid()
Console.WriteLine("Object " & myID.ToString() & " created.")
End Sub
'
' * The following operation-level behaviors are specified:
' * -- Always executes under a transaction scope.
' * -- The transaction scope is completed when the operation terminates
' * without an unhandled exception.
'
<OperationBehavior(TransactionAutoComplete:=True, TransactionScopeRequired:=True), _
TransactionFlow(TransactionFlowOption.Mandatory)> _
Public Function TxWork(ByVal message As String) As String Implements IBehaviorService.TxWork
' Do some transactable work.
Console.WriteLine("TxWork called with: " & message)
' Display transaction information.
Dim info As TransactionInformation = 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())
End Function
Public Sub Dispose() Implements IDisposable.Dispose
Console.WriteLine("Service " & myID.ToString() & " is being recycled.")
End Sub
End Class
End Namespace
Keterangan
OperationBehaviorAttribute Gunakan atribut untuk menunjukkan perilaku eksekusi khusus operasi apa yang dimiliki operasi saat dijalankan. (Untuk menentukan perilaku eksekusi di tingkat layanan, gunakan ServiceBehaviorAttribute atribut .)
Nota
Anda juga dapat menggunakan OperationBehaviorAttribute untuk mengonfigurasi operasi kontrak panggilan balik dalam aplikasi klien dupleks. Saat digunakan pada operasi panggilan balik, ReleaseInstanceMode properti harus None atau InvalidOperationException pengecualian dilemparkan pada runtime.
Atributnya OperationBehaviorAttribute adalah fitur model pemrograman Windows Communication Foundation (WCF) yang memungkinkan fitur umum yang harus diterapkan oleh pengembang sendiri.
Properti AutoDisposeParameters mengontrol apakah objek parameter yang diteruskan ke operasi dibuang ketika operasi selesai.
Properti TransactionAutoComplete menentukan apakah transaksi tempat metode dijalankan secara otomatis dilakukan jika tidak ada pengecualian yang tidak tertangani yang terjadi.
Properti TransactionScopeRequired menentukan apakah metode harus dijalankan dalam transaksi.
Properti Impersonation menentukan apakah operasi layanan dapat, harus, atau tidak dapat meniru identitas pemanggil.
Properti ReleaseInstanceMode menentukan kapan objek layanan didaur ulang selama proses pemanggilan metode.
Konstruktor
| Nama | Deskripsi |
|---|---|
| OperationBehaviorAttribute() |
Menginisialisasi instans baru dari kelas OperationBehaviorAttribute. |
Properti
| Nama | Deskripsi |
|---|---|
| AutoDisposeParameters |
Mendapatkan atau menetapkan nilai yang menunjukkan apakah parameter akan dibuang secara otomatis. |
| Impersonation |
Mendapatkan atau menetapkan nilai yang menunjukkan tingkat peniruan pemanggil yang didukung operasi. |
| ReleaseInstanceMode |
Mendapatkan atau menetapkan nilai yang menunjukkan kapan dalam pemanggilan operasi untuk mendaur ulang objek layanan. |
| TransactionAutoComplete |
Mendapatkan atau menetapkan nilai yang menunjukkan apakah akan secara otomatis menyelesaikan cakupan transaksi saat ini jika tidak ada pengecualian yang tidak tertangani yang terjadi. |
| TransactionScopeRequired |
Mendapatkan atau menetapkan nilai yang menunjukkan apakah metode memerlukan cakupan transaksi untuk eksekusinya. |
| TypeId |
Ketika diimplementasikan dalam kelas turunan, mendapatkan pengidentifikasi unik untuk Attributeini. (Diperoleh dari Attribute) |
Metode
| Nama | Deskripsi |
|---|---|
| Equals(Object) |
Mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| GetHashCode() |
Mengembalikan kode hash untuk instans ini. (Diperoleh dari Attribute) |
| GetType() |
Mendapatkan Type instans saat ini. (Diperoleh dari Object) |
| IsDefaultAttribute() |
Ketika ditimpa dalam kelas turunan, menunjukkan apakah nilai instans ini adalah nilai default untuk kelas turunan. (Diperoleh dari Attribute) |
| Match(Object) |
Saat ditimpa dalam kelas turunan, mengembalikan nilai yang menunjukkan apakah instans ini sama dengan objek tertentu. (Diperoleh dari Attribute) |
| MemberwiseClone() |
Membuat salinan dangkal dari Objectsaat ini. (Diperoleh dari Object) |
| ToString() |
Mengembalikan string yang mewakili objek saat ini. (Diperoleh dari Object) |