你是否觉得.Net Web Service 中asmx文件是多余的?
?
目錄:- 創建不依賴于asmx文件的WebService?
- 通過Spring.Net提供WebService服務?
?
了解本機,需要對Spring.Net有一些基本的認識。我大致說說它的作用:1、可以作用一種IoC(或者說的DI)的容器實現程序的解耦。
2、使用面前方面編程(AOP)的框架 3、對不同事務之間的處理可以使用同一的管理方式 4、提供驗證框架用來做驗證如怎樣配置Spring.Net。如果你對Spring.Net不夠了解,請參見;http://www.cnblogs.com/tyb1222/category/270053.html1、創建不依賴于asmx文件的WebService首先通過Spring.Net創建不依賴具體asmx文件的Web Service .添加一個原始WebService的文件,只是通過Spring.Net的配置完成對具體文件的無依賴性。? ? ? 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;
????????}
????}
?
注意圖中abstract=true,可以讓Spring.Net避免創建多余的服務對象,Spring推薦這樣做。訪問服務:2、通過Spring.Net提供WebService服務
這種方式是本節敘述的重點。由于服務的松散耦合性,很多人都認為服務更適合使用接口進行規范。Spring.Net也是基于這一點來實現的。首先我們定義服務接口(有點類似WCF中的服務契約,但無需標記ServiceContract等等任何標記)? ?接口定義:public?interface?IPerson????{
????????string?SayHello(string?name);
????????int?Add(int?x,?int?y);
????????void?SavePerson(Person?person);
????????Person?GetPerson(string?name);
????????string?GetPersonString();
????}服務實現沒有什么特殊的地方,如下:public?class?PersonService?:?IPerson?
????{
????????#region?IPerson?成員
????????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
????}
? ? <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="計算量整數之和"></property>
????????????????????????????????<property?name="MessageName"?value="計算"></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>
?
但是在4.0上的服務,如果無參數又能正常運行。這通常是因為使用的Spring.Net版本不同導致。提醒注意。
?
代碼下載:http://files.cnblogs.com/tyb1222/SpringWebService.rar?
轉載于:https://www.cnblogs.com/tyb1222/archive/2012/06/05/2537395.html
總結
以上是生活随笔為你收集整理的你是否觉得.Net Web Service 中asmx文件是多余的?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue Google浏览器插件 Vue
- 下一篇: zz数字图像的结课报告