本快速入门介绍如何在最小的 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控制台应用项目
创建新项目。 对于此项目,请使用 Visual Studio 2026。
选择.NET控制台应用项目模板。
选择“下一步”。
设置项目名称和位置。
选择“下一步”。
选择.NET 10.0(长期支持)作为框架。
选择“创建”。
在 解决方案资源管理器中,右键单击创建的项目,然后在上下文菜单中选择“ 管理 NuGet 包...” 。
浏览 最新版本的
Microsoft.PowerPlatform.Dataverse.ClientNuGet 包并安装它。
注释
系统会提示你选择“应用于预览更改”,然后在“许可接受”对话框中选择“我接受”。
为用于.NET请求的 Dataverse SDK 添加代码
在解决方案资源管理器中,双击
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(); } }更改和代码注释指示
// 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 接口,请尝试以下步骤:
转到 WhoAmIRequest 类的参考文章。
复制本文中的示例方法。 它的外观如下所示:
/// <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服务实例作为参数。将此方法
WhoAmIExample粘贴到程序中的方法下方Main。将
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(); }再次运行示例。 您应该会看到如下类似的内容:
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 表中处理业务数据:
- 使用 SDK for .NET 的实体类操作
- 使用 SDK for .NET 创建表行
- 使用 SDK for .NET 检索表行
- 使用 SDK for .NET 更新和删除表行
- 使用 SDK for .NET 关联和取消关联表行
浏览我们的代码示例
可以在 PowerApps-Samples/dataverse/orgsvc 的GitHub存储库中找到 sdk for .NET 示例代码。
使用 ServiceClient 扩展
除了实现 IOrganizationService 接口外, ServiceClient 还提供扩展方法,这些方法超出了定义的 IOrganizationService 核心方法以及启用 ILogger 日志记录的功能。
详细了解如何使用 ServiceClient