1 out of 5 rated this helpful - Rate this topic

WebService Class

Defines the optional base class for XML Web services, which provides direct access to common ASP.NET objects, such as application and session state.

Namespace:  System.Web.Services
Assembly:  System.Web.Services (in System.Web.Services.dll)
public class WebService : MarshalByValueComponent

The WebService type exposes the following members.

  Name Description
Public method WebService Initializes a new instance of the WebService class.
Top
  Name Description
Public property Application Gets the application object for the current HTTP request.
Public property Container Gets the container for the component. (Inherited from MarshalByValueComponent.)
Public property Context Gets the ASP.NET HttpContext for the current request, which encapsulates all HTTP-specific context used by the HTTP server to process Web requests.
Public property DesignMode Gets a value indicating whether the component is currently in design mode. (Inherited from MarshalByValueComponent.)
Protected property Events Gets the list of event handlers that are attached to this component. (Inherited from MarshalByValueComponent.)
Public property Server Gets the HttpServerUtility for the current request.
Public property Session Gets the HttpSessionState instance for the current request.
Public property Site Gets or sets the site of the component. (Inherited from MarshalByValueComponent.)
Public property SoapVersion Gets the version of the SOAP protocol used to make the SOAP request to the XML Web service.
Public property User Gets the ASP.NET server User object. Can be used to authenticate whether a user is authorized to execute the request.
Top
  Name Description
Public method Dispose() Releases all resources used by the MarshalByValueComponent. (Inherited from MarshalByValueComponent.)
Protected method Dispose(Boolean) Releases the unmanaged resources used by the MarshalByValueComponent and optionally releases the managed resources. (Inherited from MarshalByValueComponent.)
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from MarshalByValueComponent.)
Public method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetService Gets the implementer of the IServiceProvider. (Inherited from MarshalByValueComponent.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method ToString Returns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from MarshalByValueComponent.)
Top
  Name Description
Public event Disposed Adds an event handler to listen to the Disposed event on the component. (Inherited from MarshalByValueComponent.)
Top

If you don't need access to the common ASP.NET objects, you can still create an XML Web service without deriving from WebService. Additional ASP.NET objects can be accessed through Context.

XML Web service methods that have the OneWay property of either SoapRpcMethodAttribute or SoapDocumentMethodAttribute set to true, do not have access to their HttpContext. As such, accessing any of the properties of the WebService class, from within that XML Web service method, return null.

If you are using the .NET Framework version 1.0 XML Web service methods that have either the SoapRpcMethodAttribute or SoapDocumentMethodAttribute attribute applied to them with the OneWay property of set to true, do not have access to their HttpContext using the static Current property. To access the HttpContext, derive the class implementing the XML Web service method from WebService and access the Context property.

The example below creates an XML Web service, deriving from WebService, in order to use the Context property to obtain the time of the request on the server.


<%@ WebService Language="C#" Class="Util" %>

 using System;
 using System.Web.Services;

 public class Util: WebService {
   [ WebMethod(Description="Returns the time as stored on the Server",
   EnableSession=false)]
   public string Time() {
      return Context.Timestamp.TimeOfDay.ToString();
   }
 }
 


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
tutorial
1. Maak een nieuwe database Postcodes aan en restore de backup postcodes.bak.
2. Maak nu een nieuw project aan van het type “WCF Service Application” en noem deze Postcodes.WebSvc. We krijgen een aantal files:
a. Web.config : voor het definiëren van binding en behaviour en endpoints.
b. IService1 vb: een publieke interface voor gedefinieerde typen en methoden met attributen zoals <ServiceContract()> en <DataContract()>.
WT3.ASP.NET.LABO Blz. 2 / 6
Bestudeer aandachtig hoe in dit voorbeeld een datatype (CompositeType) als resultaat van een webmethode kan teruggegeven worden.
c. Service1.svc met Service1.svc.vb , die de interface implementeert.
3. Voeg een nieuwe WCFservice toe ( Add >> New Item >> WCF Service ) met naam PostCodeService.svc.
4. Zorg voor een gebruiker in SQL-Server met db_datareader rechten op de Postcodes database. Voeg aan de web.config een connectionstring toe.
5. Voeg een Business Logica project toe aan je solution. Plaats er de class PostCode in . Zorg voor de nodige referenties.
6. Maak in de Business Logica class naast de functie “GetPostCodes” nog een tweede functie aan met naam “SearchPostcodes”. Deze laatste laat toe om postcodes te zoeken op een deel van de code (maak hiervoor gebruik van het LIKE statement in SQL). Als je “85” meegeeft aan deze zoek methode wil je alle gemeentes waarvan de postcode begint met 85.
7. Maak de methode “SearchPostcodes” kenbaar als webmethodes in de interface ( het contract IPostCodeService.vb) van onze WCF service:
a. Importeer de Business logica class om gebruik te kunnen maken van List (Of PostCode)
b. In de interface voeg je de naam van de methodes toe als <OperationContract()> onder <ServiceContract()>
8. Ook in PostCodeService.vb importeren we de business logica en implementeren vervolgens de interface (SearchPostCodes) waarbij een lijst van PostCodes opgehaald wordt.
9. Tijd om de WCF service te testen:
a. Plaats PostCodes.WebSvc als startproject en PostCodeService als StartPage.
b. Start Debugging (F5) waarna een WCF Test Client opent.
c. Door dubbel klikken op de methode test je deze uit. Een testwaarde voor het argument kan je meegeven in de Value kolom.
d. Open even de Web.config en bekijk hoe de WCF service werd opgebouwd door Visual Studio (wsHttpBinding).
10. Op een analoge wijze breiden we de WCF service uit met de methode GetPostCodes .
a. Methode kenbaar maken als webmethode in de interface.
b. De Implementeer beide methodes in de code behind van PostCodeService.svc
c. Testen. Hier loopt
10. Op een analoge wijze breiden we de WCF service uit met de methode GetPostCodes .
a. Methode kenbaar maken als webmethode in de interface.
b. De Implementeer beide methodes in de code behind van PostCodeService.svc
c. Testen. Hier loopt het mis en krijgen we een foutboodschap, die aanduidt dat onze WCT test
WT3.ASP.NET.LABO Blz. 3 / 6
client slechts 65536 inkomende berichten aankan. Dit laten we voorlopig zo en besluiten om zlef een webclient aan te maken, die onze service kan consumeren.
11. Onze cliënt applicatie om te testen hoeft geen webapplicatie te zijn. Een winforms-, een console – of mobile applicatie zou even goed werken. We besluiten toch om een nieuwe website aan te maken , en ( hoewel niet echt logisch) maken we deze aan in dezelfde solution als onze webservice.
12. Plaats op de default.aspx pagina volgende controls:
a. Een GridView
b. Daaronder een Textbox (txtPostCode) en een Button (btnSearch).
13. Maak een referentie naar de WCF service; Build >> rechtermuis >>Add Service Reference en klik daarna op Discover. Geef een passende namespace op ( vb: Postcodes). Op de cliënt wordt onze proxy nu beschikbaar.
14. In de code behind van default.aspx importeren we de namespace van de service en maken we een objectinstantie van onze proxyclasse: Dim client As PostCode
15. Het verder uitbouwen van onze user interface is nu eenvouding:
ServiceClient
a. Onze GridView krijgt als DataSource het resultaat van de WCF methode SearchPostcodes..
b. Een DataBind() maakt het resultaat zichtbaar.
c. We testen uit door de website als startup project in te stellen.
16. Default willen we bij opstart alle postcodes tonen.
a. Haal in Page_Load alle postcodes op en zorg voor databinding met de gridview.
b. Test uit ...en ja we krijgen dezelfde fout : max 65536 berichten. Dit lossen we op via de configuratie in de web.config.
c. Via tools >> WCF Service Configuration Editor krijg je toegang tot de configuratie van de web.config van de website. Zowel endpoints als bindings kunnen van hieruit geconfigureerd worden.

Ga naar de “Bindings”configuratie gegevens. Verhoog er de MaxBufferSize en de MaxReceivedMessageSize. .
example
Imports System.ServiceModel
Imports businesslogic
' NOTE: You can use the "Rename" command on the context menu to change the interface name "IPostCodeService" in both code and config file together.
<ServiceContract()>
Public Interface IPostCodeService

    <OperationContract()>
    Sub DoWork()

    <OperationContract()>
    Function GetPostcodes() As List(Of PostCode)

    <OperationContract()>
    Function SearchPostcodes(ByVal getal As Integer) As List(Of PostCode)

End Interface


' NOTE: You can use the "Rename" command on the context menu to change the class name "PostCodeService" in code, svc and config file together.

Imports businesslogic
Public Class PostCodeService
    Implements IPostCodeService

    Public Sub DoWork() Implements IPostCodeService.DoWork
    End Sub

    Public Function GetPostcodes() As List(Of PostCode) Implements IPostCodeService.GetPostcodes
        Return PostCode.GetPostcodes()
    End Function

    Public Function SearchPostcode(ByVal getal As Integer) As List(Of PostCode) Implements IPostCodeService.SearchPostcodes
        Return PostCode.SearchPostcodes(getal)
    End Function
End Class