.net WCF web服务的控制台主机正在关闭

.net WCF web服务的控制台主机正在关闭,.net,wcf,web-services,.net,Wcf,Web Services,我的WCF web服务有这个控制台主机 PROGRAM.CS class Program { static void Main(string[] args) { WebServiceHost Host = new WebServiceHost(typeof(Service1)); try { Host.Open();

我的WCF web服务有这个控制台主机 PROGRAM.CS

 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();
            }

        }    
这是主机的my app.config

<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> 

这是服务的Service1类

   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;
        }
    }  
公共类服务1:IService1
{
公共流HandleMessage(流请求)
{
StreamReader=新的StreamReader(请求);
string text=reader.ReadToEnd();
System.Text.asciencoding encoding=新的System.Text.asciencoding();
MemoryStream ms=新的MemoryStream(encoding.GetBytes(text));
WebOperationContext.Current.OutgoingResponse.ContentType=“text/html”;
string[]sites=text.Split('\n');
int y=站点长度;
int i;
对于(i=0;i
我的主机一开始运行就关闭了。我犯了什么错误?
提前感谢。

尝试使用WCF的跟踪选项。它将为您记录错误。您的应用程序将被提前终止。跟踪将帮助您确定问题。

您正试图在控制台中启动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>


服务主机与您的WCF服务在同一个项目中吗?我的try块中已经有了该主机。我认为如果它过早关闭,可能是因为app.config错误,或者
Main
的第一行出现未经处理的异常。无论哪种方式,控制台应用程序都应该发出异常信息。@Preben Huybrechts No我已经添加了一个名为console host的新项目,并将其设置为启动项目。@HackedByChinese Yaa,但它刚刚关闭,没有显示异常。