公开WCF服务REST-
问题描述:
我创建了一个全新的WCF服务。我通过在Visual Studio中添加新项目 - > WCF服务来创建此服务。然后我编辑的合约小幅看起来像下面这样:公开WCF服务REST-
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebGet(UriTemplate = "/Authenticate/{username}/{password}", ResponseFormat = WebMessageFormat.Json)]
bool Authenticate(string username, string password);
}
我的操作如下所示:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults = false)]
public class MyService : IMyService
{
public bool Authenticate(string username, string password)
{
try
{
return false;
}
catch (Exception ex)
{
throw new ApplicationException("Unknown exception");
}
}
}
当我访问:http://localhost:80/MyService.svc/Authenticate/someUserName/somePassword在我的浏览器窗口,出现空白屏幕。我期待“假”出现在JSON语法中。我究竟做错了什么?
谢谢!
答
使用像Fiddler这样的工具来查看实际的HTTP消息。帮助调试。
二,你的请求URL是错误的。试试这个:
http://localhost:80/MyService.svc/Authenticate/someUserName/somePassword
你有一个SVC文件,是否正确?如果你在IIS中托管这些,你将需要它。如果您将自己托管在WebServiceHost对象中,则不需要它们。
using(WebServiceHost host = new WebServiceHost(typeof(MyService)))
{
host.Open();
Console.WriteLine("Service is running");
Console.WriteLine("Press enter to quit...");
Console.ReadLine();
host.Close();
}
你是对的。我有一个SVC文件。我从此编辑过这篇文章。尽管我仍然看到一个空白的屏幕。 – Villager 2010-10-15 14:57:08
空白屏幕?你用Firefox来测试这个?您可能会收到400或404错误。 IE(是的,IE)会给你HTTP错误。也试试Fiddler。非常好的工具。 http://www.fiddler2.com/fiddler2/ – MonkeyWrench 2010-10-15 14:59:59
你是如何托管服务?在IIS中? Visual Studio Dev服务器的东西?使用WebServiceHost自助托管?如果找到创建控制台应用程序并使用它来自行托管Web应用程序,则会使调试更容易。 – MonkeyWrench 2010-10-15 15:00:49