WCF Web服务关闭的控制台主机

时间:2012-06-13 11:30:40

标签: .net wcf web-services

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

        }    

这是我的主机

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

我的主机一开始运行就关闭了。我做错了什么? 提前谢谢。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

尝试使用WCF的Trace选项。它会为您记录错误。您的申请过早终止。 Trace将帮助您确定问题。

相关问题