ChannelFactory<TChannel> 类



一个创建不同类型通道的工厂,客户端使用这些通道将消息发送到不同配置的服务终结点。


示例


下面的示例演示如何创建通道工厂并用它来创建和管理通道。

C#
	       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();


下面的代码示例演示如何在工厂创建通道对象前,以编程方式插入客户端行为。

C#
VB
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();
    }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值