【问题标题】:HTTP Error 404.0 - Not Found when trying to access wcf locallyHTTP 错误 404.0 - 尝试在本地访问 wcf 时未找到
【发布时间】:2012-04-15 17:31:10
【问题描述】:

我收到了

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();

一切正常...有什么想法吗?看起来很简单。

【问题讨论】:

  • 您是尝试在 IIS 中托管还是自托管?
  • 自托管。我已经创建了一个 Windows 窗体应用程序
  • 它是由 IIS 自托管的,仍然可以托管吗?
  • 基本上我认为如果 app.config 是自托管的,您会丢失一些配置参数,但我不确定......

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


【解决方案1】:

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

<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();
    }

【讨论】:

  • 感谢工作。但作为额外的,为什么我不能在浏览器中以localhost/service1/mex 的这种方式查看 mex?
【解决方案2】:

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

这正是 WCF mexHttpBinding 所抱怨的。

【讨论】:

    猜你喜欢
    • 2020-04-30
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2017-07-28
    相关资源
    最近更新 更多