无法添加服务。服务元数据可能无法访问。确保您的服务正在运行并显示元数据。 [英] Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.?

查看:699
本文介绍了无法添加服务。服务元数据可能无法访问。确保您的服务正在运行并显示元数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

trying to build a RestFull service with wcf running in the WcfTestClient.exe. The problem is that I get an error:

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

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

I added a mex endpoint in the config file but does not solve it:

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

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

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"}
             };
        }
    }


推荐答案

WCF Restful服务,你真的需要元数据来公开服务或工作吗?答案是不。这违背了休息的原则。元数据表示接口(操作),而REST接口是固定的(http方法)。 WcfTestClient用于测试基于SOAP的服务(因为他们必须通过mex绑定暴露他们的接口)。

With WCF Restful service, do you actually need meta-data to expose service or to work on it? The answer is "NO". It's against the principles of Rest. Meta-data represents the interface(the operations), and for REST interface is fixed(http methods). WcfTestClient is for testing SOAP based Service(as they have to expose their interface through mex bindings).

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

Testing a RESTFUL service for http get could be vary easy. you just have to invoke it from your browser, using the URL. To test other http methods, you have to build your custom client. If this seems a big task, then you could also use tools like Fiddler to build request data. An example could be seen here

这篇关于无法添加服务。服务元数据可能无法访问。确保您的服务正在运行并显示元数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆