WCF服务404未找到

时间:2014-09-25 05:49:54

标签: c# wcf

我有一个基本的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。”}

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

2 个答案:

答案 0 :(得分:0)

如果您希望其他客户端可以访问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

答案 1 :(得分:0)

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>