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

时间:2019-02-17 06:59:22

标签: c# web-services wcf

亲爱的

我在控制台应用程序中托管WCF服务,但是在浏览器中使用以下网址进行浏览时

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>

那是什么问题?

1 个答案:

答案 0 :(得分:1)

如果要通过http访问Web服务描述语言(wsdl),则应在浏览器地址栏中键入http元数据地址,而不是服务端点地址。

http://localhost:8000/GettingStarted

您可以在HttpGetUrl属性中指定地址,默认情况下其值为服务基址。

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

请随时告诉我是否有什么可以帮忙的。