2013-01-13 7 views
9

フォーム私は私が受け取るセルフ

http://localhost:8080/api/*(some-controller)* 

をしようとすると、私は

namespace MascoteAquarium.Desktop 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main() 
     { 
      var config = new HttpSelfHostConfiguration("http://localhost:8080"); 
      config.Routes.MapHttpRoute(
       "DefaultApi", "api/{controller}/id", new { id = RouteParameter.Optional }); 

      using (HttpSelfHostServer server = new HttpSelfHostServer(config)) 
      { 
       server.OpenAsync().Wait(); 
      } 

      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new frmMainMenu()); 
     } 
    } 
} 

以下のコードを使用して、自己のホストにWindowsフォームアプリケーション内のWeb APIサービスをしようとしていますNullReferenceException at System.Web.Http.SelfHost.HttpSelfHostServer.ProcessRequestContext(ChannelContext channelContext、RequestContext requestContext)

誰かが何が起こっているのか知っていますか? Win Formsアプリケーション内で自己ホストすることは可能ですか?

+0

あなたが問題を解決するために管理していましたか? – Osi

答えて

1
  1. あなたは、そうでない場合は、自己のホストポートを開くことが許可されることはありません、(デバッガからのWinFormアプリを実行した場合やVS)(管理者など)特権でのWinFormsアプリケーションを実行する必要があります。

  2. すでに

8

問題がHttpSelfHostServerオブジェクトが失われるということで他のアプリケーションがポート8080で実行されていないことを確認してくださいだけ含まれていApplication.Run(...)、前プログラムを実行し続けるメインイベントループ。 using文は廃棄方法は、このようにあなたが経験しているとNullReferenceExceptionで、その結果、要求に答えるために、それが使用できなくなって、サーバーこの場合には、オブジェクトに対して呼び出されることを確認します。

が例外を修正するには、あなたのコードは次のようになります。

... 
using (HttpSelfHostServer server = new HttpSelfHostServer(config)) 
{ 
    server.OpenAsync().Wait(); 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 
    Application.Run(new frmMainMenu()); 
} 
... 
関連する問題