You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#只读Rest API导出CSV代码报错求助:两个编译错误排查

C# 导出API数据到CSV时遇到两个编译错误

我编写了用于从只读Rest API导出数据到CSV的C#代码,更新后仍出现两个编译错误。提供的文档不完善,我认为Result、IReadOnlyList、HistValueResponseDTO可能仅在某个依赖项中定义。

具体错误

  • 错误CS1061:HistValueResponseDto未包含Tags的定义,也找不到接受该类型第一个参数的可访问扩展方法Tags(是否缺少using指令或程序集引用?),位于Program.cs第89行。
  • 错误CS1503:参数1无法从“方法组”转换为“object?”,位于Program.cs第89行。

完整代码

namespace ConsoleApp7
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.IO;
    using System.Numerics;
    using System.Threading.Tasks;
    using CSharpFunctionalExtensions;
    using CsvHelper;
    using Microsoft.IdentityModel.Tokens;
    using Newtonsoft.Json.Linq;
    using Scada.OneView.Api.Client;
    using Scada.OneView.Api.Client.Abstractions;
    using Scada.OneView.Api.Client.Models.HistData;
    using Serilog;
    class Program
    {
        static async Task Main(string[] args)
        {
            // Initialize parameters
            const string oneViewUrl = "http://192.168.1.11/oneview4";
            const string authenticationUrl = "http://192.168.1.11/oneview4/auth/";
            const string username = "XXXX";
            const string password = "XXXXXXX";
            const string logFilePath = @"C:\Users\jzala\Documents\PivotGenPy\test7.txt";

            // Initialize logger
            Log.Logger = new LoggerConfiguration()
            .WriteTo.Console()
            .WriteTo.File(logFilePath)
            .CreateLogger();

            // Put OneView Authentication URL and username/password here
            var authenticationSettings = new OneViewAuthenticationSettings
            {
                AuthUrl = authenticationUrl,
                Username = username,
                Password = password,
                AuthenticationType = AuthenticationType.IdentityAuthentication,
            };

            // Put OneView URL here
            var oneviewSettings = new OneViewClientSettings
            {
                OneViewUrl = oneViewUrl
            };

            // Create single factory for one OneView, using oneview and authentication settings
            using var oneviewClientFactory = new OneViewClientFactory(
            oneviewSettings,
            authenticationSettings,
            Log.Logger);


            //Historical Values
            var unitGuid = new Guid("d6f49127-d710-4730-9aa1-d44846fd5537");
            var tags = new List<string>() { "WCNV.GriA2_10m_Max", "WCNV.GriA2_10m_Min", "WCNV.GriA2_10m_Std" };
            var dateFrom = new DateTime(2024, 3, 1, 10, 0, 0);
            var DateTO = new DateTime(2024, 3, 5, 0, 0, 0);
            const byte amountOfEvents = 90;


            IOneViewClient oneViewClient = oneviewClientFactory.CreateInstance();

            Result<IReadOnlyList<HistValueResponseDto>> tagResults = await oneViewClient.GetHistTagsAsync(unitGuid, tags, dateFrom,
                DateTO, 2, 2);


            if (tagResults.IsFailure)
            {
                // An error occured
                //Log.Logger.Error(tagResults.Error);
                return;
            }

            // Path of output CSV File
            string csvFilePath = "output.csv";

            using (StreamWriter sw = new StreamWriter(csvFilePath))
            {

                sw.WriteLine("Tags,Count");

                // Iterates over the results and writes them into the file
                foreach (var d in tagResults.Value)
                {
                    // Assuming d.Tags is a string and d.Count is an int
                    sw.WriteLine($"{d.Tags},{d.Count}");
                }



                // Parse the response body
                //var dataObjects = tagResults;
                //foreach (var d in dataObjects)
                //{
                //    Console.WriteLine("{0},{0}", d.Tags,d.Count);
                // }

                //Log.Logger.Information("Historical events from OneView");
                //tagResults.Value.ToList().ForEach(tag => 
                //Log.Logger.Information(tagResults);
                // Log.Logger.Information("Finish reading");
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Jon Z

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 02:44:50