.net 处理后输入流为空,提示:检查流有效性

j8ag8udp  于 2022-12-24  发布在  .NET
关注(0)|答案(1)|浏览(99)

我有以下3个方法从ConcurrentBag摄取数据到Kusto(Azure数据资源管理器)。它在.NET核心中工作得很好。但在.NET框架中不工作。它失败并显示错误
输入流处理后为空,提示:检查流有效性
有人能建议如何克服空流错误吗

public static void DSCMKustoBulkIngest(ILogger logger, AppSettings AppConfig)
        {
            if (DSCMJITPoliciesCollection.Count != 0)
            {
                for (int l = 0; l < DSCMJITPoliciesCollection.Count; l += 100)
                {
                    List<DSCMJITPoliciesModel> results = DSCMJITPoliciesCollection.Skip(l).Take(100).ToList();
                    DSCMIngestToKusto(results, AppConfig, logger);
                }
                logger.LogInformation(DSCMJITPoliciesCollection.Count + $"- Total DSCM Records ingested into DSCMJITPoliciesSnapshot Kusto Table: {DateTime.Now}");
            }
            else
            {
                logger.LogInformation($"No Records to Ingest into Kusto: {DateTime.Now}");
                ServicesCount = 0;
            }
        }

        private static string DSCMIngestToKusto(List<DSCMJITPoliciesModel> DSCMJITPoliciesCollection, AppSettings AppConfig, ILogger logger)
        {
            Stream stream = CreateDSCMJsonStream(DSCMJITPoliciesCollection);
            var KustoConnectionStringBuilderEngine = new KustoConnectionStringBuilder(AppConfig.KustoClusterFQDN)
                .WithAadApplicationKeyAuthentication(applicationClientId: AppConfig.KustoAppClientID, applicationKey: RAMPCommon.KeyVault
                .GetSecret(AppConfig.KustoAppKey, AppConfig, logger), authority: AppConfig.AMETenantID);
            CreateJsonMappingIfNotExists(KustoConnectionStringBuilderEngine, AppConfig.KustoDataBaseName, "DSCMJITPoliciesSnapshot");

            using (var siClient = KustoClientFactory.CreateCslStreamIngestClient(KustoConnectionStringBuilderEngine))
            {
                siClient.ExecuteStreamIngestAsync(
                    AppConfig.KustoDataBaseName,
                    "DSCMJITPoliciesSnapshot",
                    stream,
                    null,
                    DataSourceFormat.json,
                    compressStream: false,
                    mappingName: s_DSCMjsonMappingName).ResultEx();
            }            
            return "Success";
        }

        private static Stream CreateDSCMJsonStream(List<DSCMJITPoliciesModel> DSCMJITPoliciesCollection)
        {
            var ms = new MemoryStream();
            using (var tw = new StreamWriter(ms, Encoding.UTF8, 4096, true))
            {
                foreach (DSCMJITPoliciesModel R in DSCMJITPoliciesCollection)
                {
                    tw.WriteLine("{{'ServiceGroupName':'{0}', 'TeamGroupName':'{1}', 'ServiceName':'{2}', 'ServiceTreeID':'{3}', 'Level':'{4}', 'SOX':'{5}','ResourceType':'{6}', " +
                        "'PolicyID':'{7}' ,'PolicyType':'{8}', 'Environment':'{9}' , 'ConditionName':'{10}', 'RequestorGroups':'{11}', 'AccessLevels':'{12}', 'IcMTeamNames':'{13}'" +
                        ",'ApprovalPeriod':'{14}', 'NotifyEmail':'{15}' , 'Compliant':'{16}' ,'Action':'{17}' ,'CollectionTime':'{18}' ,'RowID':'{19}' ,'RunID':'{20}'}}",
                                  R.ServiceGroupName, R.TeamGroupName, R.ServiceName, R.ServiceTreeID, R.Level, R.SOX, R.ResourceType,
                                  R.PolicyID, R.PolicyType, R.Environment, R.ConditionName, R.RequestorGroups, R.AccessLevels, R.IcMTeamNames,
                                  R.ApprovalPeriod, R.NotifyEmail, R.Compliant, R.Action, R.CollectionTime, R.RowID, R.RunID);
                }
            }
            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
ilmyapht

ilmyapht1#

明白了。这是由于一个无效字符。显然Kusto不允许在输入流中使用“\”。

相关问题