Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: CSharp,ServiceChannelManager,BasicHttpBinding,ArgumentNullException,BasicHttpSecurity,ChannelFactory,EndpointAddress, Created At: 10/24/2016 8:06:53 PMViews:

HTML view:
Copy Source | Copy HTML
  1. /// <summary>
  2.     /// Менеджер канала
  3.     /// </summary>
  4.     /// <remarks>
  5.     /// Данный класс имеет фиктивную реализацию, предназначенную лишь для тестирования
  6.     /// </remarks>
  7.     public class ServiceChannelManager
  8.     {
  9.         /// <summary>
  10.         /// Конечный адресс вызова
  11.         /// </summary>
  12.         private Uri EndPointAddress;
  13.  
  14.         /// <summary>
  15.         /// Настройка транспорта
  16.         /// </summary>
  17.         BasicHttpBinding wb = new BasicHttpBinding
  18.         {
  19.             MaxReceivedMessageSize = 204003200,
  20.             MaxBufferPoolSize = 524288,
  21.             MaxBufferSize = 204003200,
  22.             ReaderQuotas =
  23.             {
  24.                 MaxDepth = 2147483647,
  25.                 MaxStringContentLength = 2147483647,
  26.                 MaxArrayLength = 2147483647
  27.             },
  28.             Security =
  29.             {
  30.                 Mode = BasicHttpSecurityMode.None,
  31.                 Transport =
  32.                 {
  33.                     ClientCredentialType = HttpClientCredentialType.None
  34.                 }
  35.             }
  36.         };
  37.  
  38.         public ServiceChannelManager(Uri linktoservice)
  39.         {
  40.             if(linktoservice==null)
  41.             {
  42.                 throw new ArgumentNullException("linktoservice");
  43.             }
  44.             EndPointAddress = linktoservice;
  45.         }
  46.  
  47.         /// <summary>
  48.         /// Осуществить удаленный вызов
  49.         /// </summary>
  50.         /// <typeparam name="TService">Контракт сервиса</typeparam>
  51.         /// <param name="operation">Делегат вызова</param>
  52.         public void Invoke<TService>(Action<TService> operation)
  53.         {
  54.             if (EndPointAddress.Scheme == "https")
  55.             {
  56.                 BasicHttpSecurity bhs = new BasicHttpSecurity
  57.                 {
  58.                     Mode = BasicHttpSecurityMode.Transport,
  59.                     Transport =
  60.                     {
  61.                         ClientCredentialType = HttpClientCredentialType.Certificate
  62.                     },
  63.                     Message =
  64.                     {
  65.                         ClientCredentialType =
  66.                             BasicHttpMessageCredentialType.Certificate,
  67.                         AlgorithmSuite = SecurityAlgorithmSuite.Default
  68.                     }
  69.                 };
  70.                 wb.Security = bhs;
  71.             }
  72.             // Создание и настройка кнала...
  73.             using (ChannelFactory<TService> channel = new ChannelFactory<TService>(wb, new EndpointAddress(EndPointAddress)))
  74.             {
  75.                 if (EndPointAddress.Scheme == "https")
  76.                 {
  77.                     channel.Credentials.ClientCertificate.Certificate = null;
  78.                 }
  79.                 // Вызов удаленной операции
  80.                 operation(channel.CreateChannel());
  81.             }
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Осуществить удаленный вызов
  86.         /// </summary>
  87.         /// <typeparam name="TService">Контракт сервиса</typeparam>
  88.         /// <typeparam name="TResult">Результат вызова</typeparam>
  89.         /// <param name="operation">Делегат вызова</param>
  90.         public TResult Invoke<TService, TResult>(Func<TService, TResult> operation)
  91.         {
  92.             if (EndPointAddress.Scheme == "https")
  93.             {
  94.                 BasicHttpSecurity bhs = new BasicHttpSecurity
  95.                 {
  96.                     Mode = BasicHttpSecurityMode.Transport,
  97.                     Transport =
  98.                         {
  99.                             ClientCredentialType = HttpClientCredentialType.Certificate
  100.                         },
  101.                     Message =
  102.                         {
  103.                             ClientCredentialType =
  104.                                 BasicHttpMessageCredentialType.Certificate,
  105.                             AlgorithmSuite = SecurityAlgorithmSuite.Default
  106.                         }
  107.                 };
  108.                 wb.Security = bhs;
  109.             }
  110.             // Создание и настройка кнала...
  111.             using (ChannelFactory<TService> channel = new ChannelFactory<TService>(wb, new EndpointAddress(EndPointAddress)))
  112.             {
  113.                 if (EndPointAddress.Scheme == "https")
  114.                 {
  115.                     channel.Credentials.ClientCertificate.Certificate = null;
  116.                 }
  117.  
  118.                 // Вызов удаленной операции
  119.                 return operation(channel.CreateChannel());
  120.             }
  121.         }
  122.     }

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2018