WCF Restful Service: - 无法添加服务。可能无法访问服务元数据。确保您的服务正在运行并公开元数据

时间:2016-05-25 08:26:28

标签: wcf

错误是

在ctrl + F5上我收到以下错误。

  

无法添加服务。可能无法访问服务元数据。确保您的服务正在运行并公开元数据。

这是我的界面声明

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

namespace DealerAuditWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IDealerAuditService" in both code and config file together.
    [ServiceContract]
    public interface IDealerAuditService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/GetDealerbyRegId/{regId}", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        Registration GetDealerbyRegId(string regId);

        //[OperationContract]
        //List<Registration> GetAllDealers();

        //[OperationContract]
        //void SaveDealerReg(List<Registration> regDetails);

    }
}

这是接口实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.Entity;

namespace DealerAuditWCFService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "DealerAuditService" in code, svc and config file together.
    public class DealerAuditService : IDealerAuditService
    {
        public Registration GetDealerbyRegId(string regId)
        {
            using (DealerAuditEntities dealerAudit = new DealerAuditEntities())
            {
                Registration regDetails = new Registration();
                var reg = (from r in dealerAudit.tblRegistrationDetails
                              where r.regId == regId
                              select r).First();

                regDetails.regId = reg.regId;
                regDetails.Location = reg.Location;
                regDetails.Region = reg.Region;
                regDetails.typeOfAudit = reg.typeOfAudit;
                return regDetails;
            }
        }
    }
}

数据成员的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;

namespace DealerAuditWCFService
{
    [DataContract]
    public class Registration
    {
        [DataMember]
        public string regId { get; set; }

        [DataMember]
        public string channelPartnerName { get; set; }

        [DataMember]
        public string Location { get; set; }

        [DataMember]
        public string Region { get; set; }

        [DataMember]
        public string typeOfAudit { get; set; }
    }
}

这是我的web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
  <system.serviceModel>
    <behaviors>
      
      
      <serviceBehaviors>
        <behavior name="metadataBehavior" >
          <serviceMetadata httpGetEnabled="True"/>
        <!--<behavior name="servicebehavior">-->
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true" />
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      
      
    </behaviors>
    <services>
      <service name="DealerAuditWCFService.DealerAuditService" behaviorConfiguration="metadataBehavior">
        <endpoint address="" binding="basicHttpBinding" contract="DealerAuditWCFService.IDealerAuditService" behaviorConfiguration="DealerAuditWCFService.DealerAuditService"/>

        <endpoint
            address="mex"
            binding="mexHttpBinding"
            contract="IMetadataExchange"/>
      </service>
    </services>

    
    
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <connectionStrings>
    <add name="DealerAuditEntities" connectionString="metadata=res://*/DealerAuditEntityDataModel.csdl|res://*/DealerAuditEntityDataModel.ssdl|res://*/DealerAuditEntityDataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=192.168.8.122;initial catalog=Beat_plan_db;persist security info=True;user id=sa;password=sa#122;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

由于我是WCF的新手,所以试图通过REST服务调用示例方法。我继续得到同样的错误。请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

首先,您需要决定是否要公开REST或SOAP端点。 decision is complex而不是真正的问题。

对于REST,您需要使用webHttpBinding for SOAP,basicHttpBinding或wsHttpBinding。

但是,要生成元数据(至少是代理生成器通过visual studio支持的类型添加服务引用功能),you are limited到SOAP端点。

要使用REST端点,您需要为服务建模相同的DTO(或者只使用定义它们的相同程序集),然后使用HttpClient或WebClient(两者都有advantages)。