WCF服务主机找不到任何服务元数据。请检查元数据是否已启用

时间:2018-05-17 14:16:24

标签: wcf wcf-data-services wcf-ria-services wcf-binding wcf-security

我的App.config文件是

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="WcfJsonRestService.Service1">
        <endpoint address="http://localhost:8733/service1" 
                  binding="webHttpBinding" 
                  contract="WcfJsonRestService.IService1"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>

我的service1.cs代码如下

using System;
using System.ServiceModel.Web;

namespace WcfJsonRestService
{
    public class Service1 : IService1
    {
        [WebInvoke(Method = "GET", 
                    ResponseFormat = WebMessageFormat.Json, 
                    UriTemplate = "data/{id}")]
        public Person GetData(string id)
        {
            // lookup person with the requested id 
            return new Person()
                       {
                           Id = Convert.ToInt32(id), 
                           Name = "Leo Messi"
                       };
        }
    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

最初这是问题

WCF服务主机配置 - 请尝试将HTTP端口更改为8733

所以我跟着在CMD中执行以下代码

netsh http add urlacl url=http://+:8733/ user=WORK\Clara

执行此代码后,我面临新的错误,如enter image description here

所示

如何解决此问题?

我也试过通过下面的链接更新App.Config,但之后我又收到了一些错误

WCF service host cannot find any service metadata

1 个答案:

答案 0 :(得分:2)

您缺少服务元数据行为配置。请添加以下配置:

<configuration>
<system.serviceModel>
<services>
  <service name="WcfJsonRestService.Service1">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8733"/>
      </baseAddresses>
    </host>
    <endpoint address="service1"
              binding="webHttpBinding"
              contract="WcfJsonRestService.IService1"/>
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior>
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled ="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>