WCF Service for ASP. NET Applications

Source: Internet
Author: User

Add a WCF Service to an ASP. NET application

Now, let's learn how to add a AJAX-supported WCF Service to the previous ASP. NET Website. To do this, right-click the sample Website AJAXWCFTest1 and select "Add New Items ...", In the "Add New Items" dialog box that appears, select the "AJAX-Enabled WCF Service" template to Add a New WCF Service and name it TimeService.

After performing the preceding operations, you will find that a service endpoint (timeservice) is added to the Web site. svc) and the Code-behind file timeservice in the App_Code folder associated with it. cs. In addition, it is noted that the configuration file web. config is also modified to provide the relevant registration and discovery information for the newly created WCF Service.

The created TimeService class implicitly describes the contract of the defined WCF Service and its explicit implementation. Note that the ServiceContract and OperationContract attributes assume the same roles as those in the previous WCF programming. In addition, interfaces are not used to define contracts for simplicity.

 
 
  1. using System;  
  2. using System.Runtime.Serialization;  
  3. using System.ServiceModel;  
  4. using System.ServiceModel.Activation;  
  5. using System.ServiceModel.Web;  
  6.  
  7. [ServiceContract (Namespace = "Samples.Services")]  
  8. [AspNetCompatibilityRequirements(  
  9. RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]  
  10. public class TimeService  
  11. ...{  
  12. [OperationContract]  
  13. public DateTime GetTime()  
  14. ...{  
  15. return DateTime.Now;  
  16. }  
  17. [OperationContract]  
  18. public string GetTimeFormat(string format)  
  19. ...{  
  20. return DateTime.Now.ToString(format);  
  21. }  
  22. }  

Note that the above TimeService class exposes two common endpoints: GetTime and GetTimeFormat.
The method endpoint in the above interface is defined in an SVC file. The following describes the timeservice. svc file:

 
 
  1. <%@ ServiceHost Language="C#" 
  2. Debug="true" 
  3. Service="TimeService" 
  4. CodeBehind="~/App_Code/TimeService.cs" %>  

This Service host ServiceHost specifies the language used to implement the Service and the location of the corresponding source file, and identifies the name of the contract used by the Service attribute.
Before you officially start testing this service, you can register the above WCF Service in the web. config configuration file of the ASP. NET application. The following shows the configuration section in the configuration file web. config:

 
 
  1. <system.serviceModel> 
  2. <behaviors> 
  3. <endpointBehaviors> 
  4. <behavior name="TimeServiceAspNetAjaxBehavior"> 
  5. <enableWebScript /> 
  6. </behavior> 
  7. </endpointBehaviors> 
  8. </behaviors> 
  9. <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
  10. <services> 
  11. <service name="TimeService"> 
  12. <endpoint address="" 
  13. behaviorConfiguration="TimeServiceAspNetAjaxBehavior" 
  14. binding="webHttpBinding" 
  15. contract="TimeService" /> 
  16. </service> 
  17. </services> 
  18. </system.serviceModel>  

Note that the above configuration content is automatically generated by the system when the WCF Service is created.

Register an action list for all the endpoints in the previous WCF Service. In this way, a behavior is defined for the WCF Service TimeServiceAspNetAjaxBehavior and it indicates that it uses client scripts to accept requests through HTTP Web protocol. Logically, the above enableWebScript element is consistent with the ScriptService attribute used to modify Web services in ASP. NET Web services.

Then, you need to enumerate all the WCF Services hosted in the current ASP. NET application. Note: The preceding web. config file only shows a service named TimeService. One of its endpoints uses the TimeService contract and the webHttpBinding binding model.

  1. XML and ASP. NET
  2. Java script in ASP. NET calls the c # Method
  3. Process of processing ASP. NET Postback Program
  4. ASP. NET Server-side control CheckBoxList
  5. Analysis of ASP. NET Membership

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.