2011-05-23 153 views
4

我有一個不起作用的RESTful服務。當我在地址欄中輸入URI時,我收到此錯誤:RESTful WCF服務返回「無法找到資源。」錯誤

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /VirtualRUS/Service1.svc/

我不知道爲什麼它不工作。我將我的代碼和web.config與一個工作示例進行了比較,我找不到任何區別。

編輯:此外,我能夠使用服務就好了,如果我使用服務引用。如果我導航到URI,或者更重要的是,當我嘗試製作WebRequest時,它不起作用。

有沒有人有任何想法,爲什麼這不起作用?

Service1.cs:

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    [WebGet(UriTemplate = "/")] 
    string Test(); 
} 

Service1.svc.cs:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
public class Service1 : IService1 
{ 
    public string Test() 
    { 
     return "It works!!"; 
    } 
} 

的Web.Config:

<?xml version="1.0" ?> 
<configuration> 
    <configSections> 
     <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 
       <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> 
       <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> 
        <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" /> 
        <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> 
        <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> 
        <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> 
       </sectionGroup> 
      </sectionGroup> 
     </sectionGroup> 
    </configSections> 
    <appSettings></appSettings> 
    <connectionStrings> 
     <add name="ResearchLibrary" connectionString="Server=XXXXXX" ; Initial Catalog=ResearchLibrary; user id=XXX; password=XXX " /> 
    </connectionStrings> 
    <system.web> 
     <compilation debug="true "> 
      <assemblies> 
       <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089 "/> 
       <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
      </assemblies> 
     </compilation> 

     <authentication mode="Windows "/> 

     <pages> 
      <controls> 
       <add tagPrefix="asp " namespace="System.Web.UI " assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
      </controls> 
     </pages> 
     <httpHandlers> 
      <remove verb="* " path="*.asmx "/> 
      <add verb="* " path="*.asmx " validate="false " type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
      <add verb="* " path="*_AppService.axd " validate="false " type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
      <add verb="GET,HEAD " path="ScriptResource.axd " type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 " validate="false "/> 
     </httpHandlers> 
     <httpModules> 
      <add name="ScriptModule " type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
     </httpModules> 
     <httpRuntime maxRequestLength="65536 "/> 
    </system.web> 
    <system.codedom> 
     <compilers> 
      <compiler language="c#;cs;csharp " extension=".cs " warningLevel="4 " type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 "> 
       <providerOption name="CompilerVersion " value="v3.5 "/> 
       <providerOption name="WarnAsError " value="false "/> 
      </compiler> 
     </compilers> 
    </system.codedom> 
    <system.web.extensions> 
     <scripting> 
      <webServices> 
      </webServices> 
     </scripting> 
    </system.web.extensions> 

    <system.webServer> 
     <validation validateIntegratedModeConfiguration="false "/> 
     <modules> 
      <add name="ScriptModule " preCondition="integratedMode " type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
     </modules> 
     <handlers> 
      <remove name="WebServiceHandlerFactory-Integrated "/> 
      <add name="ScriptHandlerFactory " verb="* " path="*.asmx " preCondition="integratedMode " type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
      <add name="ScriptHandlerFactoryAppServices " verb="* " path="*_AppService.axd " preCondition="integratedMode " type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
      <add name="ScriptResource " preCondition="integratedMode " verb="GET,HEAD " path="ScriptResource.axd " type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35 "/> 
     </handlers> 
    </system.webServer> 
    <system.serviceModel> 
     <services> 
      <service name="ResearchUploadService.Service1 " behaviorConfiguration="ResearchUploadService.Service1Behavior "> 
       <!-- Service Endpoints --> 

      <endpoint address="customBinding " binding="customBinding " bindingConfiguration="basicConfig " contract="ResearchUploadService.IService1 "/> 
      </service> 
     </services> 
     <behaviors> 
      <serviceBehaviors> 
       <behavior name="ResearchUploadService.Service1Behavior "> 

        <serviceMetadata httpGetEnabled="true "/> 
        < 
        <serviceDebug includeExceptionDetailInFaults="true "/> 
       </behavior> 
      </serviceBehaviors> 
     </behaviors> 
     <bindings> 
     <customBinding> 
      <binding name="basicConfig "> 
       <binaryMessageEncoding/> 
       <httpTransport transferMode="Streamed " maxReceivedMessageSize="67108864 "/> 
      </binding> 
     </customBinding> 
     </bindings> 
    </system.serviceModel> 
</configuration> 
+0

你可以發佈Service1.svc和web.config的內容嗎? – carlosfigueira 2011-05-23 21:40:14

+0

我真的不知道如何發佈Service1.svc的內容。當我在Solution Explorer中雙擊它時,它會打開Service1.svc.cs。 – Jimmy 2011-05-23 22:00:22

+0

您可以通過右鍵點擊Service1.svc來查看內容並選擇「查看標記」 – carlosfigueira 2011-05-23 22:39:03

回答

9

你的終點是不一個REST端點,它是一個「普通的」SOAP端點(二進制編碼/ http傳輸)。 REST端點是使用特定綁定(webHttpBinding)和行爲(webHttp)定義的。此外,添加服務引用對於REST端點不起作用,這也表明如果它適合您,那麼您不使用REST端點。

要將您的端點更改爲RESTful,您可以更改web.config,如下所示(除了您已有的SOAP之外,它實際上還添加了REST端點)。

<system.serviceModel> 
    <services> 
     <service name="ResearchUploadService.Service1" behaviorConfiguration="ResearchUploadService.Service1Behavior"> 
      <!-- Service Endpoints --> 

     <endpoint address="customBinding" binding="customBinding" bindingConfiguration="basicConfig" contract="ResearchUploadService.IService1"/> 
     <endpoint address="" binding="webHttpBinding" behaviorConfiguration="REST" contract="ResearchUploadService.IService1"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
      <behavior name="ResearchUploadService.Service1Behavior"> 

       <serviceMetadata httpGetEnabled="true"/> 
       <serviceDebug includeExceptionDetailInFaults="true"/> 
      </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
      <behavior name="REST"> 
      <webHttp/> 
      </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <bindings> 
    <customBinding> 
     <binding name="basicConfig"> 
      <binaryMessageEncoding/> 
      <httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/> 
     </binding> 
    </customBinding> 

    </bindings> 
</system.serviceModel> 
+0

解決了它!非常感謝。 – Jimmy 2011-05-24 13:50:04

+0

嘿,我怎麼能給你10 upvotes :) ..謝謝你的解決方案 – 2015-07-11 22:27:17

+0

解決了它!非常感謝 – Rasel 2017-09-17 02:34:55