【发布时间】:2018-05-31 02:57:06
【问题描述】:
我正在开发一个 REST API,其中类共享它们的大部分行为,例如简单的 GET PUT POST DELETE。
有很多类,因此我决定创建带有约束的空接口和泛型方法来管理其所有功能。
外观示例:
界面
public interface IGetByOwner
{
}
类
public class Customer : IGetByOwner
{
public int OwnerID {get;set;}
}
帮手
public class LTHttpClient
{
public static async Task<HttpResponseMessage> GetByOwner<T>(int ownerId)
where T : IGetByOwner
{
var uri = $"{LTContext.apiRoot}/{typeof(T).Name}?ownerId={ownerId}";
return await SendRequest(new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new System.Uri(uri)
});
}
}
http 获取
var response = await LTHttpClient.GetByOwner<Customer>(Owner.Id);
这按预期工作。问题出在其他 Http 方法上。
Http POST
var response = await LTHttpClient.GetByOwner<Customer>(Owner.Id);
这会引发异常消息:
System.MissingMethodException: 'Method not found:
'System.Threading.Tasks.Task`1<System.Net.Http.HttpResponseMessage>
LeafyTracker_PCL.LTHttpClient.Post(!!0)'.'
此代码(所有事务)在 PCL .NET Standard 2.0 上运行,同时在 WinForm 项目上调用方法
我做错了什么?
2018 年 1 月 6 日更新
问题似乎是某处图书馆的旧参考。
但是,我已经清理了解决方案,删除了 bin 文件夹并重新构建了所有文件,但仍然抛出异常。有什么建议吗?
【问题讨论】:
-
你有没有故意让问题中的 Http GET 和 Http POST 完全一样?它们都是“var response = await LTHttpClient.GetByOwner
(Owner.Id);”
标签: c# httprequest .net-standard