【问题标题】:Consume external WCF service using channel factory and not by using proxy or adding service reference使用通道工厂而不是使用代理或添加服务引用来使用外部 WCF 服务
【发布时间】:2019-04-26 01:35:52
【问题描述】:

我想知道是否有可能使用通道工厂来使用外部 wcf 服务(外部 wcf 服务是指不属于我的解决方案的服务)。我知道我们可以通过生成代理或添加服务引用来消费,但我想知道我们是否可以使用通道工厂。由于它是一个外部服务,我们不会使用接口类,所以需要知道通道工厂实例的外观如何?

【问题讨论】:

    标签: wcf wcf-data-services wcf-binding


    【解决方案1】:

    您需要通过查看 WSDL 文件(服务上的元数据文件)来模仿服务的接口

    然后你可以使用一些辅助方法来初始化你的服务,

    public static TChannel GetBasicHttpService<TChannel>(string serviceEndpoint) where TChannel : class
        {
            EndpointAddress myEndpoint = new EndpointAddress(serviceEndpoint);
            ChannelFactory<TChannel> myChannelFactory = new ChannelFactory<TChannel>(DefaultHttpBinding(), myEndpoint);
    
            // Create a channel.
            return myChannelFactory.CreateChannel();
        }
    
        public static BasicHttpBinding DefaultHttpBinding()
        {
            BasicHttpBinding defaultBinding = new BasicHttpBinding();
            defaultBinding.MaxReceivedMessageSize = 2147483647;
            defaultBinding.MaxBufferPoolSize = 2147483647;
            defaultBinding.MaxBufferSize = 2147483647;
            defaultBinding.ReaderQuotas.MaxArrayLength = 2147483647;
            defaultBinding.ReaderQuotas.MaxStringContentLength = 2147483647;
    
            return defaultBinding;
        }
    

    其中 TChannel 是 Mimicked 接口

    【讨论】:

      【解决方案2】:

      您应该知道服务契约接口和端点的格式,否则我们无法创建通道工厂。之所以使用通道工厂调用服务,是为了保护WCF服务,服务器端禁用发布服务元数据。我做了一个简单的demo,希望对你有用。
      服务器端。

      class Program
      {
      static void Main(string[] args)
      {
      Uri uri = new Uri("http://localhost:1900");
      BasicHttpBinding binding = new BasicHttpBinding();
      using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
      {
      sh.AddServiceEndpoint(typeof(IService), binding, "");
      sh.Open();
      Console.WriteLine("Service is ready...");
      
      Console.ReadLine();
      sh.Close();
      }
      
      }
      }
      [ServiceContract(Namespace ="mydomain")]
      public interface IService
      {
      [OperationContract(Name ="AddInt")]
      int Add1(int x, int y);
      
      }
      public class MyService : IService
      {
      public int Add(int x, int y)
      {
      return x + y;
      }
      }
      

      客户端。

      class Program
      {
      static void Main(string[] args)
      {
      Uri uri = new Uri("http://localhost:1900");
      BasicHttpBinding binding = new BasicHttpBinding();
      using (ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, new EndpointAddress(uri)))
      {
      IService sv = factory.CreateChannel();
      var result = sv.Add(34, 3);
      try
      {
      Console.WriteLine(result);
      }
      catch (Exception ex)
      {
      throw;
      }
      }
      }
      }
      [ServiceContract(Namespace = "mydomain")]
      public interface IService
      {
      [OperationContract(Name = "AddInt")]
      int Add2(int x, int y);
      }
      

      不需要保证客户端和服务端有相同的服务接口,但至少需要保证客户端和服务端接口的namespace和name属性是一致的。 如果有什么我可以帮忙的,请随时告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-27
        • 1970-01-01
        • 2012-11-14
        相关资源
        最近更新 更多