简体   繁体   English

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

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

I'm trying to build a test application to test wcf and learn more on it: 我正在尝试构建一个测试应用程序以测试wcf并了解更多信息:

I wrote a WCF service and hosted this in a windows service. 我编写了WCF服务,并将其托管在Windows服务中。 Here's my app.config from the windows service: 这是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>

Now I want to build a client app to consume the WCF service. 现在,我想构建一个客户端应用程序以使用WCF服务。 But when I go to Add Service Reference it gives me an error it can't reach the endpoint. 但是,当我转到“ Add Service Reference它给了我一个错误,它无法到达端点。 I can see the service running from service manager. 我可以从服务管理器看到服务正在运行。 I did a test by doing a Debug from VS. 我通过从VS进行调试进行了测试。 During the debug the client was able to see the endpoint. 在调试期间,客户端可以看到端点。 But if I stop and Debug go back to the service manager, the client can't see the endpoint. 但是,如果我停止调试并返回到服务管理器,则客户端将看不到端点。

What could be wrong? 有什么事吗 Any clues please? 有什么线索吗?

EDIT1 编辑1

Windows service ( Service1.cs ) 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 : 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
        }
    }
}

Nothing wrong with the code above. 上面的代码没有错。 The same code is working in my second laptop. 相同的代码正在第二台笔记本电脑上工作。 So something to do with the OS or IE on my first laptop. 因此,与第一台笔记本电脑上的OS或IE有关。 Will check that out. 会检查出来的。

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

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