.net-core - simple.odata 和 Microsoft.odata 无法将“@OData.Community.Display.V1.FormattedValue”检索到字典和模型
问题描述
我正在使用 .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 服务,将来可能会被弃用?
请帮忙。非常感谢。
解决方案
推荐阅读
- php - PHP Require 调用上的错误 404,由区分大小写的差异引起
- java - LinkedList 实现指针是基于 Java 的吗?
- python-3.x - 莎拉的报价函数
- java - 如何让 JTextArea 使用其关键事件?
- networking - 网络日志中的 WebRTC 数据包
- javascript - 如何从javascript中的3个对象数组中排序并获取最大值并将其分配给另一个数组?
- amazon-web-services - 我无法访问具有公共 IP 的网站
- python - 要求已安装,但是当我尝试运行该程序时它说它没有
- python - groupby 对象到数据框
- html - 导航栏徽标在 CSS 中不起作用,但在 HTML 中起作用