2012-05-29 64 views
1

TL劫持; DR - 我有[WebGet(UriTemplate = "/")]和它不工作根UriTemplate是越来越受WCF帮助页


我有WCF服务,看起来像这样:

public interface IUserService 
{ 
    // This doesn't work 
    [OperationContract] 
    [WebGet(UriTemplate = "/")] 
    IList<User> GetAllUsers(); 

    ///////////////////////////////////// 
    // Everything below this works 
    ///////////////////////////////////// 

    [OperationContract] 
    [WebGet(UriTemplate = "/{id}/")] 
    User GetUserById(string id); 

    [OperationContract] 
    [WebInvoke(UriTemplate = "/", Method = "POST")] 
    IList<User> AddUser(); 

    [OperationContract] 
    [WebInvoke(UriTemplate = "/{id}/", Method = "PUT")] 
    IList<User> UpdateUser(string id, User user); 
} 

这里是端点的配置

<service name="MyCompany.UserService"> 
    <host> 
     <baseAddresses> 
      <add baseAddress="http://localhost:80/api/users/" /> 
     </baseAddresses> 
    </host> 
    <endpoint address="" 
         behaviorConfiguration="WebHttpBehavior" 
         binding="webHttpBinding" 
         contract="MyCompany.IUserService" /> 
    <endpoint address="mex" 
         binding="mexHttpBinding" 
         contract="IMetadataExchange" /> 
    <endpoint address="soap" 
     binding="wsHttpBinding" 
     contract="MyCompany.IUserService" /> 
</service> 

因为你c一看,我从这项服务中为REST和SOAP提供服务。这个问题只涉及REST。

当我在我的浏览器中访问http://localhost:80/api/users/(WCF条款中的GET "/")时,我得到描述端点的WCF帮助页面 - 对SOAP有用但对REST没有太大帮助的端点。但是,如果我做了其他事情,它会按预期工作。如果我POST这个URL,或GET /123456,我得到正常的JSON响应(如,它实际上执行我的服务)。

看来,WCF被劫持的“/”运行。有什么办法可以关闭这个WCF“帮助”行为,以便我可以执行我的操作?任何建议,非常感谢。

回答

3

首先,你可以通过下面的配置选项来禁用该服务帮助页面:

<serviceBehaviors> 
    <serviceDebug httpHelpPageEnabled="false" httpsHelpPageEnabled="false" /> 
</serviceBehaviors> 

然而,这将使基址默认返回服务WSDL。所以,移动,你也可以使用这个配置选项:

<serviceBehaviors> 
    <serviceMetadata httpGetEnabled="true" httpGetUrl="wsdl"/> 
</serviceBehaviors> 

这将移动WSDL URL来your_service_base_address + “WSDL WSDL?”。