繁体   English   中英

使用Windows Service的WCF托管,客户端无法看到终结点

[英]WCF hosting with windows Service, client cannot see endpoint

我正在尝试构建一个测试应用程序以测试wcf并了解更多信息:

我编写了WCF服务,并将其托管在Windows服务中。 这是Windows服务中的我的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>    
     <services>
        <service name="WCFLibrary.CalculatorService"
                 behaviorConfiguration="CalculatorServiceBehavior">
           <host>
              <baseAddresses>
                 <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
              </baseAddresses>
           </host>
           <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service  -->
           <endpoint 
               address=""
               binding="wsHttpBinding"
               contract="WCFLibrary.ICalculator" />
           <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
           <endpoint 
               address="mex"
               binding="mexHttpBinding"
               contract="IMetadataExchange" />
         </service>
      </services>
      <behaviors>
         <serviceBehaviors>
            <behavior name="CalculatorServiceBehavior">
               <serviceMetadata httpGetEnabled="true"/>
               <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
         </serviceBehaviors>
      </behaviors>
   </system.serviceModel>
</configuration>

现在,我想构建一个客户端应用程序以使用WCF服务。 但是,当我转到“ Add Service Reference它给了我一个错误,它无法到达端点。 我可以从服务管理器看到服务正在运行。 我通过从VS进行调试进行了测试。 在调试期间,客户端可以看到端点。 但是,如果我停止调试并返回到服务管理器,则客户端将看不到端点。

有什么事吗 有什么线索吗?

编辑1

Windows服务Service1.cs

namespace WindowsService
{
   public partial class Service1 : ServiceBase
   {
        public ServiceHost serviceHost = null;

        public Service1()
        {
             InitializeComponent();
        }

        public void onDebug()
        {
            OnStart(null);
        }

        protected override void OnStart(string[] args)
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            serviceHost = new ServiceHost(typeof(CalculatorService));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            serviceHost.Open();
        }

        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                 serviceHost.Close();
                 serviceHost = null;
            }
        }
    }
}

Program.cs

namespace WindowsService
{
    public  class CalculatorWindowsService :ServiceBase
    {
        public ServiceHost serviceHost = null;

        public CalculatorWindowsService()
        {
            // Name the Windows Service
            ServiceName = "WCFWindowsServiceSample";
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static void Main()
        {
//#if DEBUG
//           Service1 myservice = new Service1();
//           myservice.onDebug();
//           System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
//#else
            //  CalculatorWindowsService calc = new CalculatorWindowsService();
            ServiceBase.Run(new CalculatorWindowsService());
//#endif
        }
    }
}

上面的代码没有错。 相同的代码正在第二台笔记本电脑上工作。 因此,与第一台笔记本电脑上的OS或IE有关。 会检查出来的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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