控制台应用程序中的主机服务

host service in console application

亲爱的,

我在控制台应用程序中托管 WCF 服务,但在浏览器中使用以下内容探索它时 url

http://localhost:8000/GettingStarted/CalculatorService 我得到错误

“服务不可用
HTTP - 错误 503。服务不可用。"

以下是托管服务的控制台应用程序代码

class Program
{
    static void Main(string[] args)
    {
        // Step1: Create URI to serve as the base address
        Uri baseAddress = new Uri("http://localhost:8000/GettingStarted");

        // Step2: Create service host instance
        ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

        try
        {
            // Step 3: Add service endpoint 
            selfHost.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "CalculatorService");

            // Enable Metadata exchange
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            // Starts the service
            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            // Close the ServiceHostBase to shutdown the service.
            selfHost.Close();

        }
        catch (CommunicationException ex)
        {
             Console.WriteLine("An exception occurred: {0}", ex.Message);
            selfHost.Abort();
        }
    }
}

这是 app.config 在役

<?xml version="1.0" encoding="utf-8" ?>
    <configuration>

      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" />
      </system.web>
      <!-- When deploying the service library project, the content of the config file must be added to the host's 
      app.config file. System.Configuration does not support config files for libraries. -->
      <system.serviceModel>
        <services>
          <service name="GettingStartedLib.CalculatorService">
            <host>
              <baseAddresses>
                <add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />
              </baseAddresses>
            </host>
            <!-- Service Endpoints -->
            <!-- Unless fully qualified, address is relative to base address supplied above -->
            <endpoint address="" binding="wsHttpBinding" contract="GettingStartedLib.ICalculator">
              <!-- 
                  Upon deployment, the following identity element should be removed or replaced to reflect the 
                  identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
                  automatically.
              -->
              <identity>
                <dns value="localhost"/>
              </identity>
            </endpoint>
            <!-- Metadata Endpoints -->
            <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
            <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, 
              set the values below to false before deployment -->
              <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
              <!-- To receive exception details in faults for debugging purposes, 
              set the value below to true.  Set to false before deployment 
              to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="False" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>

    </configuration>

所以问题是什么?

如果您想通过http访问网络服务描述语言(wsdl),您应该在浏览器地址栏中输入http元数据地址,而不是服务端点地址。

http://localhost:8000/GettingStarted

可以在HttpGetUrl中指定地址属性,默认为服务基地址

smb.HttpGetUrl = new Uri("http://localhost:9000");

如果有什么我可以帮忙的,请随时告诉我。