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


OperationContextScope Конструкторы

Определение

Инициализирует новый экземпляр класса OperationContextScope.

Перегрузки

Имя Описание
OperationContextScope(IContextChannel)

Инициализирует новый экземпляр OperationContextScope класса, который использует указанный IContextChannel для создания нового OperationContext для области.

OperationContextScope(OperationContext)

Инициализирует новый экземпляр OperationContextScope класса, чтобы создать область для указанного OperationContext объекта.

OperationContextScope(IContextChannel)

Исходный код:
OperationContextScope.cs
Исходный код:
OperationContextScope.cs
Исходный код:
OperationContextScope.cs

Инициализирует новый экземпляр OperationContextScope класса, который использует указанный IContextChannel для создания нового OperationContext для области.

public:
 OperationContextScope(System::ServiceModel::IContextChannel ^ channel);
public OperationContextScope(System.ServiceModel.IContextChannel channel);
new System.ServiceModel.OperationContextScope : System.ServiceModel.IContextChannel -> System.ServiceModel.OperationContextScope
Public Sub New (channel As IContextChannel)

Параметры

channel
IContextChannel

Канал, используемый при создании области для нового OperationContext.

Примеры

В следующем примере показано, как создать OperationContextScope новый контекст в клиентском приложении, чтобы добавить пользовательский заголовок в исходящее сообщение.

SampleServiceClient wcfClient = new SampleServiceClient(new InstanceContext(this));
try
{
  using (OperationContextScope scope = new OperationContextScope(wcfClient.InnerChannel))
  {
    MessageHeader header
      = MessageHeader.CreateHeader(
      "Service-Bound-CustomHeader",
      "http://Microsoft.WCF.Documentation",
      "Custom Happy Value."
      );
    OperationContext.Current.OutgoingMessageHeaders.Add(header);

    // Making calls.
    Console.WriteLine("Enter the greeting to send: ");
    string greeting = Console.ReadLine();

    //Console.ReadLine();
    header = MessageHeader.CreateHeader(
        "Service-Bound-OneWayHeader",
        "http://Microsoft.WCF.Documentation",
        "Different Happy Value."
      );
    OperationContext.Current.OutgoingMessageHeaders.Add(header);

    // One-way
    wcfClient.Push(greeting);
    this.wait.WaitOne();

    // Done with service.
    wcfClient.Close();
    Console.WriteLine("Done!");
    Console.ReadLine();
  }
}
catch (TimeoutException timeProblem)
{
  Console.WriteLine("The service operation timed out. " + timeProblem.Message);
  Console.ReadLine();
  wcfClient.Abort();
}
catch (CommunicationException commProblem)
{
  Console.WriteLine("There was a communication problem. " + commProblem.Message);
  Console.ReadLine();
  wcfClient.Abort();
}
Dim wcfClient As New SampleServiceClient(New InstanceContext(Me))
Try
  Using scope As New OperationContextScope(wcfClient.InnerChannel)
            Dim header As MessageHeader = MessageHeader.CreateHeader("Service-Bound-CustomHeader", _
                                "http://Microsoft.WCF.Documentation", "Custom Happy Value.")
    OperationContext.Current.OutgoingMessageHeaders.Add(header)

    ' Making calls.
    Console.WriteLine("Enter the greeting to send: ")
    Dim greeting As String = Console.ReadLine()

    'Console.ReadLine();
            header = MessageHeader.CreateHeader("Service-Bound-OneWayHeader", _
                                                "http://Microsoft.WCF.Documentation", "Different Happy Value.")
    OperationContext.Current.OutgoingMessageHeaders.Add(header)

    ' One-way
    wcfClient.Push(greeting)
    Me.wait.WaitOne()

    ' Done with service. 
    wcfClient.Close()
    Console.WriteLine("Done!")
    Console.ReadLine()
  End Using
Catch timeProblem As TimeoutException
  Console.WriteLine("The service operation timed out. " & timeProblem.Message)
  Console.ReadLine()
  wcfClient.Abort()
Catch commProblem As CommunicationException
  Console.WriteLine("There was a communication problem. " & commProblem.Message)
  Console.ReadLine()
  wcfClient.Abort()
End Try

Комментарии

OperationContextScope Используйте конструктор, чтобы создать новый OperationContext для клиентского канала, который можно использовать для добавления или изменения заголовков исходящих сообщений, чтения заголовков входящих сообщений или доступа к другим свойствам OperationContextвремени выполнения.

Заголовки, добавленные в OperationContext.IncomingMessageHeaders свойство только что созданного OperationContext , применяются только к каналу, переданному конструктору OperationContextScope . Если пользователь создает новый канал в пределах своей области, заголовки не применяются к сообщениям, отправленным на новый канал.

Применяется к

OperationContextScope(OperationContext)

Исходный код:
OperationContextScope.cs
Исходный код:
OperationContextScope.cs
Исходный код:
OperationContextScope.cs

Инициализирует новый экземпляр OperationContextScope класса, чтобы создать область для указанного OperationContext объекта.

public:
 OperationContextScope(System::ServiceModel::OperationContext ^ context);
public OperationContextScope(System.ServiceModel.OperationContext context);
new System.ServiceModel.OperationContextScope : System.ServiceModel.OperationContext -> System.ServiceModel.OperationContextScope
Public Sub New (context As OperationContext)

Параметры

context
OperationContext

Активен OperationContext в созданной области.

Комментарии

OperationContextScope Используйте конструктор для создания блока кода, в котором указанный OperationContextScope объект является текущей областью.

Применяется к