stack
stack

Reputation: 653

HTTP Error 404.0 - Not Found when trying to access wcf locally

I'm getting this

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

when trying to access the service from my browser. Here is my config.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>

        <services>
            <!-- Note: the service name must match the configuration name for the service implementation. -->
            <service name="WcfServiceLibrary.Service1" behaviorConfiguration="MyServiceTypeBehaviors" >
                <!-- Add the following endpoint.  -->
                <!-- Note: your service must have an http base address to add this endpoint. -->
                <endpoint contract="WcfServiceLibrary.Service1" binding="basicHttpBinding" address="http://localhost/service1" />
                <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="http://localhost/service1/mex" />
            </service>
        </services>

        <behaviors>
            <serviceBehaviors>
                <behavior name="MyServiceTypeBehaviors" >
                    <!-- Add the following element to your service behavior configuration. -->
                    <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost/service1" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

    </system.serviceModel>

</configuration>

When I type http://localhost/service1 in the web browser I get the 404. But if I remove the app.config below and just simpley do this in the code behind

string serviceUrl = "http://localhost/service1";
 Uri uri = new Uri(serviceUrl);
 host = new ServiceHost(typeof(Service1), uri);
host.Open();

All works well... Any ideas? Seems simple enough.

Upvotes: 1

Views: 6285

Answers (2)

Petar Vučetin
Petar Vučetin

Reputation: 3615

You can show http://localhost/service1?Wsdl in the browser but mex only works with add service reference or WCFTestClient (C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE) because you will get the HTTP Bad Request error which comes from the fact that the browser issues an HTTP GET request where the contents of the message are in the HTTP headers, and the body is empty.

This is exactly what the WCF mexHttpBinding is complaining about.

Upvotes: 0

Petar Vučetin
Petar Vučetin

Reputation: 3615

I think you are missing the host element under your services:

<service name="WcfServiceLibrary2.Service1">
    <host>
        <baseAddresses>
            <add baseAddress = "http://localhost/service1" />
        </baseAddresses>
    </host>
    <endpoint address ="" binding="wsHttpBinding" contract="WcfServiceLibrary2.IService1">
        <identity>
            <dns value="localhost"/>
        </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>

Service host does not need URL then.

    static void Main(string[] args)
    {
        var host = new ServiceHost(typeof(Service1));
        host.Open();

        Console.WriteLine("Host running");
        Console.ReadLine();
    }

Upvotes: 3

Related Questions