Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using (var proxy = new somenamespace.UserOrgHierarchyServiceInterfaceClient(binding, new EndpointAddress(host + "somewsdl.blah")))
- {
- proxy.ClientCredentials.UserName.UserName = username;
- proxy.ClientCredentials.UserName.Password = password;
- var getDevicesParametersType = new somenamespace.organizationQueryParametersType();
- int itemsCount = RequestWithCustomHeader(a => Convert.ToInt32(
- proxy.getOrganizationCount(
- new somenamespace.getOrganizationCountRequestType
- {
- queryParams = getDevicesParametersType
- }).responseData?.count), proxy);
- if (itemsCount == 0)
- Console.WriteLine("No organization");
- else
- {
- Console.WriteLine("Items count: {0}", itemsCount);
- var totalTasks = (itemsCount - 1) / size + 1;
- var progress = new TimerProgress(totalTasks, itemsCount);
- using (var tokenSource = new CancellationTokenSource())
- {
- var cancellationToken = tokenSource.Token;
- try
- {
- CreateTimerTask(progress, cancellationToken);
- var organizationParentsList = new List<OrganizationParents>();
- var errorAction = CreateErrorAction(targetPath);
- var tasks = Enumerable.Range(1, totalTasks)
- .Select(page =>
- Task.Factory.StartNew(() =>
- {
- progress.initiatedTask++;
- var result = Retry(() => RequestWithCustomHeader(b => proxy.getOrganizationsQuery(
- new somenamespace.getOrganizationsQueryRequestType
- {
- batchSize = size.ToString(),
- pageNumber = page.ToString(),
- queryParams = getDevicesParametersType,
- }), proxy), RETRY_LIMIT);
- ...
- process with "result"
- ...
- })
- .ContinueWith(errorAction, page, TaskContinuationOptions.OnlyOnFaulted)
- .ContinueWith(t => progress.completedTask++)
- ).ToArray();
- Console.WriteLine("Tasks: {0}", tasks.Length);
- Task.WaitAll(tasks);
- tokenSource.Cancel();
- }
- catch (AggregateException ex)
- {
- }
- catch
- {
- throw;
- }
- }
- }
- Console.WriteLine("Press any key to continue");
- }
- /* definition for RequestWithHeader() */
- private static T RequestWithCustomHeader<T, S>(Func<ClientBase<S>, T> func, ClientBase<S> proxy) where S : class
- {
- using (new OperationContextScope(proxy.InnerChannel))
- {
- var httpRequestProperty = new HttpRequestMessageProperty();
- UserNamePasswordClientCredential userName = proxy.ClientCredentials.UserName;
- httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(userName.UserName + ":" + userName.Password));
- OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
- return func(proxy);
- }
- }
Add Comment
Please, Sign In to add comment