OperationContextScope Constructors
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Initializes a new instance of the OperationContextScope class.
OperationContextScope(IContextChannel) |
Initializes a new instance of the OperationContextScope class that uses the specified IContextChannel to create a new OperationContext for the scope. |
OperationContextScope(OperationContext) |
Initializes a new instance of the OperationContextScope class to create a scope for the specified OperationContext object. |
- Source:
- OperationContextScope.cs
- Source:
- OperationContextScope.cs
Initializes a new instance of the OperationContextScope class that uses the specified IContextChannel to create a new OperationContext for the scope.
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)
Parameters
- channel
- IContextChannel
The channel to use when creating the scope for a new OperationContext.
Examples
The following example shows how to use the OperationContextScope to create a new context in a client application in order to add a custom header to the outgoing message.
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
Remarks
Use the OperationContextScope constructor to create a new OperationContext for a client channel that can be used to add or modify outbound message headers, read incoming message headers, or access other run-time properties of the OperationContext.
The headers added to the OperationContext.IncomingMessageHeaders property of the newly created OperationContext apply only to the channel that was passed to the OperationContextScope constructor. If the user creates a new channel within its scope then the headers are not applied to messages sent on the new channel.
Applies to
.NET 8 (package-provided) e altre versioni
Prodotto | Versioni |
---|---|
.NET | Core 1.0, Core 1.1, 8 (package-provided) |
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |
UWP | 10.0 |
- Source:
- OperationContextScope.cs
- Source:
- OperationContextScope.cs
Initializes a new instance of the OperationContextScope class to create a scope for the specified OperationContext object.
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)
Parameters
- context
- OperationContext
The active OperationContext in the created scope.
Remarks
Use the OperationContextScope constructor to create a code block within which the specified OperationContextScope object is the current scope.
Applies to
.NET 8 (package-provided) e altre versioni
Prodotto | Versioni |
---|---|
.NET | Core 1.0, Core 1.1, 8 (package-provided) |
.NET Framework | 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1 |
.NET Standard | 2.0 (package-provided) |
UWP | 10.0 |
Feedback su .NET
.NET è un progetto di open source. Selezionare un collegamento per fornire feedback: