Failed to add a service. Service metadata may not be accessible. Make sure your service is running and exposing metadata.?

时光毁灭记忆、已成空白 提交于 2019-12-12 00:26:44

问题


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.

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

 [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:


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).

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



来源:https://stackoverflow.com/questions/11390594/failed-to-add-a-service-service-metadata-may-not-be-accessible-make-sure-your

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!