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.
- using System;
- using System.Runtime.Serialization;
- using System.ServiceModel;
- using System.ServiceModel.Activation;
- using System.ServiceModel.Web;
-
- [ServiceContract (Namespace = "Samples.Services")]
- [AspNetCompatibilityRequirements(
- RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
- public class TimeService
- ...{
- [OperationContract]
- public DateTime GetTime()
- ...{
- return DateTime.Now;
- }
- [OperationContract]
- public string GetTimeFormat(string format)
- ...{
- return DateTime.Now.ToString(format);
- }
- }
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:
- <%@ ServiceHost Language="C#"
- Debug="true"
- Service="TimeService"
- 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:
- <system.serviceModel>
- <behaviors>
- <endpointBehaviors>
- <behavior name="TimeServiceAspNetAjaxBehavior">
- <enableWebScript />
- </behavior>
- </endpointBehaviors>
- </behaviors>
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
- <services>
- <service name="TimeService">
- <endpoint address=""
- behaviorConfiguration="TimeServiceAspNetAjaxBehavior"
- binding="webHttpBinding"
- contract="TimeService" />
- </service>
- </services>
- </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.
- XML and ASP. NET
- Java script in ASP. NET calls the c # Method
- Process of processing ASP. NET Postback Program
- ASP. NET Server-side control CheckBoxList
- Analysis of ASP. NET Membership