ChannelFactory<TChannel> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
一个创建不同类型通道的工厂,客户端使用这些通道将消息发送到不同配置的服务终结点。
generic <typename TChannel>
public ref class ChannelFactory : System::ServiceModel::ChannelFactory, System::ServiceModel::Channels::IChannelFactory<TChannel>
public class ChannelFactory<TChannel> : System.ServiceModel.ChannelFactory, System.ServiceModel.Channels.IChannelFactory<TChannel>
type ChannelFactory<'Channel> = class
inherit ChannelFactory
interface IChannelFactory<'Channel>
interface IChannelFactory
interface ICommunicationObject
type ChannelFactory<'Channel> = class
inherit ChannelFactory
interface IChannelFactory
interface ICommunicationObject
interface IChannelFactory<'Channel>
Public Class ChannelFactory(Of TChannel)
Inherits ChannelFactory
Implements IChannelFactory(Of TChannel)
类型参数
- TChannel
由通道工厂生成的通道类型。 此类型必须为 IOutputChannel 或 IRequestChannel。
- 继承
- 派生
- 实现
示例
下面的示例演示如何创建通道工厂并用它来创建和管理通道。
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8000/ChannelApp");
ChannelFactory<IRequestChannel> factory =
new ChannelFactory<IRequestChannel>(binding, address);
IRequestChannel channel = factory.CreateChannel();
channel.Open();
Message request = Message.CreateMessage(MessageVersion.Soap11, "hello");
Message reply = channel.Request(request);
Console.Out.WriteLine(reply.Headers.Action);
reply.Close();
channel.Close();
factory.Close();
}
下面的代码示例演示如何在工厂创建通道对象前,以编程方式插入客户端行为。
public class Client
{
public static void Main()
{
try
{
// Picks up configuration from the config file.
ChannelFactory<ISampleServiceChannel> factory
= new ChannelFactory<ISampleServiceChannel>("WSHttpBinding_ISampleService");
// Add the client side behavior programmatically to all created channels.
factory.Endpoint.Behaviors.Add(new EndpointBehaviorMessageInspector());
ISampleServiceChannel wcfClientChannel = factory.CreateChannel();
// Making calls.
Console.WriteLine("Enter the greeting to send: ");
string greeting = Console.ReadLine();
Console.WriteLine("The service responded: " + wcfClientChannel.SampleMethod(greeting));
Console.WriteLine("Press ENTER to exit:");
Console.ReadLine();
// Done with service.
wcfClientChannel.Close();
Console.WriteLine("Done!");
}
catch (TimeoutException timeProblem)
{
Console.WriteLine("The service operation timed out. " + timeProblem.Message);
Console.Read();
}
catch (FaultException<SampleFault> fault)
{
Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage);
Console.Read();
}
catch (CommunicationException commProblem)
{
Console.WriteLine("There was a communication problem. " + commProblem.Message);
Console.Read();
}
}
Public Class Client
Public Shared Sub Main()
Try
' Picks up configuration from the config file.
Dim factory As New ChannelFactory(Of ISampleServiceChannel)("WSHttpBinding_ISampleService")
' Add the client side behavior programmatically to all created channels.
factory.Endpoint.Behaviors.Add(New EndpointBehaviorMessageInspector())
Dim wcfClientChannel As ISampleServiceChannel = factory.CreateChannel()
' Making calls.
Console.WriteLine("Enter the greeting to send: ")
Dim greeting As String = Console.ReadLine()
Console.WriteLine("The service responded: " & wcfClientChannel.SampleMethod(greeting))
Console.WriteLine("Press ENTER to exit:")
Console.ReadLine()
' Done with service.
wcfClientChannel.Close()
Console.WriteLine("Done!")
Catch timeProblem As TimeoutException
Console.WriteLine("The service operation timed out. " & timeProblem.Message)
Console.Read()
Catch fault As FaultException(Of SampleFault)
Console.WriteLine("SampleFault fault occurred: {0}", fault.Detail.FaultMessage)
Console.Read()
Catch commProblem As CommunicationException
Console.WriteLine("There was a communication problem. " & commProblem.Message)
Console.Read()
End Try
End Sub
注解
使用此泛型类可实现一些更高级的方案,在这些方案中有创建通道工厂(可用于创建多个通道类型)的要求。
以编程方式添加行为时,可以在创建任何通道之前将行为添加到 Behaviors
上相应的 ChannelFactory 属性。 有关代码示例,请参见“示例”部分。
频繁使用此类型的客户端或调用应用程序(例如,中层应用程序也是客户端应用程序)也具有复杂的状态管理需求和性能需求。 有关这些方案的详细信息,请参阅 中间层客户端应用程序。
构造函数
ChannelFactory<TChannel>() |
初始化 ChannelFactory<TChannel> 类的新实例。 |
ChannelFactory<TChannel>(Binding) |
初始化 ChannelFactory<TChannel> 类的新实例。 |
ChannelFactory<TChannel>(Binding, EndpointAddress) |
使用指定的绑定和终结点地址初始化 ChannelFactory<TChannel> 类的新实例。 |
ChannelFactory<TChannel>(Binding, String) |
使用指定的绑定和远程地址初始化 ChannelFactory<TChannel> 类的新实例。 |
ChannelFactory<TChannel>(ServiceEndpoint) |
使用指定的终结点初始化生成通道的 ChannelFactory<TChannel> 类的新实例。 |
ChannelFactory<TChannel>(String) |
使用指定的终结点配置名称初始化 ChannelFactory<TChannel> 类的新实例。 |
ChannelFactory<TChannel>(String, EndpointAddress) |
初始化 ChannelFactory<TChannel> 类的新实例,该类与终结点配置和远程地址的指定名称相关联。 |
ChannelFactory<TChannel>(Type) |
初始化 ChannelFactory<TChannel> 类的新实例。 |
属性
Credentials |
获取客户端在通过由工厂产生的通道与服务终结点进行通信时使用的凭据。 (继承自 ChannelFactory) |
DefaultCloseTimeout |
获取为完成关闭操作提供的默认时间间隔。 (继承自 ChannelFactory) |
DefaultOpenTimeout |
获取为完成打开操作提供的默认时间间隔。 (继承自 ChannelFactory) |
Endpoint |
获取由工厂产生的通道所要连接的服务终结点。 (继承自 ChannelFactory) |
IsDisposed |
获取一个值,该值指示通信对象是否已被释放。 (继承自 CommunicationObject) |
State |
获取指示通信对象当前状态的值。 (继承自 CommunicationObject) |
ThisLock |
获取在状态转换过程中保护类实例的相互排斥锁。 (继承自 CommunicationObject) |
方法
事件
Closed |
当通信对象转换到已关闭状态时发生。 (继承自 CommunicationObject) |
Closing |
当通信对象转换到正在关闭状态时发生。 (继承自 CommunicationObject) |
Faulted |
在通信对象转换到出错状态时发生。 (继承自 CommunicationObject) |
Opened |
当通信对象转换到已打开状态时发生。 (继承自 CommunicationObject) |
Opening |
当通信对象转换到正在打开状态时发生。 (继承自 CommunicationObject) |
显式接口实现
IAsyncCommunicationObject.CloseAsync(TimeSpan) |
一个创建不同类型通道的工厂,客户端使用这些通道将消息发送到不同配置的服务终结点。 (继承自 CommunicationObject) |
IAsyncCommunicationObject.OpenAsync(TimeSpan) |
一个创建不同类型通道的工厂,客户端使用这些通道将消息发送到不同配置的服务终结点。 (继承自 CommunicationObject) |
IAsyncDisposable.DisposeAsync() |
一个创建不同类型通道的工厂,客户端使用这些通道将消息发送到不同配置的服务终结点。 (继承自 ChannelFactory) |
IDisposable.Dispose() |
关闭当前的通道工厂。 (继承自 ChannelFactory) |
扩展方法
CloseHelperAsync(ICommunicationObject, TimeSpan) |
一个创建不同类型通道的工厂,客户端使用这些通道将消息发送到不同配置的服务终结点。 |
OpenHelperAsync(ICommunicationObject, TimeSpan) |
一个创建不同类型通道的工厂,客户端使用这些通道将消息发送到不同配置的服务终结点。 |
GetInternalCloseTimeout(CommunicationObject) |
一个创建不同类型通道的工厂,客户端使用这些通道将消息发送到不同配置的服务终结点。 |
ConfigureAwait(IAsyncDisposable, Boolean) |
配置如何执行从异步可处置项返回的任务的等待。 |
适用于
线程安全性
此类型是线程安全的。
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈