无法对Nancy自托管执行简单的GET请求:RuntimeBinderException和ViewNotFoundException

时间:2017-12-13 12:11:15

标签: c# httpresponse nancy httpserver viewengine

我的代码:

import { Platform } from 'ionic-angular';
import { Contacts, Contact, ContactField, ContactName } from '@ionic-native/contacts';

constructor(private contacts: Contacts, private plt: Platform) {   
   this.plt.ready().then((readySource) => {
      console.log('Platform ready from', readySource);
      // Platform now ready, execute any required native code
      this.initContacts();
    });
}

initContacts(): void {
   let contact: Contact = this.contacts.create();

   contact.name = new ContactName(null, 'Smith', 'John');
   contact.phoneNumbers = [new ContactField('mobile', '6471234567')];
   contact.save().then(
     () => console.log('Contact saved!', contact),
     (error: any) => console.error('Error saving contact.', error)
   );

   // If you want to open the native contacts screen and select the contacts from there use pickContact()

   this.contacts.pickContact()
                .then((response: Contact) => { 
                   console.log(response)
                });
}

使用Insomnia REST client运行以下HTTP请求:public class MyWebService : IDisposable { private readonly NancyHost _host; public MyWebService(int port = 7017) { var uri = new Uri(string.Format("http://localhost:{0}", port)); _host = new NancyHost(uri); _host.Start(); } public void Dispose() { if (_host != null) { _host.Stop(); _host.Dispose(); } } } internal class MyWebModule : NancyModule { public MyWebModule() { Get["/"] = _ => "Received GET request"; } } 时,我会收到以下隐藏异常:

GET http://localhost:7017/

使用来源:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot convert type 'Nancy.ErrorHandling.DefaultStatusCodeHandler.DefaultStatusCodeHandlerResult' to 'Nancy.Response'' at CallSite.Target(Closure , CallSite , Object )

随后

Anonymously Hosted DynamicMethods Assembly

在例外情况下没有任何其他信息。

然后,REST客户端显示错误Nancy.ViewEngines.ViewNotFoundException at Nancy.ViewEngines.DefaultViewFactory.GetRenderedView(String viewName, Object model, ViewLocationContext viewLocationContext)

我定义了什么错误?我已按照以下示例进行操作:building-a-simple-http-server-with-nancy

1 个答案:

答案 0 :(得分:1)

如果在调试器下运行代码 - 确保抛出的异常实际上未处理。代码可能会抛出异常并且您的调试器(如果配置为这样做)可能会破坏它们,即使处理了这些异常(通过某些catch块)。因为你毕竟收到404回复并且没有崩溃 - 我想这些例外是“正常”南希流的一部分,因此被处理。

至于404 - 你的模块是内部的,Nancy不会发现它。改为公开:

public class MyWebModule : NancyModule
{
    public MyWebModule()
    {
        Get["/"] = _ => "Received GET request";
    }
}