【发布时间】: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 网址?那里开门了吗?
-
是的,它在浏览器中工作