我有一个带有名为SlashBaseService的基本ApiController的webapi项目:
[RouteArea("uBase")]
public abstract class SlashBaseService : ApiController
{
}
Run Code Online (Sandbox Code Playgroud)
生成的dll用于WebForms项目中,因此我还有一个WebActivator类,其中包含以下代码来生成路由:
RouteTable.Routes.MapHttpAttributeRoutes(config =>
{
// Get all services inheriting from SlashBaseService
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
{
foreach (var type in assembly.GetTypes())
{
if (type.IsSubclassOf(typeof(SlashBaseService)))
{
// Scan assembly
config.ScanAssembly(assembly);
// Skip the remaining types in this assembly
break;
}
}
}
});
RouteTable.Routes.MapHttpRoute(
name: "DefaultBase",
routeTemplate: "base/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
Run Code Online (Sandbox Code Playgroud)
我还在一个单独的程序集中有一个testservice:
public class SampleSlashBaseService : SlashBaseService
{
[GET("TestOpenMethod")]
public string GetTestOpenMethod()
{
return "Hello anonymous!"; …Run Code Online (Sandbox Code Playgroud) 今天我在使用异步ApiControllers创建Web API时遇到了问题.我正在使用MongoDB,因为C#驱动程序不支持异步,我尝试在我的存储库层实现它.
构建存储库中生成的方法如下所示:
public async Task<IEnumerable<Building>> GetAll()
{
var tcs = new TaskCompletetionSource<IEnumerable<Building>>();
await Task.Run(() => {
var c = this.MongoDbCollection.FindAll();
tcs.SetResult(c);
});
return await tcs.Task;
}
Run Code Online (Sandbox Code Playgroud)
现在,当使用NUnit自行测试存储库时,这非常有效.
但是当从控制器进行测试时(使用HttpClient),它在运行后永远不会进入"返回"线tcs.SetResult(c).测试一直运行,直到我手动中止.
当我删除Task.Run代码并同步执行所有操作时,一切都按预期工作:
public async Task<IEnumerable<Building>> GetAll()
{
var c = this.MongoDbCollection.FindAll();
return c;
}
Run Code Online (Sandbox Code Playgroud)
有没有人知道为什么我在测试存储库+数据库和测试控制器+存储库+数据库时遇到不同的行为?
的控制器的方法是这样的:(buildingRepository在使用Ninject构造注入)
public async Task<HttpResponseMessage> Get()
{
var result = await this.buildingRepository.GetAll();
return Request.CreateResponse(HttpStatusCode.OK, result);
}
Run Code Online (Sandbox Code Playgroud)
编辑:这里也是测试方法.第一个是不工作的那个:( this.client是一个设置了accept-header的HttpClient对象"application/json")
[Test]
public void Get_WhenBuildingsExist_ShouldReturnBuilding()
{
var task …Run Code Online (Sandbox Code Playgroud) asp.net asynchronous mongodb asp.net-web-api mongodb-.net-driver
有没有人知道如何在没有企业订阅的情况下通过HTTPS/SSL获取maptiles?
.net ×1
asp.net ×1
asp.net-mvc ×1
asynchronous ×1
c# ×1
here-api ×1
https ×1
mongodb ×1
ssl ×1