2013-05-29 29 views
1

我後,我的單頁Web應用程序的工作(網頁與ServiceStack的RazorFormat()MVC服務,不.ASP MVC),我跑了(以前路過)服務測試。測試失敗。再次測試Web應用程序(調試運行,在瀏覽器中導航到//localhost:1337/ResourceList:仍在工作。我的測試有問題嗎?ServiceStack JsonServiceClient基於測試失敗,但在瀏覽器服務的工作

這裏的錯誤:

Test Name: TestResourceList 
Test FullName: [0-1015]ServiceWrapper.Test.TestSWrapperServices.TestResourceList 
Test Source: c:\Users\uname\Documents\Visual Studio 2012\Projects\ServiceWrapper\UnitTestProject1\ServiceTests.cs : line 96 
Test Outcome: Failed 
Test Duration: 0:00:02.188 

Result Message: 
System.Net.WebException : Unable to connect to the remote server 
    ----> System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it 127.0.0.1:1337 
Result StackTrace: 
at System.Net.HttpWebRequest.GetResponse() 
at ServiceStack.ServiceClient.Web.ServiceClientBase.Send[TResponse](String httpMethod, String relativeOrAbsoluteUrl, Object request) 
at ServiceStack.ServiceClient.Web.ServiceClientBase.Get[TResponse](IReturn`1 request) 
at ServiceWrapper.Test.TestSWrapperServices.TestResourceList() in c:\Users\uname\Documents\Visual Studio 2012\Projects\ServiceWrapper\UnitTestProject1\ServiceTests.cs:line 98 
--SocketException 
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) 

這裏的測試:

namespace ServiceWrapper.Test 
{ 
    [TestFixture] 
    public class TestSWrapperServices 
    { 
     AppHost appHost; 
     private const string ListeningOn = "http://*:1337/"; 
     public const string Host = "http://localhost:1337"; 
     private const string BaseUri = Host + "/"; 

     [TestFixtureSetUp] 
     public void OnTestFixtureSetUp() 
     { 
      var appSettings = new AppSettings(); 
      var username = Environment.GetEnvironmentVariable("USERNAME"); 
      var userdomain = Environment.GetEnvironmentVariable("USERDOMAIN"); 

      AppHost.AppConfig = new AppConfig(new AppSettings()); 

      appHost = new AppHost(); 

      // initialize Service Server 
      ServiceServer.SetUser(AppHost.AppConfig.UserName, AppHost.AppConfig.Password); 
      ServiceServer.SetLog(String.Empty); 

      try 
      { 
       appHost.Init(); 
       appHost.Start(ListeningOn); 
      } 
      catch (HttpListenerException ex) 
      { 
       if (ex.ErrorCode == 5) 
       { 
        System.Diagnostics.Debug.WriteLine("You need to run the following command (as admin):"); 
        System.Diagnostics.Debug.WriteLine(" netsh http add urlacl url={0} user={1}\\{2} listen=yes", 
         ListeningOn, userdomain, username); 
       } 
       else 
       { 
        System.Diagnostics.Debug.WriteLine("ERROR: {0}: {1}", ex.GetType().Name, ex.Message); 
       } 
      } 
      catch (Exception ex) 
      { 
       System.Diagnostics.Debug.WriteLine("ERROR: {0}: {1}", ex.GetType().Name, ex.Message); 
      } 
     } 

     [TestFixtureTearDown] 
     public void OnTestFixtureTearDown() 
     { 
      appHost.Dispose(); 
     } 

     [Test] 
     public void TestResourceList() 
     { 
      JsonServiceClient client = new JsonServiceClient(BaseUri); 
      ResourceList response = client.Get(new ResourceList()); 
      Assert.Contains("Some Value", response.property); 
     } 
     [Test] 
    } 
} 
+0

可能是類似於此的防火牆或網絡問題(http://stackoverflow.com/questions/15888834/self-hosting-servicestack-rest-service-on-local-network)。也許嘗試將ListeningOn更改爲http:// localhost:1337。 – paaschpa

+0

我會試試看。我很懷疑,因爲在將RazorFormat()添加到配置之前測試正在工作 - 所以這個問題不應與防火牆相關聯 - 自從我第一次在此項目上設置servicestack後,我沒有更改ListeningOn 。 –

+0

而且 - 如果在我的ListeningOn值中有'localhost'或'*',則沒有區別。這與測試中的錯誤完全相同。 –

回答

3

我升級到最新ServiceStack - 55年3月9日,它仍然沒有工作。所以,我重新開始了,從一開始就進行了理智檢查。事實證明,在Program.cs的ListeningOn具有http://*:1337/而NUnit的的TestFixture ListeningOn被http://localhost:1337/

檢查urlacl(如管理員)對http://localhost:1337/:爲http://*:1337/

C:\Windows\system32>netsh http show urlacl url=http://localhost:1337/ 

    URL Reservations: 
    ----------------- 

檢查urlacl(如管理員):

C:\Windows\system32>netsh http show urlacl url=http://*:1337/ 

    URL Reservations: 
    ----------------- 

     Reserved URL   : http://*:1337/ 
      User: DOMAIN\user 
       Listen: Yes 
       Delegate: No 
       SDDL: D:(A;;GX;;;S-1-5-21-2595267603-2801715271-1705165942-1002) 

我以前的疑難解答留下了兩個項目的ListeningOn值不一致。有趣的是,使用http:// *:1337 /不能用作通配符url,正如我所預料的那樣。

這是一個方便的代碼片段,可以幫助您構建add urlacl命令。它還提供了一個有用的(!)健全檢查您正在監聽的確切網址。

Console.WriteLine("You need to run the following command:"); 
    Console.WriteLine(" netsh http add urlacl url={0} user={1}\\{2} listen=yes", 
    ListeningOn, userdomain, username); 

---更新---

升級ServiceStack消除了 '連接積極拒絕' 的錯誤消息。一旦ListeningOn值統一,暴露真實 錯誤消息:

Result Message: ServiceStack.ServiceClient.Web.WebServiceException : Service Unavailable 
Result StackTrace: 
at ServiceStack.ServiceClient.Web.ServiceClientBase.ThrowWebServiceException[TResponse](Exception ex, String requestUri) 
at ServiceStack.ServiceClient.Web.ServiceClientBase.ThrowResponseTypeException[TResponse](Object request, Exception ex, String requestUri) 
at ServiceStack.ServiceClient.Web.ServiceClientBase.HandleResponseException[TResponse](Exception ex, Object request, String requestUri, Func`1 createWebRequest, Func`2 getResponse, TResponse& response) 
at ServiceStack.ServiceClient.Web.ServiceClientBase.Send[TResponse](String httpMethod, String relativeOrAbsoluteUrl, Object request) 
at ServiceStack.ServiceClient.Web.ServiceClientBase.Get[TResponse](IReturn`1 request) 
at RemoteServerWrapper.Test.TestRSWrapperServices.TestDataList() in c:\Users\user\Documents\Visual Studio 2012\Projects\RemoteServerWrapper\UnitTestProject1\ServiceTests.cs:line 183 

它仍然是模糊的 - 但至少它沒有報告東西是從真正的問題完全不同。於是我實施跟蹤在我的app.config,像這樣:

<configuration> 

    <!-- ... other config settings ... --> 

    <system.diagnostics> 
    <sources> 
     <source name="System.Net" tracemode="includehex" maxdatasize="1024"> 
     <listeners> 
      <add name="System.Net"/> 
      <add name="console"/> 
     </listeners> 
     </source> 
     <source name="System.Net.HttpListener"> 
     <listeners> 
      <add name="System.Net"/> 
      <add name="console"/> 
     </listeners> 
     </source> 
    </sources> 
    <switches> 
     <add name="System.Net" value="Verbose"/> 
     <add name="System.Net.HttpListener" value="Verbose"/> 
    </switches> 
    <sharedListeners> 
     <add name="console" 
     type="System.Diagnostics.ConsoleTraceListener" 
     initializeData="false"/> 
     <add name="System.Net" 
     type="System.Diagnostics.TextWriterTraceListener" 
     initializeData="network.log" 
     /> 
    </sharedListeners> 
    <trace autoflush="true"/> 
    </system.diagnostics> 
</configuration> 

裏面露出一個更好的錯誤消息:

ERROR: [::1]:1337 Request not found: /datarequest?DataKey=some_key&startDate=20130701&endDate=20130708 

OK - 現在我已經在servicestack源拉,所以我可以一步通過代碼並找出爲什麼我在測試中得到「未找到」,何時在「調試/運行」並通過瀏覽器進行測試時發揮作用。原來RestHandler.FindMatchingRestPath(httpMethod, pathInfo, contentType)沒有返回匹配。姆。這是爲什麼? AppHost被一致地聲明。那麼,有什麼不同?

其餘的服務住在我的項目的主要程序集中。當從「調試/運行」運行時,默認程序集具有服務,並且一切正常。但是,當從測試項目運行時,將服務程序集作爲參考添加時,servicestack無法找到它們。它們不在相對於測試項目的默認位置。所以我增加了一個APPHOST類在我的測試文件的頂部,而不是依靠從我的Program.cs中之一,並宣佈它,如下所示:

public class RSWrapperServicesAppHostHttpListener 
      : AppHostHttpListenerBase 
{ 
    public RSWrapperServicesAppHostHttpListener() 
     : base("RSWrapper Services Tests", typeof(DataRequestService).Assembly) { } 

// 'DataRequestService' is a random rest service class, 
// defined in the referenced services assembly 

} 

現在ServiceStack是幸福的,我的測試中再次合作。

他們是如何工作的?原本一切都在一個項目中混雜在一起。一旦我把事情分解成單獨的程序集,即DTO,服務,業務邏輯和測試,我就把它分解了。但是因爲我在暫時停止單元測試的同時讓UI工作,所以我沒有馬上注意到。

相關問題