只有绝对URI可以用作基址

时间:2015-06-12 18:32:06

标签: c# asp.net .net wcf contracts

请在以下代码

中的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);
    }

App.Config中

<?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>

3 个答案:

答案 0 :(得分:5)

我相信你错过了冒号(:):

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

应该是

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

答案 1 :(得分:1)

<endpoint address="HelloService"...

应该是

<endpoint address="/HelloService"...

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

答案 2 :(得分:0)

简而言之,这是与基址有关的错误。因此,请检查您是否正确输入了基址。

错误:

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

已解决:

<add baseAddress=" http://localhost:8732/Design_Time_Addresses/WcfServiceLibraryShop/IStudent/" />