none
WCF返回JSON格式的问题 RRS feed

  • 问题

  • 关键代码如下:
    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无法打开
    到底哪里有问题?
    2011年11月29日 6:36

答案