2014-09-25 77 views
1

當我調試單元測試此代碼工作得很好:Design_Time_Address工作在調試器,但單位試運行期間不

[TestFixture] 
public class Tests 
{ 
    private ServiceHost _host; 
    private Uri _baseAddress = new Uri(<code>"http://localhost:8733/Design_Time_Addresses/service"</code>); 
    private Inbound.Client.ServiceClient _client; 

    [SetUp] 
    public void Setup() 
    { 
     _host = new ServiceHost(typeof(MyService), _baseAddress); 
     var smb = new ServiceMetadataBehavior(); 
     smb.HttpGetEnabled = true; 
     smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15; 
     _host.Description.Behaviors.Add(smb); 
     _host.Open(); 
     _client = new Inbound.Client.ServiceClient(); 
    } 

    [TearDown] 
    public void TearDown() 
    { 
     _client.Close(); 
     _host.Close();   
    } 

    [Test] 
    public void Can_ping() 
    { 
     PingResponse response = _client.Ping(new PingRequest { Echo = "Hi" }); 
     Assert.IsNotNull(response); 
     Assert.AreEqual("Hi", response.Echo); 
    } 
} 

然而,當我運行單元測試(右鍵單擊測試在VS測試資源管理器,運行選定的測試),我收到此錯誤信息:

"... System.ServiceModel.ServerTooBusyException : 
The HTTP service located at http://localhost:8733/Design_Time_Addresses is unavailable. ... 
----> System.Net.WebException : The remote server returned an error: (503) Server Unavailable." 

看來,當NUnit的測試運行器運行測試,它無法啓動主機。我知道在VS安裝過程中,Design_Time_Address uri是在netsh中註冊的,我想知道是否應該爲NUnit測試運行器另外註冊。

回答

2

Design_Time_Address與WcfTestClient關聯。有一個WCF項目屬性「在同一解決方案中調試另一個項目時啓動WCF服務主機」。此屬性已被檢查。在單元測試期間,啓動WCF測試客戶端。有趣的部分是_host = new ServiceHost(typeof(MyService), _baseAddress);初始化host剛剛啓動的WCF測試客戶端實例,而不是創建一個新的服務主機實例。當單元測試在調試器之外運行時WCF測試客戶端未啓動,致電ServiceHost僅僅是失敗。

的問題是,服務URL應該是

(<code>"http://localhost:8733/Design_Time_Addresses"</code>

,而不是

<code>"http://localhost:8733/Design_Time_Addresses/service

即使使用默認WCF腳手架模板是 「/服務」

相關問題