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

时间:2012-07-09 07:21:05

标签: wcf c#-4.0 rest wcftestclient

尝试使用在WcfTestClient.exe中运行的wcf构建RestFull服务。问题是我收到错误:

Failed to add a service. Service metadata may not be accessible.

我在配置文件中添加了一个mex端点,但没有解决它:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="MyRest.Service"  behaviorConfiguration="ServBehave">
        <!--Endpoint for REST-->
        <endpoint
          address="XMLService"
           binding="webHttpBinding"
           behaviorConfiguration="restPoxBehavior"
           contract="MyRest.IService"/>
        <endpoint
            address="mex"
            binding="mexHttpBinding"
            contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServBehave" >
          <!-- 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>
      <endpointBehaviors>
        <!--Behavior for the REST endpoint for Help enability-->
        <behavior name="restPoxBehavior">
          <webHttp helpEnabled="true"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

IService1.cs

 [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebGet(UriTemplate = "/Employees", ResponseFormat = WebMessageFormat.Xml)]
        Employee[] GetEmployees();

    }

    [DataContract]
    public class Employee
    {
        [DataMember]
        public int EmpNo { get; set; }
        [DataMember]
        public string EmpName { get; set; }
        [DataMember]
        public string DeptName { get; set; }
    }

Service1.cs

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class Service1 : IService1
    {
        public Employee[] GetEmployees()
        {
            return new Employee[] 
             {
                  new Employee() {EmpNo=101,EmpName="Mahesh",DeptName="CTD"},
                  new Employee() {EmpNo=102,EmpName="Akash",DeptName="HRD"}
             };
        }
    }

1 个答案:

答案 0 :(得分:2)

使用WCF Restful服务,您是否真的需要元数据来公开服务或对其进行处理?答案是不”。这违反了休息的原则。元数据表示接口(操作),而REST接口是固定的(http方法)。 WcfTestClient用于测试基于SOAP的服务(因为它们必须通过mex绑定公开它们的接口)。

为http get测试RESTFUL服务可能很容易。你只需要使用URL从浏览器调用它。要测试其他http方法,您必须构建自定义客户端。 如果这似乎是一项大任务,那么您也可以使用Fiddler等工具来构建请求数据。可以看到一个例子here