【问题标题】:How to expose simple console host web service?如何公开简单的控制台主机 Web 服务?
【发布时间】:2015-08-22 14:46:35
【问题描述】:

我需要编写不会由 iis 托管的简单 Web 服务 => 所以我使用简单的控制台应用程序作为主机。

我创建了 2 个项目

1) 包含 IService.cd 和 Service.cs 的 dll
2)将主机的控制台应用程序。

我剩下的所有步骤都和here一样

但是当我运行我的应用程序时——当我将主机定义为 IIS 时,我无法看到来自其他机器的服务。

如何解决?

配置文件:

<configuration>
  <system.serviceModel>
    <serviceHostingEnvironment  aspNetCompatibilityEnabled="True"/>
    <services>
      <service name="CalcService.Calc" behaviorConfiguration="NewBehavior0">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9003/Calc"/>
            <add baseAddress="net.tcp://localhost:9002/Calc"/>
          </baseAddresses>
        </host>
        <endpoint address="http://localhost:9003/Calc" binding="basicHttpBinding" contract="CalcService.ICalc"/>
        <!--<endpoint address="net.tcp://localhost:9002/Calc" binding="netTcpBinding" contract="CalcService.ICalc"/>-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="NewBehavior0">
          <serviceMetadata httpGetEnabled="true"/> 
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
static void Main(string[] args)
    {
        ServiceHost svcHost = null;
        try
        {
            svcHost = new ServiceHost(typeof(CalcService.Calc));
            svcHost.Open();
            Console.WriteLine("\n\nService is Running  at following address");

            Console.WriteLine("\nhttp://localhost:9001/Calc");
            Console.WriteLine("\nnet.tcp://localhost:9002/Calc");
        }
        catch(Exception eX)
        {
            svcHost = null;
            Console.WriteLine("Service can not be started \n\nError Message [" + eX.Message + "]");
        }

        if(svcHost != null)
        {
            Console.WriteLine("\nPress any key to close the Service");
            Console.ReadKey();
            svcHost.Close();
            svcHost = null;
        }       
    }

【问题讨论】:

  • 显示有关您如何托管服务的代码。
  • 添加主机服务-嵌入式

标签: c# asp.net wcf iis


【解决方案1】:

我建议您需要将所有配置转移到您的控制台应用程序所在的 app.config 中。

您可以考虑以下链接来检查您的服务是否正常运行。

对于添加多个端点,您可以从MSDN 访问此链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    相关资源
    最近更新 更多