WCF REST服务不起作用

时间:2015-06-23 05:47:56

标签: c# asp.net web-services wcf

我创建了简单的WCF Web服务项目,并在其上启用了soap和rest服务。

soap服务工作正常,但我无法使用其余服务和帮助页面。

我是这样做的:

  1. 我的网络服务类:

    [OperationContract]
    [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json,
        UriTemplate = "/InsertWithDate/personTypeName({personTypeName})/accessName({accessName})/firstName({firstName})/lastName({lastName})/nationalID({nationalID})/email({email})/userName({userName})/passwordHash({passwordHash})/genderName({genderName})/statusName({statusName})")]
    [Description("Description for GET /InsertWithDate")]
    string InsertWithDate(string personTypeName, string accessName, string firstName, string lastName, string nationalID, string email, string userName, string passwordHash, string genderName, string statusName);
    
  2. 我的web.config文件:

    <?xml version="1.0"?>
    <configuration>
       <appSettings>
          <add key="aspnet:UseTaskFriendlySynchronizationContext" 
               value="true" />
           <!-- (Log4net diagnostics) -->
           <add key="log4net.Internal.Debug" value="true"/>
       </appSettings>
       <system.web>
          <compilation debug="true" targetFramework="4.5" />
          <httpRuntime targetFramework="4.5"/>
       </system.web>
       <system.serviceModel>
           <!-- Add Rest service -->
           <services>
               <service name="WcfWebService.TestWS" 
                        behaviorConfiguration="ServiceBehavior">
               <!-- Use a bindingNamespace to eliminate tempuri.org. 
               bindingNamespace="http://contoso.com/services" -->
                  <endpoint 
                      address="soap" 
                      binding="basicHttpBinding" 
                      contract="WcfWebService.ITestWS"/>
                  <endpoint 
                      address="rest" 
                      binding="webHttpBinding" 
                      behaviorConfiguration="http" 
                      contract="WcfWebService.ITestWS"/>
                  <endpoint 
                      address="mex" 
                      binding="mexHttpBinding" 
                      contract="IMetadataExchange" />
               </service>
           </services>
           <behaviors>
              <serviceBehaviors>
              <!-- Web service behavior (All kind of web Services hast it) -->
                 <behavior name="ServiceBehavior">
                     <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                     <serviceDebug includeExceptionDetailInFaults="true"/>
                 </behavior>               
              </serviceBehaviors>
              <!-- Rest service behavier) -->
              <endpointBehaviors>
                 <behavior name="http">
                    <webHttp/>
                 </behavior>
              </endpointBehaviors>
          </behaviors>
          <protocolMapping>
              <add binding="basicHttpsBinding" scheme="https" />
          </protocolMapping>    
          <serviceHostingEnvironment aspNetCompatibilityEnabled="true" 
                         multipleSiteBindingsEnabled="true" />
          <standardEndpoints>
              <webHttpEndpoint>
                  <standardEndpoint name="" helpEnabled="true"  
                           automaticFormatSelectionEnabled="true"/>
              </webHttpEndpoint>
          </standardEndpoints>
      </system.serviceModel>
      <system.webServer>
          <modules runAllManagedModulesForAllRequests="true"/>
          <directoryBrowse enabled="true"/>
       </system.webServer>
    </configuration>
    
  3. 现在,当我尝试这些页面时:

    1. 本地主机:51850 / TestWS.svc /帮助
    2. 本地主机:51850 / TestWS.svc / InsertWithDate /帮助
    3. 本地主机:51850 / TestWS.svc / InsertWithDate / personTypeName(10)/访问名(10)/的firstName(MN)/ lastName的(MN)/ nationalID(12)/电子邮件(a@asd.com)/用户名(nvcvncm)/ passwordHash(123)/ genderName(男性)/ statusName(d)
    4. 出现相同的页面:&#39; /&#39;中的服务器错误应用

      谢谢。

      更新: 没有错误:只是这个页面: enter image description here

1 个答案:

答案 0 :(得分:0)

抱歉,我的错:我完全忘记了我的REST服务地址:休息 并且为了启用帮助页面,我只需将<webHttp helpEnabled="true"/>添加到我的web.conf文件中。