简体   繁体   中英

RESTful WCF Service returns “The resource cannot be found.” error

I have a RESTful service that doesn't work. When I type the URI into the address bar, I receive this error:

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/

I can't tell why its not working. I compared my code and web.config to a working example, and I can't find any differences.

EDIT: Also, I'm able to use the service just fine if I use a service reference. It just doesn't work if I navigate to the URI, or more importantly, when I try to make a WebRequest .

Does anyone have any idea why this wouldn't work?

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>

Your endpoint isn't a REST endpoint, it's a "normal" SOAP endpoint (binary encoding / http transport). REST endpoints are defined with a specific binding (webHttpBinding) and behavior (webHttp). Also, Add Service Reference doesn't work for REST endpoints, which is also an indication that if it works for you, then you're not using a REST endpoint.

To change your endpoint to be RESTful, you can change the web.config as shown below (it actually adds a REST endpoint in addition to the SOAP one which you already have).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM