5

using (ServiceHost host = new ServiceHost(typeof(HelloService.HelloService)))请帮助在下面的代码中获取异常

例外:只有绝对 URI 可以用作基地址

WCF 主机应用程序

    class Program
    {
        static void Main()
        {
            using (ServiceHost host = new ServiceHost(typeof(HelloService.HelloService)))
            {
                host.Open();
                Console.WriteLine("Service Started");
                Console.ReadLine();
            }
        }
    }

合同执行

    public class HelloService : IHelloService
    {
        public string GetMessage(string Name)
        {
            return "Hello" + Name;
        }
    }

合同

[ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        string GetMessage(string Name);
    }

应用程序配置

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService" behaviorConfiguration="mexBehaviour">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService">
        </endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/HelloService"/>
            <add baseAddress="net.tcp//localhost:8090/HelloService"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
4

3 回答 3

5

我相信您缺少冒号(:):

<add baseAddress="net.tcp//localhost:8090/HelloService"/>

应该

<add baseAddress="net.tcp://localhost:8090/HelloService"/>
于 2015-06-12T21:06:15.950 回答
1
<endpoint address="HelloService"...

应该

<endpoint address="/HelloService"...

请参阅https://msdn.microsoft.com/en-us/library/ms733749(v=vs.110).aspx

于 2015-06-12T19:04:44.867 回答
0

简而言之,这是与基地址相关的错误。所以,请检查您是否正确输入了基地址。

错误:

<add baseAddress=" &quot;http://localhost:8732/Design_Time_Addresses/WcfServiceLibraryShop/IStudent/&quot;&gt;" />

解决了:

<add baseAddress=" http://localhost:8732/Design_Time_Addresses/WcfServiceLibraryShop/IStudent/" />
于 2018-10-04T19:45:49.780 回答