0

我正在使用 .Net Core 3.1 开发一个系统,以在 odata Web API 端点上与 Microsoft Dynamics 365 集成。

但是,当我想使用 Web API 获取实体时,我无法获取 OptionSet、2 个选项的“FormattedValue”。因为似乎这两个 API 都会尝试解析 $metadata XML 中声明的属性但是,这些 FormattedValue 属性不会出现在 $metadata

            var settings = new ODataClientSettings(new Uri($"{Environment.GetEnvironmentVariable("CRM_API_URL")}"));
            settings.IgnoreResourceNotFoundException = true;
            settings.BeforeRequest += message =>
            {
                string authority = $"https://login.microsoftonline.com/{ Environment.GetEnvironmentVariable("Tenant_ID") }/oauth2";
                string[] scopes = new string[] { $"{Environment.GetEnvironmentVariable("CRM_URL")}/.default" };
                string secret = Environment.GetEnvironmentVariable("Client_Secret");
                string clientId = Environment.GetEnvironmentVariable("Client_ID");
                IConfidentialClientApplication app = ConfidentialClientApplicationBuilder.Create(clientId)
                                                          .WithClientSecret(secret)
                                                          .WithAuthority(new Uri(authority))
                                                          .Build();
                AuthenticationResult result = null;
                try
                {
                    result = System.Threading.Tasks.Task.Run(() => app.AcquireTokenForClient(scopes).ExecuteAsync()).Result;
                }
                catch (MsalServiceException ex)
                {
                    // AADSTS70011
                    // Invalid scope. The scope has to be of the form "https://resourceurl/.default"
                    // Mitigation: this is a dev issue. Change the scope to be as expected
                }
                message.Headers.Add("Authorization", "Bearer " + result.AccessToken);
                message.Headers.Add("Prefer", "odata.include-annotations=\"*\"");
                Console.WriteLine(message.Method + " " + message.RequestUri);
            };
            settings.IgnoreUnmappedProperties = false;
            settings.AfterResponse += message =>
            {
                HttpResponseMessage resultMessage = (HttpResponseMessage)message;
                Console.WriteLine(resultMessage.Content.ReadAsStringAsync().Result);
            };
            var client = new ODataClient(settings);
            var x = ODataDynamic.Expression;
            dynamic sapaccount = client.For(x.ttr_sapaccount).Filter(x.ttr_sapaccountId == new Guid("eff4de9c-78c7-eb11-bacc-000d3aa2b221")).FindEntryAsync().Result;
            SAPAccount sapaccount2 = client.For<SAPAccount>("ttr_sapaccount").Filter(x => x.ttr_sapaccountId == new Guid("eff4de9c-78c7-eb11-bacc-000d3aa2b221")).FindEntryAsync().Result;

如果我尝试使用动态类型:

dynamic sapaccount = client.For(x.ttr_sapaccount).Filter(x.ttr_sapaccountId == new Guid("eff4de9c-78c7-eb11-bacc-000d3aa2b221")).FindEntryAsync().Result;

sapaccount 将只包含字典中的标准属性。

如果我尝试通过以下示例使用实体类:

        [Column("_createdby_value")]
        public Guid? CreatedById { get; set; }
        [Column("_createdby_value@OData.Community.Display.V1.FormattedValue")]
        public string CreatedBy { get; set; }
        [Column("createdon")]
        public DateTimeOffset? CreatedOn { get; set; }
        ...
        [Column("versionnumber")]
        public long? VersionNumber { get; set; }

    }
SAPAccount sapaccount2 = client.For<SAPAccount>("ttr_sapaccount").Filter(x => x.ttr_sapaccountId == new Guid("eff4de9c-78c7-eb11-bacc-000d3aa2b221")).FindEntryAsync().Result;

sapaccount2 在“CreatedBy”上将为空,但“CreatedById”包含 SystemUser 的 Guid。

我还尝试使用 AfterResponse 事件调试原始输出 json,该事件检查是否返回了那些“*@OData.Community.Display.V1.FormattedValue”。

无论如何我可以绕过元数据从 JSON 中获取所有数据吗?或者是否有任何建议的库可以做到这一点,除了 Micrtosoft.crmsdk,因为它使用的是 XRM 服务,将来可能会被弃用?

请帮忙。非常感谢。

4

0 回答 0