2012-06-13 50 views
0

我有這個控制檯主機爲我的WCF Web服務 Program.cs的控制檯主機WCF Web服務關停

class Program 
    { 
     static void Main(string[] args) 
     { 
      WebServiceHost Host = new WebServiceHost(typeof(Service1)); 

      try 
      { 
       Host.Open(); 
       Console.ReadLine(); 
       Host.Close(); 
      } 

      catch (Exception e) 
      { 
       Console.WriteLine(e); 
       Host.Abort(); 
      } 

     }  

這是我的主機

<configuration> 
    <system.web> 
    <compilation debug="true"/> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="Csvpost.Service1"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:8000/"/> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    </system.serviceModel> 
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup> 
</configuration> 

這是的app.config服務1類的服務

public class Service1 : IService1 
    { 
     public Stream HandleMessage(Stream request) 
     { 
      StreamReader reader = new StreamReader(request); 
      string text = reader.ReadToEnd(); 
      System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); 
      MemoryStream ms = new MemoryStream(encoding.GetBytes(text)); 
      WebOperationContext.Current.OutgoingResponse.ContentType = "text/html"; 
      string[] sites = text.Split('\n'); 
      int y = sites.Length; 
      int i; 
      for (i = 0; i < y; i++) 
      { 
       /logic/ 
       System.Data.SqlClient.SqlConnection con; 
       System.Data.SqlClient.SqlCommand cmd; 
       con = new System.Data.SqlClient.SqlConnection(connection); 
       cmd = new System.Data.SqlClient.SqlCommand(query, con); 
       con.Open(); 
       cmd.ExecuteNonQuery(); 
       con.Close(); 
      } 
      return ms; 
     } 
    } 

我的主機一旦開始運行就會關閉。我做了什麼錯誤? 在此先感謝。

+0

是在同一個項目中WCF服務的服務主機? –

+1

我已經在我的嘗試塊。 –

+0

我希望,如果它被過早關閉,這將是因爲一個糟糕的app.config,或者Main'的'第一線未處理的異常。無論哪種方式,控制檯應用程序應該發出異常信息。 – HackedByChinese

回答

0

嘗試使用WCF的跟蹤選項。它會爲你記錄錯誤。您的應用程序正在過早終止。跟蹤將幫助您找出問題。

2

您試圖啓動一個控制檯WebServiceHost。你需要啓動一個ServiceHost。這也可能是您沒有收到錯誤消息的原因,因爲WebServiceHost不會寫入控制檯。

而你不重視服務在配置的端口。這是一臺爲我工作的控制檯主機。

<services> 
    <service name="MajicEightBallServiceLib.MagicEightBallService" 
      behaviorConfiguration="EightBallServiceMEXBehavior" > 
    <endpoint address="" 
       binding="wsHttpBinding" 
       contract="MajicEightBallServiceLib.IEightBall" /> 
    <endpoint address="mex" 
       binding ="mexHttpBinding" 
       contract="IMetadataExchange" /> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://localhost:8000/MagicEightBallService"/> 
     </baseAddresses> 
    </host>    
    </service> 
</services>