Introduction to the creation, deployment, and use of the. Net Platform WebService

Source: Internet
Author: User
Tags configuration settings soap

. NET platform with support for Web service, including the building and use of Web service. Unlike other development platforms, it is used. NET platform, you do not need other tools or SDKs to complete the development of Web service. The. NET Framework itself fully supports Web service, including server-side request handlers and support for sending and receiving SOAP messages to clients. Down we'll use a very simple example to describe the creation, deployment, and use of Web service.

First, create one of the simplest web Service with Visual Studio

First, open vs, open "File->new->web Site" and select "ASP."

Looking at the Service.cs code, you will find that a default framework has been established for the Web service file. The original code is:

Default Web Service<!--Code highlighting produced by Actipro Codehighlighter (freeware) http://www.CodeHighlighter.com/-->using System;
Using System.Linq;
Using System.Web;
Using System.Web.Services;
Using System.Web.Services.Protocols;
Using System.Xml.Linq;

[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
To allow this Web Service to being called from script, using ASP. Uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class Service:System.Web.Services.WebService
{
Public Service () {
Uncomment the following line if using designed components
InitializeComponent ();
}

[WebMethod]
public string HelloWorld () {
Return "Hello World";
}
}

This Web service uses http://tempuri.org/as its default namespace. There is already a Hello World method in the project, run directly to see the effect, click on the "HelloWorld" hyperlink in the display page to jump to the next page. Then click on the "Invoke" button, you can see the results of the Web service returned in XML format is: <string>hello world</string>. So we can add all the methods we want to provide.

It is important to note that:

1. http://tempuri.org/is the default namespace, it is recommended that you replace your own namespace before publishing.

2. [WebMethod] is a feature that Web service provides for us, which means that the following method is a Web method that allows the Web to invoke the method using SOAP. There can be more than one [WebMethod] in a Web service file, and a [WebMethod] is only valid for the methods below it.

Ii. deployment of WebService

WebService can be published to a TCP server only if it is deployed and released for external use.

The webservice deployment will have the following directory structure:

1. \inetpub

2. \wwwroot

3. \webservicename

4. Webservicename.asmx

5. \ bin

The articles published by WebService are:

Entry

Description

Web application directory

As the root directory of your XML Web service. All the files are stored in this directory. This directory should be marked for use by an IIS Web application.

The base URL of the client that invokes the XML Web service. This file can be any valid file name.

. disco file

(optional) as the discovery mechanism for XML Web services.. disco files are not automatically created for XML Web services. This file can be any valid file name.

Web. config file

(selectable) If you need to override the default configuration settings, You can include a Web. config file. Web services Use this configuration file to allow for customization and extensibility of the system. For example, if your Web service needs to be validated in the system and other Web applications do not need it, you can provide a Web service-specific website. config file.

\ bin directory

Contains binaries for this XML Web service. If your XML Web service class and the. asmx file are not in the same directory, the part that contains the class must be in the \ Bin directory.

Third, the use of WebService

1. Add Web Reference---instantiate Web service class---Execute WebService method
WebService may be called on this project, not on this project, or even offsite.

* Add a Web service reference

* You can change the reference name of the Web service and then instantiate the application to invoke the method again: (WebService ws = new WebService (); string s = ws. HelloWorld ();)

* Once you've added a reference, you'll see a file with the Web service already added under the App_WebReferences folder

*.disco,*.discomap,*.wsdl

2. WebService in the foreground page JS call method

2.1 First Use the following method to webservice in the foreground

<asp:scriptmanager runat= "Server" >

<Services>

<asp:servicereference path= "Webservice.asmx" inlinescript= "True"/>

</Services>

</asp:ScriptManager>

2.2 can then be called through the JS program, the example is as follows:

<script. Type= "Text/jscript" >

function A ()

{

Webservice.helloworld (Onresult);

}

A ();

......

</script>

Four, commissioning WebService

In VS, if the WebService is in the same project as ASP., setting breakpoints directly can be done in single-step debugging. However, if the WebService is a standalone project, it cannot be debugged even if the breakpoint is set after the client calls.

If it's just a separate debug on WebService. You can do this, right-click on the WebService project, debug, launch a new instance, you can set a breakpoint for one-step debugging.

But if you want to test the data sent to WebService by the client to debug, you can do this: start the client, such as WinForm or Web page, and then attach to the process under the Web service debugging, set a breakpoint in the WebService can be debugged.

You can also start trace in Web. config to detect data changes in real time.

Introduction to the creation, deployment, and use of the. Net Platform WebService

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.