2012-07-09 66 views
0

試圖在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; } 
    } 

服務1。 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"} 
      }; 
     } 
    } 
+0

您可以通過瀏覽瀏覽器中的URL(IE/chrome/firefox/etc ..)來測試您的服務是否正常工作。您需要使用的URL應該如下所示:http://localhost/ServiceApp/Service1.svc/XMLService/Employees,並且應該以xml格式返回2個員工詳細信息 – Rajesh 2012-07-09 11:44:42

回答

2

With WCF Restful servic e,你是否真的需要元數據來公開服務或者處理它?答案是不」。這違背了休息的原則。元數據表示接口(操作),並且對於REST接口是固定的(http方法)。 WcfTestClient用於測試基於SOAP的服務(因爲他們必須通過mex綁定公開他們的接口)。

測試HTTP GET的RESTFUL服務可能很容易。你只需要使用URL從你的瀏覽器調用它。要測試其他http方法,您必須構建自定義客戶端。 如果這看起來很重要,那麼你也可以使用像Fiddler這樣的工具來構建請求數據。可以看到一個例子here

相關問題