1

If I try to hit a method using jQuery or via browser, I get a 404. My configuration looks like the following:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior name="RegistrationBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="RegistrationEndpointBehavior">
                <enableWebScript/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
        <service name="Corp.EmployeeService" behaviorConfiguration="RegistrationBehavior">
            <endpoint address="" binding="webHttpBinding" contract="Corp.IEmployeeService" behaviorConfiguration="RegistrationEndpointBehavior" />
        </service>
    </services>
</system.serviceModel>

I believe it has something to do with the configuration as my method is decorated with an OperationContract and a WebInvoke attribute.

My interface (simplified for posting) looks similar to the following:

namespace Corp
{
    [ServiceContract]
    public interface IEmployeeService
    {
        [OperationContract]
        [WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "POST")]
        int Register(string username);
    }
}

On the service class itself, I have decorated it with the following attribute:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class EmployeeService : IEmployeeService

As I stated, I've tried hitting the service using http://localhost/EmployeeService.svc/Register or using jQuery like so:

$.ajax({
    cache: false,
    async: true,
    type: "POST",
    dataType: "json",
    url: "http://localhost/EmployeeService.svc/Register",
    data: '{"username":"test"}',
    contentType: "application/json;charset=utf-8",
    success: function (msg) {
        var status = msg.RegisterResult;
        if (status == 1) {
            alert('Success');
        } else {
            alert('Failure');
        }
    },
    error: function (msg) {
        alert('Exception - ' + msg);
    }
});

Both return 404's.

Since I'm hosting this service with a shared hosting company, I've created my on ServiceFactory as shown below:

public class IisServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var newAddress = baseAddresses[0];

        if (!newAddress.AbsoluteUri.Contains("http://www."))
            newAddress = new Uri(newAddress.AbsoluteUri.Replace("http://", "http://www."));

        var webServiceHost = new IisServiceHost(serviceType, newAddress);
        return webServiceHost;
    }
}

public class IisServiceHost : ServiceHost
{
    public IisServiceHost(Type serviceType, params Uri[] baseAddresses): base(serviceType, baseAddresses) { }
}

A POST is occurring when trying to hit it from jQuery. The 404 response (from a different sample service that was simplified for testing) is shown below:

404 Response from another service in the same project

The text for this exception is: 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.

Jason N. Gaylord
  • 7,910
  • 15
  • 56
  • 95

0 Answers0