WCF返回JSON格式数据问题
关键代码如下:
public interface IService
{
[OperationContract(Name="sayHelloJson")]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "sayHello", BodyStyle = WebMessageBodyStyle.Wrapped)]
String sayHello();
[OperationContract(Name = "SendMessageJson")]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "SendMessage?Message={Message}", BodyStyle = WebMessageBodyStyle.Wrapped)]
String SendMessage(String Message);
}
static void Main(string[] args)
{
Uri ea = new Uri("http://localhost:8000/Service");
WebHttpBinding bb = new WebHttpBinding();
ServiceHost sh = new ServiceHost(typeof(Service),ea);
ServiceMetadataBehavior behave = new ServiceMetadataBehavior();
behave.HttpGetEnabled = true;
sh.Description.Behaviors.Add(behave);
sh.AddServiceEndpoint(typeof(IService),bb,ea);
try
{
sh.Open();
Console.WriteLine("启动成功……");
Console.ReadLine();
sh.Close();
}
catch
{
Console.WriteLine("启动失败……");
}
}
没使用配置文件
浏览器访问http://localhost:8000/Service正常
现在想在浏览器中查看sayHello方法的返回数据
http://localhost:8000/Service/Json/sayHello无法打开
到底哪里有问题?
------解决方案--------------------
------解决方案--------------------另外ServiceHost要改成
WebServiceHost,否则还要麻烦添加Dispatch Behaviour。