简体   繁体   中英

WCF service not opening in the browser?

I'm learning WCF, I started creating a very basic host app which defines a class with one method as follows:

 [ServiceContract()]
    public interface IMath
    {
        [OperationContract]
        int Add(int a, int b);
    }
    public class MathCalcs : IMath
    {
        public MathCalcs()
        {
            Console.WriteLine("Service await two numbers...");
        }
        public int Add(int a, int b)
        {
            return a + b;
        }
    }

and that is how I configured the App.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="ConsoleHost.MathCalcs" behaviorConfiguration="MathServiceMEXBehavior">
        <endpoint address="http://localhost:8080/MathCalcs"
                  binding="basicHttpBinding"
                  contract="ConsoleHost.IMath"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/MathCalcs"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MathServiceMEXBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
</configuration>

Then I called the service from Main

 using (ServiceHost host = new ServiceHost(typeof(MathCalcs)))
            {
                host.Open();
                Console.WriteLine("***The service is ready***");
            }
            Console.ReadLine();

But it fails to view the metadata of the service through the URI http://localhost:8080/MathCalcs , I'm sure I'm following the steps right as the book I'm reading from and as a preceding example works fine, the only difference is that I didn't separate the service logic (the interface and the class) in a stand alone class library. What am I missing?

The following line of code

Console.ReadLine();

must be inside the braces of the using clause! When that is done, retry finding the WSDL metadata.

Here is a related post that I think may lead you in the right direction.

WCF service showing blank in browser

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