我正在try 使用C#编写以下代码来调用REST API.当我将这段代码放在它自己的单独C#应用程序中时,它就可以工作了.然而,当我try 将其合并到另一个应用程序中,以便可以动态克隆报表时,HttpResponseMessage就退出了,没有响应、没有错误或任何东西.

所以基本上我调试,我一行一行地调试.当它到达HttpResponseMessage时,它退出ReportClone.Main()类并启动DatasetManager.ConnectToSQLServer()类.我看不到任何错误消息.

下面是调用报表克隆REST API的代码.

using System;
using System.Data.SqlClient;
using Microsoft.AnalysisServices.Tabular;

namespace PowerBI_Development {

  class Program {

    static void Main() {


            ReportClone.Main();

            DatasetManager.ConnectToSQLServer();


        }


  }
}

然后调用该文件:


using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;


namespace PowerBI_Development 
{
    internal class ReportClone
    {
        static readonly HttpClient client = new HttpClient();

        public static async Task Main()

        {

            try
            {
                var reportName = "REPORT_NAME";
                var reportID = "REPORT_ID";
                var targetModelID = "TARGET_MODEL_ID";
                var targetWorkspaceID = "TARGET_WORKSPACE_ID";
                var bearer = "BEARER TOKEN";

                var url = $"https://api.powerbi.com/v1.0/myorg/reports/{reportID}/Clone";
                var data = "{\r\n  \"name\": \"" + reportName + "\",\r\n  \"targetModelId\": \"" + targetModelID + "\",\r\n  \"targetWorkspaceId\": \"" + targetWorkspaceID + "\"\r\n}";
               

                client.DefaultRequestHeaders.Add("Authorization", "Bearer " + bearer);

                var content = new StringContent(data, Encoding.UTF8, "application/json");

                HttpResponseMessage response = await client.PostAsync(url, content);

                response.EnsureSuccessStatusCode();

                string responseBody = await response.Content.ReadAsStringAsync();

                Console.WriteLine(responseBody);
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Error copying reports: " + ex.Message);
                Console.ResetColor();
            }
        }
    }
}

我可以让它在一个单独的文件中工作,但我希望将其组合在一起,这样我就可以在循环创建数据集的同时循环访问rest API.我想同时创建一个数据集和一个报表.

推荐答案

这个答案来自 comments 中的@XWIKO.他们提到要做以下事情:

好的.将静态空Main设置为静态异步任务Main.等待ReportClone.Main();.并且不要接受Try Catch块中的异常(抛出ex;).我打赌你会看到一些结果的.- XWIKO 10月5日20:26

我得到了它,并创建了这个运行正常的代码:

using System;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Microsoft.AnalysisServices.Tabular;


namespace PowerBI_Development 
{

    class Program
    {

        static async Task Main()
        {

            
            DatasetManager.GetCompanyCategoryFormID();


            await ReportClone.callReportCloneRestAPI();

            



        }


    }
}

Csharp相关问答推荐

有没有一种方法可以在包含混合文本的标签中嵌入超链接?

如何告诉自己创建的NuGet包在应用程序中发生了变化?

try 还原包时出错:Dapper已经为System.Data.SQLClient定义了依赖项''''

在FilePath中搜索一个词,并根据First Match从左到右提取文件路径

在C#中使用类中的对象值

不带身份的Blazor服务器.Net 8 Cookie身份验证

如何在C#中从正则表达式中匹配一些数字但排除一些常量(也是数字)

为什么我的表单在绑定到对象时提交空值?

使用带有WithAppOnly()请求选项的pageIterator

取决于您的数据量的多个嵌套循环

我的MRG32k3a算法实现返回的数字超出了预期的[0,1]范围

Savagger使用Fastendpoint更改用户界面参数

是否可以将Collectionview中的数组与ObservableCollection绑定?

C#按名称从类获取属性值类型<;t>;,我需要反射吗?

将操作从编辑页重定向到带参数的索引页

Postgres ENUM类型在第一次运行时对Dapper不可见

我找不到ASP.NET Web应用程序(.NET框架).已 Select .NET框架项目和项模板以及此组件(&Q;)

如何使用.NET 8.0中新的CompositeFormat类?

从HTML元素获取 colored颜色

ASP.NET核心MVC|如何在控制器方法之间传递值