简体   繁体   中英

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:

I wrote a WCF service and hosted this in a windows service. Here's my app.config from the windows service:

<?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. But when I go to Add Service Reference it gives me an error it can't reach the endpoint. I can see the service running from service manager. I did a test by doing a Debug from 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

Windows service ( 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
        }
    }
}

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. Will check that out.

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