简体   繁体   English

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

[英]Exception while running ServiceHost app for WCF service - no http base address

I am trying to learn WCF services. 我正在尝试学习WCF服务。 And for that purpose, I wrote small app. 为此,我编写了一个小型应用程序。 My service host code is: 我的服务主机代码是:

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();
        }
    }
}

My app.config file is: 我的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>

When I try to run the ServiceHost app, I get the following exception: 当我尝试运行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.

I don't understand why I am getting this error when I have already set the address in app.confg file. 我不明白为什么已经在app.confg文件中设置了地址,却收到此错误。

Any ideas ? 有任何想法吗 ?

Thanks 谢谢

Like it appears in the error message "...HttpGetUrl property is a relative address, but there is no http base address" you should add the base address. 就像错误消息中显示的那样:“ ... HttpGetUrl属性是相对地址,但是没有http基址”,您应该添加基址。 This will make the MEX address reachable. 这将使MEX地址可访问。 Your app.config file can look like this: 您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM