如何使用基本WCF服务独立

时间:2016-06-08 19:45:08

标签: .net wcf

我已经在这个searchkick教程中成功实现了这个示例,当从Visual Studio中运行时,一切正常。

然而,当我从命令行开始在VS之外自行运行控制台主机应用程序时 - 代码就像一切正常,但在指定的地址没有http服务,就像它是从VS内部开始。

我在这里缺少什么?显然VS正在做一些事情来建立我没有看到的必要基础设施 - 这是什么?

编辑:来自Main()的代码:

static void Main(string[] args)
{
    // Step 1 Create a URI to serve as the base address.
    Uri baseAddress = new Uri("http://localhost:8080/GettingStarted/");

    // Step 2 Create a ServiceHost instance
    ServiceHost selfHost = new ServiceHost(typeof(CalculatorService), baseAddress);

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

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

        // Step 5 Start 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 ce)
    {
        Console.WriteLine("An exception occurred: {0}", ce.Message);
        Console.WriteLine();
        Console.WriteLine("Press <ENTER> to terminate service.");
        Console.ReadLine();
        selfHost.Abort();
    }
}

我的app.config来自 GettingStartedHost

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
  </startup>
</configuration>

我的App.config来自 GettingStartedLib

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <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.Service1">-->
      <service name="GettingStartedLib.CalculatorService">
        <host>
          <baseAddresses>
            <!--<add baseAddress="http://localhost:8733/Design_Time_Addresses/GettingStartedLib/Service1/"/>-->
            <add baseAddress="http://localhost:8080/GettingStarted/CalculatorService/"/>
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <!--<endpoint address="" binding="basicHttpBinding" contract="GettingStartedLib.IService1">-->
        <endpoint address="http://localhost:8080/GettingStarted/CalculatorService/" binding="basicHttpBinding" 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 value below to false before deployment -->
          <serviceMetadata httpGetEnabled="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>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
  </startup>
</configuration>

0 个答案:

没有答案