Do you think the asmx file in. Net Web Service is redundant?

Source: Internet
Author: User
Generally, A. asmx file is added first when WebService is developed using. Net, and then WebService is constructed by marking WebService and WebMethod in its. cs file. Have you ever wondered whether the. asmx file is redundant .? In fact, asmx is not used as a UI like aspx, nor is it used to process business logic like A. handler file. Its existence is redundant. Using Spring. Net to build a WebService completely removes the need for the. asmx file, and directly injects it into the class that provides external services through its IoC container. This article will take you into a WebService without a. asmx file.

 

Directory:
  • Create a WebService independent of the asmx File
  • Use Spring. Net to provide WebService

 

 

To understand the local machine, you need to have some basic understanding of Spring. Net. I will give a general idea about its role:

1. it can act on an IoC (or DI) container to implement program decoupling.

2. Use the front-end programming (AOP) framework. 3. Use the same management method for processing different transactions. 4. Provide a verification framework for verification. For example, configure Spring. Net. If you do not know enough about Spring. Net, see; movie Service. Add a file for the original WebService, but do not depend on the specific file through Spring. Net configuration. Public class HelloWorldService: WebService

{
// [WebMethod]
Public string HelloWorld (string str)
{
Return "Hello World:" + str;
}

[WebMethod]
Public Person GetPerson ()
{
Return new Person {Age = 25, Name = "zhansan "};
}

[WebMethod]
Public void SavePerson (Person person)
{
Return;
}
}

Configuration example:

 

Note that abstract = true in the figure, so that Spring. Net can avoid creating redundant service objects. Spring recommends doing so. Access Service:

2. Provide WebService services through Spring. Net

This method is the focus of this section. Due to the loose coupling of services, many people think that services are more suitable for using interfaces for standardization. Spring. Net is also implemented based on this. First, we define the Service interface (similar to the service contract in WCF, but there is no need to mark ServiceContract and so on) interface Definition: public interface IPerson
{
String SayHello (string name );
Int Add (int x, int y );
Void SavePerson (Person person );
Person GetPerson (string name );
String GetPersonString ();
} There is nothing special about service implementation: public class PersonService: IPerson

{
# Region IPerson Member

Public string SayHello (string name)
{
Return "Hello word:" + name;
}

Public int Add (int x, int y)
{
Return x + y;
}

Public void SavePerson (Person person)
{
Return;
}

Public Person GetPerson (string name)
{
Return new Person {Age = 25, Name = "zhangsan "};
}

Public string GetPersonString ()
{
Return JsonConvert. SerializeObject (new Person {Age = 25, Name = "zhangsan "});
}
# Endregion
}

Configure the service through Spring. Net: <configuration>

<ConfigSections>
<SectionGroup name = "spring">
<Section name = "context" type = "Spring. Context. Support. WebContextHandler, Spring. Web"/>
<Section name = "objects" type = "Spring. Context. Support. DefaultSectionHandler, Spring. Core"/>
</SectionGroup>
</ConfigSections>

<Spring>
<Context>
<Resource uri = "config: // spring/objects"> </resource>
</Context>
<Objects xmlns = "http://www.springframework.net" xmlns: aop = "http://www.springframework.net/aop">
<Object id = "person" type = "SpringWebServiceIoC. PersonService, SpringWebServiceIoC">
</Object>
<Object id = "personObj" type = "SpringWebServiceIoCContract. Person, SpringWebServiceIoCContract"> </object>
<Object id = "PersonService" type = "Spring. Web. Services. WebServiceExporter, Spring. Web">
<Property name = "targetName" value = "person"> </property>
<Property name = "MemberAttributes">
<Dictionary>
<Entry key = "Add">
<Object type = "System. Web. Services. WebMethodAttribute, System. Web. Services">
<Property name = "Description" value = "sum of calculated integers"> </property>
<Property name = "MessageName" value = "computing"> </property>
</Object>
</Entry>
</Dictionary>
</Property>
</Object>
</Objects>
</Spring>

<System. web>
<Compilation debug = "true" targetFramework = "4.0"/>
<HttpHandlers>
<Add verb = "*" path = "*. asmx" type = "Spring. Web. Services. WebServiceHandlerFactory, Spring. Web"/>
</HttpHandlers>
<HttpModules>
<Add name = "SpringModule" type = "Spring. Context. Support. WebSupportModule, Spring. Web"/>
</HttpModules>
</System. web>

</Configuration>

In addition to Handler for processing the amsx file, you must configure the httpModules module to forward requests to Handler.

Access Service:

Note: When Spring. NEt is used to implement WebService, services developed on the 3.5 platform can run normally on the 4.0 platform, and the following exceptions may occur:

 

However, services on 4.0 can run normally without parameters. This is usually caused by different Spring. Net versions. Reminder.

 

Code download: http://files.cnblogs.com/tyb1222/SpringWebService.rar

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.