<system.serviceModel>

  <behaviors>

   <endpointBehaviors>

    <behavior name='ServiceAspNetAjaxBehavior'>

     <enableWebScript/>

    </behavior>

   </endpointBehaviors>

  </behaviors>

  <serviceHostingEnvironment

   aspNetCompatibilityEnabled='true' />

  <services>

   <service name='Service'>

    <endpoint address=''

     behaviorConfiguration='ServiceAspNetAjaxBehavior'

     binding='webHttpBinding' contract='Service'/>

   </service>

  </services>

 </system.serviceModel>

</configuration>

In the Service.cs file located in the App_Code folder, give the service a namespace of 'WCFService', and code the following GetServerTime() method:

using System;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.ServiceModel.Activation;

using System.ServiceModel.Web;

[ServiceContract(Namespace = 'WCFService')]

[AspNetCompatibilityRequirements(RequirementsMode =

 AspNetCompatibilityRequirementsMode.Allowed)]

public class Service {

 // Add [WebGet] attribute to use HTTP GET

 [OperationContract]

 public void DoWork() {

  // Add your operation implementation here return;

 }

 [OperationContract]

 public DateTime GetServerTime() {

  return DateTime.Now;

 }

}

In the Source view of Default.aspx, add the following highlighted code:

<form id='form1' runat='server'>

 <div>

  <asp:ScriptManager ID='ScriptManager1' runat='server'>

   <Services>

    <asp:ServiceReference Path='~/Service.svc' />

   </Services>

  </asp:ScriptManager>

 </div>

 <input id='Button1' type='button' value='Get Server Time'

  onclick='return Button1_onclick()' />

 <div id='result' />

</form>

This adds an instance of the <ScriptManager> control to the page and references the WCF service (Service.svc). It also adds a Button control to the page.

Insert the following JavaScript code into Default.aspx:

<body>

 <script language='javascript' type='text/javascript'>

  function Button1_onclick() {

   WCFService.Service.GetServerTime(CallBackFunction);

  }

  function CallBackFunction(result) {

   $get('result').innerHTML = result;

  }

 </script>

 <form id='form1' runat='server'>

The Button1_onclick() JavaScript function is invoked when the button on the page is clicked. It calls the WCF service and the returning result is retrieved via the CallBackFunction() function.

Press F5 to debug the application. You can now click the Get Server Time button to obtain the server time without causing a refresh on the web page (see Figure 20-47).

Figure 20-47

Summary

This chapter provided an overview of WCF and explained how it differs from traditional ASMX Web Services. It explored the limitations of Web Services today and examined how WCF aims to provide a better way of writing and hosting your services. The various examples shown throughout this chapter afford concrete illustrations of what WCF offers and hopefully provide enough motivations for you to explore this important technology further. 

Part III

Appendixes 

Appendix A

C# Keywords

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату