Guest User

Untitled

a guest
Dec 5th, 2017
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. using (var proxy = new somenamespace.UserOrgHierarchyServiceInterfaceClient(binding, new EndpointAddress(host + "somewsdl.blah")))
  2. {
  3. proxy.ClientCredentials.UserName.UserName = username;
  4. proxy.ClientCredentials.UserName.Password = password;
  5. var getDevicesParametersType = new somenamespace.organizationQueryParametersType();
  6. int itemsCount = RequestWithCustomHeader(a => Convert.ToInt32(
  7. proxy.getOrganizationCount(
  8. new somenamespace.getOrganizationCountRequestType
  9. {
  10. queryParams = getDevicesParametersType
  11. }).responseData?.count), proxy);
  12. if (itemsCount == 0)
  13. Console.WriteLine("No organization");
  14. else
  15. {
  16. Console.WriteLine("Items count: {0}", itemsCount);
  17. var totalTasks = (itemsCount - 1) / size + 1;
  18. var progress = new TimerProgress(totalTasks, itemsCount);
  19. using (var tokenSource = new CancellationTokenSource())
  20. {
  21. var cancellationToken = tokenSource.Token;
  22. try
  23. {
  24. CreateTimerTask(progress, cancellationToken);
  25. var organizationParentsList = new List<OrganizationParents>();
  26. var errorAction = CreateErrorAction(targetPath);
  27. var tasks = Enumerable.Range(1, totalTasks)
  28. .Select(page =>
  29. Task.Factory.StartNew(() =>
  30. {
  31. progress.initiatedTask++;
  32.  
  33. var result = Retry(() => RequestWithCustomHeader(b => proxy.getOrganizationsQuery(
  34. new somenamespace.getOrganizationsQueryRequestType
  35. {
  36. batchSize = size.ToString(),
  37. pageNumber = page.ToString(),
  38. queryParams = getDevicesParametersType,
  39. }), proxy), RETRY_LIMIT);
  40. ...
  41. process with "result"
  42. ...
  43. })
  44. .ContinueWith(errorAction, page, TaskContinuationOptions.OnlyOnFaulted)
  45. .ContinueWith(t => progress.completedTask++)
  46. ).ToArray();
  47.  
  48. Console.WriteLine("Tasks: {0}", tasks.Length);
  49. Task.WaitAll(tasks);
  50. tokenSource.Cancel();
  51. }
  52. catch (AggregateException ex)
  53. {
  54.  
  55. }
  56. catch
  57. {
  58. throw;
  59. }
  60. }
  61.  
  62. }
  63.  
  64.  
  65. Console.WriteLine("Press any key to continue");
  66. }
  67.  
  68. /* definition for RequestWithHeader() */
  69. private static T RequestWithCustomHeader<T, S>(Func<ClientBase<S>, T> func, ClientBase<S> proxy) where S : class
  70. {
  71. using (new OperationContextScope(proxy.InnerChannel))
  72. {
  73. var httpRequestProperty = new HttpRequestMessageProperty();
  74. UserNamePasswordClientCredential userName = proxy.ClientCredentials.UserName;
  75. httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(userName.UserName + ":" + userName.Password));
  76. OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
  77. return func(proxy);
  78. }
  79. }
Add Comment
Please, Sign In to add comment