OperationContextScope Konstruktorer

Definition

Initierar en ny instans av OperationContextScope klassen.

Överlagringar

Name Description
OperationContextScope(IContextChannel)

Initierar en ny instans av OperationContextScope klassen som använder den angivna IContextChannel för att skapa en ny OperationContext för omfånget.

OperationContextScope(OperationContext)

Initierar en ny instans av OperationContextScope klassen för att skapa ett omfång för det angivna OperationContext objektet.

OperationContextScope(IContextChannel)

Initierar en ny instans av OperationContextScope klassen som använder den angivna IContextChannel för att skapa en ny OperationContext för omfånget.

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)

Parametrar

channel
IContextChannel

Den kanal som ska användas när du skapar omfånget för en ny OperationContext.

Exempel

I följande exempel visas hur du använder OperationContextScope för att skapa en ny kontext i ett klientprogram för att lägga till en anpassad rubrik i det utgående meddelandet.

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

Kommentarer

OperationContextScope Använd konstruktorn för att skapa en ny OperationContext för en klientkanal som kan användas för att lägga till eller ändra utgående meddelandehuvuden, läsa inkommande meddelandehuvuden eller få åtkomst till andra körningsegenskaper för OperationContext.

Rubrikerna som läggs till i OperationContext.IncomingMessageHeaders egenskapen för den nyligen skapade OperationContext gäller endast för den kanal som skickades till OperationContextScope konstruktorn. Om användaren skapar en ny kanal inom dess omfång tillämpas inte rubrikerna på meddelanden som skickas på den nya kanalen.

Gäller för

OperationContextScope(OperationContext)

Initierar en ny instans av OperationContextScope klassen för att skapa ett omfång för det angivna OperationContext objektet.

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)

Parametrar

context
OperationContext

Den aktiva OperationContext i det skapade omfånget.

Kommentarer

OperationContextScope Använd konstruktorn för att skapa ett kodblock där det angivna OperationContextScope objektet är det aktuella omfånget.

Gäller för