快速入门:在 C 中为 .NET 请求执行 Dataverse SDK#

本快速入门介绍如何在最小的 C# 控制台应用程序中使用 Microsoft Dataverse SDK for .NET。 使用 ServiceClient 类连接到组织服务,并执行 Web 服务请求以验证对环境业务数据的访问。

应用程序调用 IOrganizationService.Execute 方法 并传递类的 WhoAmIRequest 实例。 从 Web 服务返回的结果是填充 的 WhoAmIResponse.UserId 值,该值是 Dataverse 系统用户帐户的唯一标识符。

注释

本快速入门示例不包括为简洁起见的异常处理。 本快速入门是需要连接到的最小代码示例,并使用 SDK 进行.NET。

可以从 GitHub GetStarted 获取完整的代码示例。 有关更多详细信息,请参阅程序的 自述文件 。

先决条件

  • Visual Studio (2026 或更高版本)
  • Internet 连接
  • 目标环境的 Dataverse 系统用户帐户的登录凭据
  • 要连接的 Dataverse 环境的 URL 地址
  • 基本了解 Visual C# 语言

阅读以下有关在应用程序代码中使用连接字符串或用户名/密码身份验证的重要信息。

重要

Microsoft建议使用可用的最安全的身份验证流。 本文中所述的身份验证流要求在应用程序中高度信任,并且存在其他流中不存在的风险。 仅当其他更安全的流(如托管标识)不可行时,才应使用此流。

在 Visual Studio 中创建.NET控制台应用项目

  1. 创建新项目。 对于此项目,请使用 Visual Studio 2026。

    显示启动新项目的选项的Visual Studio屏幕截图

  2. 选择.NET控制台应用项目模板。

    显示.NET控制台应用项目模板的Visual Studio屏幕截图。

    选择“下一步”。

  3. 设置项目名称和位置。

    显示.NET名称和位置对话框的Visual Studio屏幕截图。

    选择“下一步”。

  4. 选择.NET 10.0(长期支持)作为框架。

    显示.NET框架支持配置的Visual Studio的屏幕截图。

    选择“创建”。

  5. 在 解决方案资源管理器中,右键单击创建的项目,然后在上下文菜单中选择“ 管理 NuGet 包...” 。

    为项目选择“管理 NuGet 包”的解决方案资源管理器屏幕截图。

  6. 浏览 最新版本的 Microsoft.PowerPlatform.Dataverse.Client NuGet 包并安装它。

    NuGet 程序包管理器的屏幕截图,其中显示了Microsoft。PowerPlatform.Dataverse.Client 包已准备好安装。

注释

系统会提示你选择“应用于预览更改”,然后在“许可接受”对话框中选择“我接受”。

为用于.NET请求的 Dataverse SDK 添加代码

  1. 在解决方案资源管理器中,双击Program.cs以编辑文件。 将文件的内容替换为以下代码。

    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.PowerPlatform.Dataverse.Client;
    using Microsoft.Xrm.Sdk;
    
    /// <summary>
    /// Public client authentication followed by a web service request.
    /// Use of username and password for authentication.
    /// </summary>
    /// <remarks>This sample uses a standard app registration used for
    /// demonstrating SDK code samples.</remarks>
    class Program
    {
        // TODO Enter your Dataverse environment's URL and logon info.
        static string url = "https://myorg.crm.dynamics.com";
        static string userName = "someone@myorg.onmicrosoft.com";
        static string password = "password";
    
        // This service connection string uses the info provided above.
        // The standard AppId and RedirectUri are provided for sample code demonstration only.
        static string connectionString = $@"
        AuthType = OAuth;
        Url = {url};
        UserName = {userName};
        Password = {password};
        AppId = 51f81489-12ee-4a9e-aaae-a2591f45987d;
        RedirectUri = http://localhost;
        LoginPrompt=Auto;
        RequireNewInstance = True";
    
        static void Main()
        {
            // ServiceClient class implements IOrganizationService interface
            IOrganizationService service = new ServiceClient(connectionString);
    
            var response = (WhoAmIResponse)service.Execute(new WhoAmIRequest());
    
            Console.WriteLine($"User ID is {response.UserId}.");
    
    
            // Pause the console so it does not close.
            Console.WriteLine("Press the <Enter> key to exit.");
            Console.ReadLine();
        }
    }
    
  2. 更改和代码注释指示// TODO的值userNameurlpassword。

    注释

    可以在旧版 Web 应用程序中的“设置自定义>开发人员资源”>或“Power Apps设置(齿轮图标)>开发人员资源中找到环境 URL。

    虽然此代码示例将用户名和密码信息放在代码中以简单起见,但其他代码示例使用更推荐的方法提示该信息或将其存储在单独的 App.config 或 appsettings.json 文件中。

    可以找到连接字符串参数中列出的 AuthType 支持的值。

运行程序

按 F5 运行程序。 输出应类似于以下示例:

User ID is 00aa00aa-bb11-cc22-dd33-44ee44ee44ee
Press any key to exit.

使用 IOrganizationService 接口方法

此程序使用的 Dataverse.Client.ServiceClient 实现 IOrganizationService 接口,其中包括 Execute 方法。

若要更好地了解 IOrganizationService 接口,请尝试以下步骤:

  1. 转到 WhoAmIRequest 类的参考文章。

  2. 复制本文中的示例方法。 它的外观如下所示:

    /// <summary>
    /// Outputs the data returned from the WhoAmI message
    /// </summary>
    /// <param name="service">Authenticated client implementing the IOrganizationService interface</param>
    static void WhoAmIExample(IOrganizationService service) {
    
       var response = (WhoAmIResponse)service.Execute(new WhoAmIRequest());
    
       Console.WriteLine($"OrganizationId:{response.OrganizationId}");
       Console.WriteLine($"BusinessUnitId:{response.BusinessUnitId}");
       Console.WriteLine($"UserId:{response.UserId}");
    
    }
    

    请注意,它接受 IOrganizationService 服务实例作为参数。

  3. 将此方法 WhoAmIExample 粘贴到程序中的方法下方 Main 。

  4. 将 Main 程序中的方法替换为:

        static void Main()
     {
         //ServiceClient implements IOrganizationService interface
         IOrganizationService service = new ServiceClient(connectionString);
    
         WhoAmIExample(service: service);
    
         // Pause the console so it does not close.
         Console.WriteLine("Press the <Enter> key to exit.");
         Console.ReadLine();
     }
    
  5. 再次运行示例。 您应该会看到如下类似的内容:

    OrganizationId:00aa00aa-bb11-cc22-dd33-44ee44ee44ee
    BusinessUnitId:11bb11bb-cc22-dd33-ee44-55ff55ff55ff
    UserId:22cc22cc-dd33-ee44-ff55-66aa66aa66aa
    Press the <Enter> key to exit.
    

后续步骤

有了连接到 Dataverse 的简单控制台程序后,请使用此项目尝试其他方法和消息。 可以使用此快速启动控制台应用程序项目执行即席测试。

尝试其他 IOrganizationService 接口方法

Tip

在文档中,可以找到许多接受参数的示例方法WhoAmIExampleIOrganizationService service。

尝试这些 IOrganizationService 方法的示例:

尝试其他消息

可以在以下命名空间中使用 Execute 方法 查找其他可以调用的消息:

了解如何使用记录数据

以下文章介绍如何在 Dataverse 表中处理业务数据:

浏览我们的代码示例

可以在 PowerApps-Samples/dataverse/orgsvc 的GitHub存储库中找到 sdk for .NET 示例代码。

使用 ServiceClient 扩展

除了实现 IOrganizationService 接口外, ServiceClient 还提供扩展方法,这些方法超出了定义的 IOrganizationService 核心方法以及启用 ILogger 日志记录的功能。 详细了解如何使用 ServiceClient