WCF(ServiceHost)和HTML(JQuery)之间的通信

时间:2015-10-22 14:36:04

标签: json wcf servicehost

我想为WCF服务和html页面之间的通信编写一个简单的“Hello world”示例。

要对WCF服务器进行编程,请使用以下代码:

    namespace ConsoleHelloWorldServiceClient 
     {
     class Program
     {
        static  void Main(string[] args)
        {
             var adrs = new Uri[1];
            adrs[0] = new Uri("http://localhost:6464");
            using (ServiceHost host = new ServiceHost(typeof(HelloWorld.HelloWorldService ),adrs))
            {
                host.Open();
                Console.WriteLine("Server is open");
                Console.WriteLine("Press Enter to close server");
                Console.ReadLine();
            }         
       }     
     }
 }     

Hello world Interface

  namespace HelloWorld 
    {
    [ServiceContract]  
     public interface IHelloWorldService 
     {
       [OperationContract]
        string SayHello();
     }
    }   

Hello world class

namespace HelloWorld
 {
    [DataContract]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class HelloWorldService : IHelloWorldService
      {
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "SayHello")]
        public string SayHello()
        {
             return "Hello World!";
        }
      }
  }

现在在我的HTML页面中,我想点击一个按钮并显示文字。

然后我使用JQuery与服务进行通信:

 <!DOCTYPE HTML>
        <html>
           <head> 
        <script type="text/javascript"
          src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.1.min.js">
        </script>
    
        <script type="text/javascript">
            var url = 'http://localhost:6464/HelloWorldService';
            $(document).ready(function () { 
                $("#get").click(function () {
                    var urlGet = url + 'SayHello';
                    $.getJSON(urlGet, function (data) {
                        $('#text').val(data);
                    });
                });
            });
    
            
        </script>
      </head>
      <body>
      <input id="text" type="text" value="Hello" />
       <input id="get" type="button" value="Get" />
      </body>
    </html>

但我觉得这个客户端只用于网络服务器...... 我该怎么办?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

你在托管中遗漏了几件事。没有定义绑定。特定于连接JQuery客户端服务应该可以通过Http协议获得,消息应该是简单或可理解的格式,即JSON。

var adrs = new Uri("http://localhost:6464");
using (ServiceHost host = new ServiceHost(typeof(HelloWorld.HelloWorldService ),adrs))
{
    var restEndPoint = host.AddServiceEndpoint(typeof(IHelloWorldService), new WebHttpBinding(), "");
    restEndPoint.Behaviors.Add(new WebHttpBehavior();

    host.Open();
    Console.WriteLine("Server is open");
    Console.WriteLine("Press Enter to close server");
    Console.ReadLine();
}  

ServiceContract声明中的更正。 WebInvoke属性应该在接口契约上。

 [ServiceContract]  
     public interface IHelloWorldService 
     {
       [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "SayHello")]
        string SayHello();
     }

您的服务如下:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class HelloWorldService : IHelloWorldService
  {
        public string SayHello()
        {
             return "Hello World!";
        }
      }
  }

DataContract只应用于邮件,即不在服务中的实体。