Get-6ren">
gpt4 book ai didi

c# - 具有Simple.OData.Client的DTO

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:11 24 4
gpt4 key购买 nike

在我的服务器中,我有以下设置:

EntitySet<Contact>("Contacts");
EntityType<ContactDto>();


这是控制器动作:

[EnableQuery]
public ActionResult<IQueryable<ContactDto>> Get()
{
return Ok(_DbContext.Contacts.Select(c => new ContactDto(c)));
}

[EnableQuery]
public async Task<ActionResult<Contact>> Get(int key)
{
return await _DbContext.Contacts.FindAsync(key);
}


ContactContactDto实现 IContact,除了它们彼此之间不知道。

当查询服务器(使用 Contacts路由)时,我得到了预期的结果,但是在客户端中,我试图这样做:

var settings = new ODataClientSettings(new Uri(
$"https://localhost:{Constants.Port}/{Constants.Api}"));
var client = new ODataClient(settings);

var result = await client
.For<Contact>()
.FindEntriesAsync();


我有一个例外:


   Microsoft.OData.ODataException: 'The context URL 'https://localhost:54687/api/$metadata#Contacts/ODataDtoRepro.Models.ContactDto' is invalid


堆栈跟踪:

at Microsoft.OData.JsonLight.ODataJsonLightContextUriParser.ParseContextUriFragment(String fragment, Func`3 clientCustomTypeResolver, Boolean throwIfMetadataConflict, Boolean& isUndeclared)
at Microsoft.OData.JsonLight.ODataJsonLightContextUriParser.ParseContextUri(ODataPayloadKind expectedPayloadKind, Func`3 clientCustomTypeResolver, Boolean throwIfMetadataConflict)
at Microsoft.OData.JsonLight.ODataJsonLightContextUriParser.Parse(IEdmModel model, String contextUriFromPayload, ODataPayloadKind payloadKind, Func`3 clientCustomTypeResolver, Boolean needParseFragment, Boolean throwIfMetadataConflict)
at Microsoft.OData.JsonLight.ODataJsonLightDeserializer.ReadPayloadStart(ODataPayloadKind payloadKind, PropertyAndAnnotationCollector propertyAndAnnotationCollector, Boolean isReadingNestedPayload, Boolean allowEmptyPayload)
at Microsoft.OData.JsonLight.ODataJsonLightReader.ReadAtStartImplementation()
at Microsoft.OData.ODataReaderCore.ReadImplementation()
at Microsoft.OData.ODataReaderCore.ReadSynchronously()
at Microsoft.OData.ODataReaderCore.InterceptException[T](Func`1 action)
at Microsoft.OData.ODataReaderCore.Read()
at Simple.OData.Client.V4.Adapter.ResponseReader.ReadResponse(ODataReader odataReader, IODataResponseMessageAsync responseMessage)
at Simple.OData.Client.V4.Adapter.ResponseReader.<GetResponseAsync>d__3.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Simple.OData.Client.ODataClient.<ExecuteRequestWithResultAsync>d__147`1.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Simple.OData.Client.ODataClient.<FindAnnotatedEntriesAsync>d__88.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Simple.OData.Client.ODataClient.<FindEntriesAsync>d__89.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
at Simple.OData.Client.FluentClientBase`2.<FilterAndTypeColumnsAsync>d__91.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Client.Program.<Main>d__0.MoveNext() in D:\Users\Shimmy\Documents\Visual Studio 2019\Projects\ODataDtoRepro\Client\Program.cs:line 16


我在 GitHub上做了一个repro项目。

我创建了另一个 repro branch,尝试使用不同的方法,以便它使用在EDM模型中注册的共享基类,而不是接口。
尽管如此,Simple.OData.Client仍然无法解决它。
请同时检查我对 this问题的评论。

如何使DTO与OData一起使用?

更新资料

可能的解决方案:使用动作/功能。尝试进行过测试,但无法使其在客户端和服务器之间运行,请参阅 this答案以获取更多信息。

最佳答案

我认为问题在于两个对象之间没有关系。我删除了您的代码,并通过删除实体集使其正常工作

var edmModel = new ODataConventionModelBuilder();
edmModel.EntitySet<ContactDto>("Contacts");
routeBuilder.MapODataServiceRoute("API Route", Constants.Api, edmModel.GetEdmModel());


并通过ContactD来扩展Contact

public class ContactDto : Contact


由于联系人是联系人dto的子集,因此按预期返回且没有错误。让我知道是否要我将代码提交到您的github。

关于c# - 具有Simple.OData.Client的DTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58278116/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com