The difference between WEB Reference and service reference

Source: Internet
Author: User

Today, because the project needs to use a service reference, the previous experience is added to the following steps:
Project Root--reference--right--Add service reference--advanced--Add Web Reference--Enter the URL address of the interface--carriage return (appears below the definition of the interface)--Modify the Web reference name--Add a reference.

By following the steps above, you can add references to the Web References folder, which is also a previous experience, but when used specifically, it was found that adding a web Reference did not meet the needs of the project, and that it needed service Reference. There have been a lot of articles before, and most of them say there is no big difference between the two, mainly the service reference. NET version is higher and you can use WCF. So what's the difference between the two?

Web reference generated proxies can be invoked by the. net1.1 or. net2.0 client, and Service reference generated proxies can only be invoked by the. net3.0+ Client.

Second, the WEB reference cannot use WCF (as mentioned above, because WCF requires. net3.0 support); Service reference can use WCF (of course, 3.0+ clients).

Third, the Web Reference is generated by the Wsdl.exe client proxy, the method that generates the WebService proxy class, that is, derived from System.Web.Services.Protocols.SoapHttpClientProtocol, the proxy class code can specify the ur to be accessed L property; Service Reference is generated by svcutil.exe client proxies, and is based on the new WebService Client access mechanism WCF (not directly derived from System.Web.Services.Protocols.SoapHttpClientProtocol). The proxy class code does not have a URL attribute to specify, but instead specifies endpoint in the client's configuration file app.config to access the specified URL.

So how to add a service reference, the steps are similar to the beginning of the article, specifically as follows:
Project Root--reference--right--Add service reference--enter address--go to (there will be two small windows of service and operation, the service is the proxy class name, the action is the callable method defined in the proxy class)--Modify the namespace--OK.

Unlike adding Web reference, the Advanced button is not clicked, and the Web reference added in advanced is a reference to the. net2.0 version, which is reserved for compatibility.

MSDN Explanation: To invoke the method of a Web service, you should call the corresponding method of the generated Web service proxy class. The generated proxy class executes a Web request to the Web service method.
Attention
To enable an AJAX-enabled ASP.net application to invoke a Web service from a script, you must configure the ASP.net HTTP handler that is required for that application to register.
The Web service that you invoke must be an. asmx file and must be qualified with the Scriptserviceattribute attribute. Each method invoked from the script must be qualified with the WebMethodAttribute attribute. Methods without this attribute are not exposed in the proxy class.

The above is the service reference steps and related knowledge, below see how to add a service interface at a site:

Service to be built on the web layer, on the web layer or in either folder right--add--New Item--Select Web Service (Note that file is a suffix of. asmx)

Here is a small example:

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Services;
Using System.Data;
Namespace Demoweb.api
{
    ///<summary>
    ///wsverify Summary description
    ///</summary>
    [ WebService (Namespace = "http://www.*****.com/")]
    [webservicebinding (ConformsTo = wsiprofiles.basicprofile1_1) ]
    [System.ComponentModel.ToolboxItem (false)]
    //To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.
    //[System.Web.Script.Services.ScriptService] public
    class WSVerify:System.Web.Services.WebService
    {
        [WebMethod (EnableSession = true)] public
        DataTable Getloginuserid (string Username, string password)
        {
            //check whether the user logs on to the
            DataTable dt = new Bll.company (). GETUSERBYUSERNAMEPWD (username, password);
            return dt;
        }}}

Where the Webmehodattribute enclosing class cannot be inherited, the method can be invoked from a remote WEB client after the attribute is added to a method in an XML Web service created using asp.net. Otherwise it cannot be invoked.

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.