HTTP错误404.0 - 尝试在本地访问wcf时找不到

时间:2012-04-15 17:31:10

标签: wcf localhost http-status-code-404

我收到了这个

HTTP错误404.0 - 未找到 您要查找的资源已被删除,名称已更改或暂时不可用。

尝试从我的浏览器访问该服务时。这是我的配置。

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

当我在网络浏览器中输入http://localhost/service1时,我得到了404.但如果我删除下面的app.config并且只是简单地在后面的代码中执行此操作

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

一切运作良好......有什么想法吗?看起来很简单。

2 个答案:

答案 0 :(得分:3)

我认为您缺少服务项下的主机元素:

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

服务主机不需要URL。

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

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

答案 1 :(得分:0)

您可以在浏览器中显示http://localhost/service1?Wsdl,但mex仅适用于添加服务引用或WCFTestClient(C:\ Program Files(x86)\ Microsoft Visual Studio 10.0 \ Common7 \ IDE),因为您将收到HTTP错误请求错误来自浏览器发出HTTP GET请求,其中消息的内容位于HTTP标头中,并且正文为空。

这正是WCF mexHttpBinding抱怨的内容。