为WCF服务运行ServiceHost应用程序时出现异常 - 没有http基地址

时间:2017-12-08 21:07:22

标签: c# wcf

我正在尝试学习WCF服务。为此,我写了一个小应用程序。 我的服务主机代码是:

using System;
using System.Linq;

namespace ServiceHost
{
    public class ServiceHost<T> : System.ServiceModel.ServiceHost
    {
        public ServiceHost() : base(typeof(T))
        { }

        public ServiceHost(params string[] baseAddresses) : base(typeof(T),
        baseAddresses.Select(address => new Uri(address)).ToArray())
        { }
        public ServiceHost(params Uri[] baseAddresses) : base(typeof(T), baseAddresses)
        { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var serviceHost = new ServiceHost<CalculatorService.CalculatorService>();

            serviceHost.Open();
            Console.WriteLine("Press ANY key to exit");
            Console.ReadKey();
            serviceHost.Close();
        }
    }
}

我的app.config文件是:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <system.serviceModel>
    <services>
      <service name = "CalculatorService.CalculatorService" behaviorConfiguration = "MEXGET">
        <endpoint
      address  = "http://localhost:8101/CalculatorService"
      binding  = "wsHttpBinding"
      contract = "CalculatorService.ICalculatorService"
        />
    <endpoint
      address  = "net.tcp://localhost:8102/CalculatorService"
      binding  = "netTcpBinding"
      bindingConfiguration = "TransactionalTCP"
      contract = "CalculatorService.ICalculatorService"
    />
  </service>
</services>
<bindings>
  <netTcpBinding>
    <binding name = "TransactionalTCP"
             transactionFlow = "true"
    />
  </netTcpBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name = "MEXGET">
      <serviceMetadata httpGetEnabled = "true" httpGetUrl = "MyMEXAddress"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

当我尝试运行ServiceHost应用程序时,出现以下异常:

System.InvalidOperationException occurred
HResult=0x80131509
Message=The HttpGetEnabled property of ServiceMetadataBehavior is set to true and the HttpGetUrl property is a relative address, but there is no http base address.  Either supply an http base address or set HttpGetUrl to an absolute address.

当我已经在app.confg文件中设置地址时,我不明白为什么会出现此错误。

有什么想法吗?

由于

1 个答案:

答案 0 :(得分:0)

就像它出现在错误消息“... HttpGetUrl属性是一个相对地址,但没有http基地址”,你应该添加基地址。这将使MEX地址可达。您的app.config文件可能如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <system.serviceModel>
      <services>
        <service name = "CalculatorService.CalculatorService" behaviorConfiguration = "MEXGET">
          <endpoint
        address  = "CalculatorService"
        binding  = "wsHttpBinding"
        contract = "CalculatorService.ICalculatorService"
          />

          <endpoint
            address  = "net.tcp://localhost:8102/CalculatorService"
            binding  = "netTcpBinding"
            bindingConfiguration = "TransactionalTCP"
            contract = "CalculatorService.ICalculatorService"
          />

          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:8101"/>
            </baseAddresses>
          </host>

        </service>
      </services>
      <bindings>
        <netTcpBinding>
          <binding name = "TransactionalTCP"
                   transactionFlow = "true"
          />
        </netTcpBinding>
      </bindings>
      <behaviors>
        <serviceBehaviors>
          <behavior name = "MEXGET">            
            <serviceMetadata httpGetEnabled = "true" httpGetUrl = "CalculatorService"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
    </system.serviceModel>  
</configuration>