【问题标题】:WCF service 404 Not foundWCF 服务 404 未找到
【发布时间】:2014-09-25 05:49:54
【问题描述】:

我有一个基本的 WCF 库项目,我试图在控制台应用程序中托管它。 Program.cs 文件的 Main 方法如下:

    static void Main(string[] args)
    {
        // Create a binding and set the security mode to Message.
        BasicHttpBinding b = new BasicHttpBinding();//WSHttpBinding(SecurityMode.Message);

        Type contractType = typeof(SecureWCFLib.IService1);
        Type implementedContract = typeof(SecureWCFLib.Service1);

        Uri baseAddress = new Uri("http://localhost:8733/Design_Time_Addresses/SecureWCFLib/Service1/");

        ServiceHost sh = new ServiceHost(implementedContract, baseAddress);

        sh.AddServiceEndpoint(contractType, b, "Service1");

        ServiceMetadataBehavior sm = new ServiceMetadataBehavior();
        sm.HttpGetEnabled = true;
        sh.Description.Behaviors.Add(sm);

        sh.Open();
        Console.WriteLine("Listening");
        Console.ReadLine();
        sh.Close();


    }

我有另一个充当客户端的控制台应用程序。我正在尝试使用 Program.cs 中的服务,如下所示:

 static void Main(string[] args)
        {   
            IService1 productChannel = null;
            EndpointAddress productAddress = new EndpointAddress("http://localhost:8733/Design_Time_Addresses/SecureWCFLib/Service1/");
            productChannel = ChannelFactory<IService1>.CreateChannel(new BasicHttpBinding(), productAddress);
            string result = productChannel.GetData(123);
            Console.WriteLine(result);
            Console.Read();
        }

但我得到了例外

{"远程服务器返回错误:(404) Not Found."}

请让我知道我在这里做错了什么。

【问题讨论】:

  • 您是否尝试在浏览器中访问您的 WCF 网址?那里开门了吗?
  • 是的,它在浏览器中工作

标签: c# wcf


【解决方案1】:
EndpointAddress productAddress = new EndpointAddress("http://localhost:8733/Design_Time_Addresses/SecureWCFLib/Service1/Service1"); 

Services1添加到构造函数参数的末尾。

client endpointaddress = serviceshost_baseAddress + relative address.

您的代码等于配置:

<service name="SecureWCFLib.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/SecureWCFLib/Service1/" />
          </baseAddresses>
        </host>
        <endpoint address="Service1" binding="basicHttpBinding" contract="SecureWCFLib.IService1">
        </endpoint>
<behaviors>
<!--......-->
<behaviors>
</service>

【讨论】:

    【解决方案2】:

    如果您希望其他客户端访问 IIS express 上的 Web 服务,您必须授予对 IIS express 端口的远程访问权限。 请看下面的链接。

    http://johan.driessen.se/posts/Accessing-an-IIS-Express-site-from-a-remote-computer

    http://www.iis.net/learn/extensions/using-iis-express/handling-url-binding-failures-in-iis-express

    【讨论】:

    • 它不在 IIS 上。我正在使用 VS 开发服务器。
    • 是的,我知道,但是 VS 使用 IIS EXPRESS。
    猜你喜欢
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2016-03-17
    相关资源
    最近更新 更多