1

I developed a WCF service application and deployed it to IIS 8.

With a browser, when I go to http://localhost:6000/CustomService.svc, it shows "You have created a service" and other information. This means the service is successfully deployed.

But when I go to http://localhost:6000/testservice/date/2016/12/1, it showed HTTP 404 Not Found.

Here is service contract:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    using System.ServiceModel.Web;

    namespace WCF
    {
        [ServiceContract]
        public interface ICustomService
        {
            [WebGet(UriTemplate = "date/{year}/{month}/{day}", ResponseFormat = WebMessageFormat.Xml)]
            [OperationContract]
            string GetDate(string day, string month, string year); 
        }
    }

Here is implemented class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCF
{
    public class CustomService : ICustomService
    {
        public string GetDate(string day, string month, string year)
        {
            return new DateTime(Convert.ToInt32(year), Convert.ToInt32(month), Convert.ToInt32(day)).ToString("dddd, MMMM dd, yyyy");
        }
    }
}

Here is Web.config:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="CustomServiceBehavior" name="WCF.CustomService">
        <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="WCF.ICustomService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6000/testservice" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="CustomServiceBehavior">
          <!-- 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 name="webBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

Where could the problem be? I basically copied most of the stuff from https://weblogs.asp.net/kiyoshi/wcf-using-webhttpbinding-for-rest-services.

user3573403
  • 1,780
  • 5
  • 38
  • 64

2 Answers2

1

It worked with no problem for me.

enter image description here

using this URL:

http://localhost/WcfService1/Service1.svc/date/2017/1/31

and this config file:

 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="NewBinding0">
          <security>
            <message clientCredentialType="UserName" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="" behaviorConfiguration="NewBehavior0" binding="webHttpBinding"
          bindingConfiguration="" contract="WcfService1.IService1" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="NewBehavior0">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

Go to IIS Manager right-click your .SVC file, select browse and make sure you have the correct base address in your config file. Your base address looks more like an IIS Express address.

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
  • What would be my URL in my case? You are using "WcfService1" which is what? Can your example uses the same namespace and class names as mine? – user3573403 Jan 20 '17 at 03:59
  • Pls see my last comment. – jsanalytics Jan 20 '17 at 04:00
  • When I browse the the .SVC file, it opens up as http://localhost:6000/CustomService.svc on the browser. It is always "localhost", even when I run "Start Debugging" on Visual Studio. – user3573403 Jan 20 '17 at 04:05
  • Ok, note that this base address does not match the one you posted in your question. – jsanalytics Jan 20 '17 at 04:06
  • Is it `/testservice` or `/CustomerService.svc` ? – jsanalytics Jan 20 '17 at 04:09
  • Did you try in your dev machine or in the server you deployed? – jsanalytics Jan 20 '17 at 04:16
  • What should be the correct url to call my service method? – user3573403 Jan 20 '17 at 04:16
  • Should be whatever you see when you go to IIS Manager and browse your SVC file. – jsanalytics Jan 20 '17 at 04:17
  • When I browse svc file in browser, it doesn't say how to call my method. Why don't you take my codes and try it? Don't change anything at all and tell me if it works. – user3573403 Jan 20 '17 at 04:20
  • The url to call your method can only be one of two things: 1) http://localhost:6000/testservice/date/2017/1/31 or 2) http://localhost:6000/CustomService.svc/date/2017/1/31. – jsanalytics Jan 20 '17 at 04:23
  • I did use your code, just the project name is not the same. – jsanalytics Jan 20 '17 at 04:25
  • Note that my config file does not explicitly set the base address and sets the contract. Yours does exactly the opposite. – jsanalytics Jan 20 '17 at 04:30
  • I'm merely following the example from https://weblogs.asp.net/kiyoshi/wcf-using-webhttpbinding-for-rest-services . Anyway, I removed the ... part from Web.config, and then run it again from Visual Studio. Again, it is returning 404 when I tried to access http://localhost:57371/testservice/date/12/1/1. – user3573403 Jan 20 '17 at 05:02
  • Hi jstreet, I just tried with http://localhost:6000/CustomService.svc/date/2017/1/31 and it works. Not sure why it didn't work earlier. I must have entered something wrongly earlier. – user3573403 Jan 20 '17 at 07:33
  • But it doesn't work with http://localhost:6000/testservice/date/2017/1/31. Does it mean that I can't change the "CustomService.svc" part of the URL to something else? – user3573403 Jan 20 '17 at 07:40
  • You can sure change it to something else if you change it in your project, re-compile and re-deploy it, so everything is consistent. – jsanalytics Jan 20 '17 at 12:20
  • But it seems that we can't really get rid of the ".svc" in the URL even if I change the service file to some other name. – user3573403 Jan 20 '17 at 12:30
  • Take a look [HERE](http://stackoverflow.com/questions/355165/how-to-remove-the-svc-extension-in-restful-wcf-service). – jsanalytics Jan 20 '17 at 12:35
0

Try using the WCF test client that comes baked into Visual Studio. It's located here: C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\WcfTestClient.exe. Open the test client, add the service and make your call.

You can use fiddler or some other network request capturing tool to see what URL is being requested by your service. Hopefully that should allow you to troubleshoot further.

Here is the MSDN for the WCF test client. https://msdn.microsoft.com/en-us/library/bb552364(v=vs.110).aspx

Brendan Long
  • 276
  • 1
  • 6
  • I don't have IIS on my development laptop. I deployed it to a server with IIS, and on that server, there is no Visual Studio. – user3573403 Jan 20 '17 at 03:39
  • You can use IIS express. That comes with visual studio. There isn't a start or run option when your project is "Set as startup project"? – Brendan Long Jan 20 '17 at 03:42
  • I can run "Start Debugging" from my development laptop. A browser will open with http://localhost:57371/, which shows the directory of my development files. Then what next? When I go to http://localhost:57371/testservice/date/12/1/1, it is also showing me HTTP 404. – user3573403 Jan 20 '17 at 03:48
  • Take localhost:57371/whateverthepathistoyourservice.svc and put it into the wcf client – Brendan Long Jan 20 '17 at 03:49
  • I ran WCFTestClient, then in File -> Add Service, I entered my service http://localhost:57371/CustomService.svc. Then what next? In the program left pane, it is just showing "My Service Projects", and on the right pane it is the "Start Page". – user3573403 Jan 20 '17 at 03:54
  • WCFTestClient can't be used to test REST, only SOAP. It looks like OP is trying to do RESTful WCF. – Tim Jan 20 '17 at 03:58
  • Where does it say that? I'm pretty sure it's just a missing config option, but I don't know what config he is missing. – Brendan Long Jan 20 '17 at 04:06
  • Where in my codes does it indicates RESTful? Does it mean I can't call my service from a browser if it is RESTful? – user3573403 Jan 20 '17 at 04:07
  • He is probably referring to the WebGet attribute you have on your GetDate method on the interface. – Brendan Long Jan 20 '17 at 04:08
  • Try adding this line below your other endpoint entry (right above host) – Brendan Long Jan 20 '17 at 04:21
  • Hi Brendan, I added the line as you suggested, so now I have two lines: my original one with binding="webHttpBinding", and yours with binding="basicHttpBinding". I then started debugging from Visual Browser, and in the launched browser, I entered http://localhost:57371/testservice/date/2016/12/1 and again I got HTTP 404 Not Found. – user3573403 Jan 20 '17 at 05:35
  • The config setting I mentioned was just to help debug from the WCF test client. (I'm not 100% sure it will work). – Brendan Long Jan 20 '17 at 05:43