WCF 错误:相对端点地址

Posted

技术标签:

【中文标题】WCF 错误:相对端点地址【英文标题】:WCF Error : Relative end point addresses 【发布时间】:2013-03-18 21:00:30 【问题描述】:

嗯,对 WCF 很陌生。我想我有点搞砸了。所以这就是我到目前为止所做的,我已经在 IIS 中托管了我的 WCF 服务

首先是合同

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using YangkeeServer.dto;

namespace YangkeeServer
   

public class Service1 : IService1

    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "single")]
    public YangkeeTrailerDTO getTrailor()
    
        return new YangkeeTrailerDTO()
        
            loanFrom = "heaven"
        ;
    


    [WebInvoke(Method = "GET",
       ResponseFormat = WebMessageFormat.Json,
       UriTemplate = "datum/id")]
    public Test getName(string id)
    
        return new Test()
        
            Id = Convert.ToInt32(id) * 12,
            Name = "Leo Messi"
        ;
    


这是我的 web.config 文件

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

  </system.web>
  <system.serviceModel>

    <services>

      <service name="YangkeeServer.Service1">

        <endpoint
          contract="YangkeeServer.IService1"
         binding="webHttpBinding"
         behaviorConfiguration="WebHttp"
         address="http://localhost:7000/Service1.svc">
       </endpoint>

     </service>

      <service name="YangkeeServer.Service2">

       <endpoint
          contract="YangkeeServer.IService2"
         binding="webHttpBinding"
         behaviorConfiguration="WebHttp"
         address="http://localhost:7000/Service2.svc">
       </endpoint>

     </service>

      <service name="YangkeeServer.YangkeeTrailer">

        <endpoint
          contract="YangkeeServer.IYangkeeTrailor"
          binding="webHttpBinding"
          behaviorConfiguration="WebHttp"
         address="http://localhost:7000/YangkeeTrailor.svc">
        </endpoint>

      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
       <behavior name="WebHttp">
         <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
   <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
 </system.serviceModel>
 <system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/>
 </system.webServer>

 </configuration>

然后使用这个 urlhttp://localhost:7000/Service1.svc 并且遇到这个错误

Server Error in '/' Application.

When 'system.serviceModel/serviceHostingEnvironment/multipleSiteBindingsEnabled' is set to true     in configuration, the endpoints are required to specify a relative address. If you are specifying a     relative listen URI on the endpoint, then the address can be absolute. To fix this problem, specify a relative uri for endpoint 'http://localhost:7000/Service1.svc'.

谁能告诉我我哪里做错了?提前谢谢你。

【问题讨论】:

【参考方案1】:

这对我有用:

1.在服务中插入以下行:

 <host>
      <baseAddresses>
        <add baseAddress="http://localhost:7000/Service1.svc" />
      </baseAddresses>
    </host>

2.设置地址值如下:

address=""

完整代码:

<service name="YangkeeServer.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:7000/Service1.svc" />
          </baseAddresses>
        </host>
        <endpoint
           contract="YangkeeServer.IService1"
          binding="webHttpBinding"
          behaviorConfiguration="WebHttp"
          address="">
        </endpoint>
      </service>

【讨论】:

【参考方案2】:

为了修复这个错误,我添加了... listenUri="/" ... 到服务端点。

例子:

<service name="_name_">
  <endpoint address="http://localhost:_port_/.../_servicename_.svc"
    name="_servicename_Endpoint" 
    binding="basicHttpBinding"
    bindingConfiguration="GenericServiceBinding"
    contract="_contractpath_._contractname_"
    listenUri="/" />
</service>

MSDN: Multiple Endpoints at a Single ListenUri

【讨论】:

【参考方案3】:

将服务端点地址设置为服务文件名(这是相对部分):

address="Service1.svc"

或将每个端点的地址设置为空白(我认为在开发环境或 IIS 中托管时不需要它)

address=""

见this answer to another question

相关部分是:

*.svc 文件所在的虚拟目录(在 IIS 中)定义了服务端点的地址。

【讨论】:

感谢设置地址=""的提示,只有在添加多个 IIS 主机名绑定后我才能让它工作。干杯。

以上是关于WCF 错误:相对端点地址的主要内容,如果未能解决你的问题,请参考以下文章

WCF 错误“找不到与具有绑定 NetTcpBinding 的端点的方案 net.tcp 匹配的基地址”

WCF 多端点

WCF命名管道另一个端点错误但没有其他端点?

WCF 多重绑定 - 错误:没有端点监听

WCF 服务错误没有在 HTTPS 上侦听的端点

WCF 错误 - 找不到引用合同“UserService.UserService”的默认端点元素