简体   繁体   中英

Console host for WCF web service shutting down

I have this console host for my WCF web service 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();
            }

        }    

This is my app.config for the host

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

This is Service1 class for the service

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

My host is shutting down as soon as it starts running. what mistake have I done? Thanks in advance.

You are trying to start a WebServiceHost in a console. You need to start a ServiceHost. That is also possibly why you are not getting an error message as WebServiceHost would not write to the console.

And you are not attaching the service to the port in the config. This is a console host that is working for me.

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

Try using Trace option of WCF. It will log the error for you. Your application is getting prematurely terminated. Trace will help you to identify the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM