This article is "Do you think asmx files in. Net Web Service are redundant?" . This article mainly discusses the interface-based publishing and calling of WebService released by Spring. Net.
Directory
- . Net client call
- Ajax call
1.. Net client calls can be done by loose calls in. Net for services similar to those published through interface specifications in the previous section. To what extent is it loose? Only two conditions are required: 1. WebService address 2. Service Interface assembly. The call process is as follows: <objects xmlns = "http://www.springframework.net" xmlns: aop = "http://www.springframework.net/aop">
<Object id = "person" type = "Spring. Web. Services. WebServiceProxyFactory, Spring. Services">
<! -- Address -->
<Property name = "ServiceUri" value = "http: // localhost: 53825/PersonService. asmx"> </property>
<! -- Service interface -->
<Property name = "ServiceInterface" value = "SpringWebServiceIoCContract. IPerson, SpringWebServiceIoCContract"/>
</Object>
<Object id = "personObj" type = "SpringWebServiceIoCContract. Person, SpringWebServiceIoCContract"> </object> </objects> the call process is simple:
Using (IApplicationContext context = ContextRegistry. GetContext ())
{
Var person0 = context. GetObject ("person") as IPerson;
If (null! = Person0)
{
Console. WriteLine (person0.Add (1, 2 ));
}
}
Output: 2. Ajax call is a little troublesome (maybe I have not found the best call method ), here I will only list the call methods for reference. ajax ({
Type: "POST ",
Url: "WebService1.asmx/GetPerson ",
DataType: "json ",
ContentType: "application/json; charset = UTF-8 ",
Success: function (json) {$ (json. d). each (function () {alert (this. Name + "-" + this. Age )})},
Error: function (error ){
Alert ("call error" + error. responseText );
}
}); Success, the returned data is of the json type. However, if you use Spring. for the Web Service released by. Net, the reconfiguration of the httpModule and HttpHandler in our configuration file has changed the processing module used by the client to request WebService calls. If the configuration is as follows: <Add verb = "*" path = "*. asmx" type = "Spring. Web. Services. WebServiceHandlerFactory, Spring. Web"/>
</HttpHandlers>
<HttpModules>
<Add name = "SpringModule" type = "Spring. Context. Support. WebSupportModule, Spring. Web"/>
</HttpModules>
This method also produces problems. For example, Spring. Web. Extensions assembly is also provided in Spring. Net. If the module filtered by HttpModule is handed over to it for processing, there is no problem. In this case, our configuration should be: <Add verb = "*" path = "*. asmx" type = "Spring. Web. Script. Services. ScriptHandlerFactory, Spring. Web. Extensions"/>
</HttpHandlers>
<HttpModules>
<Add name = "SpringModule" type = "Spring. Context. Support. WebSupportModule, Spring. Web"/>
</HttpModules>
In this case, the preceding Ajax method can call traditional Web Services. Tip: the. Net client (ConsoleApplication) Call can also be successful. The problem is that with Spring. Web. Extensions, can we call the WebServicve released by Spring. Net .? Change the url of the preceding Ajax call to PersonService. asmx, as follows: $. ajax ({
Type: "POST ",
Url: "PersonService. asmx/GetPerson ",
DataType: "json ",
ContentType: "application/json; charset = UTF-8 ",
Success: function (json) {$ (json. d). each (function () {alert (this. Name + "-" + this. Age )})},
Error: function (error ){
Alert ("call error" + error. responseText );
}
});
The problem arises:
We can see that PersonService. asmx/GetPerson can be called successfully, but an exception occurs during JSON conversion. Solution: return the GetPerson type in the IPerson interface to the string type and convert it using JsonConvert. SerializeObject, as shown below: public string GetPerson (string name)
{
Person person = new Person {Age = 25, Name = "zhangsan "};
Return JsonConvert. SerializeObject (person );
}
Ajax call: $. ajax ({
Type: "POST ",
Url: "PersonService. asmx/GetPerson ",
Data: {name: 'A '},
DataType: "JSON ",
Success: function (json ){
Alert (new Function ("return" + json. replace (/<[^>] +> | [\ r \ n]/g, "") (). Name)
},
Error: function (error ){
Alert ("error:" + error. responseText );
}
});
In this way, the call is successful. Look at the differences between calling and traditional WebService: Note: The traditional WebService call, dataType: "json", and now dataType: "JSON ". "JSON" is case sensitive. There is also an AJAX call method: <scriptManager> is used for the following Configuration:
<Add verb = "*" path = "*. asmx" type = "Spring. Web. Script. Services. ScriptHandlerFactory, Spring. Web. Extensions"/>
PersonService. GetPerson (name, GetDataOnSucceeded );
Function GetDataOnSucceeded ()
{
Alert ("Name:" + result. Name + "\ n" + "Age:" + result. Age );
} The call result is as follows: