asmx文件_将Web服务的实现与ASMX文件分开

asmx文件_将Web服务的实现与ASMX文件分开asmx文件Afellowsaidrecentlythathewantedtobuilda”monsterwebservice”withover20classesandover20methods(well,notTHATmonster,butnotHelloWorld).Hesaid:一位同僚最近说,他想构建一个具有20多个类和20多种…

大家好,又见面了,我是你们的朋友全栈君。

asmx文件

asmx文件

A fellow said recently that he wanted to build a “monster web service” with over 20 classes and over 20 methods (well, not THAT monster, but not Hello World). He said:

一位同僚最近说,他想构建一个具有20多个类和20多种方法的“怪物Web服务”(嗯,不是那个怪物,而是Hello World)。 他说:

Is there any way to provide my consumers with a single end-point (.asmx) exposing these methods from several different class files? I can’t see my dev team all working on a single ASMX file…what am I missing here?

有什么方法可以为我的使用者提供一个单一端点(.asmx),以从多个不同的类文件中公开这些方法? 我看不到我的开发团队都在处理单个ASMX文件…在这里我缺少什么?

It’s easy to make the assumption that the ASMX file has some magic and that everything needs to go in it. But the ASMX is just an endpoint like an ASPX or ASMX. It gives IIS and ASP.NET something to think about, but it’s just a broker – even less – it’s a front.

可以很容易地假设ASMX文件具有魔力,并且所有内容都需要放入其中。 但是ASMX只是一个端点,例如ASPX或ASMX。 它为IIS和ASP.NET提供了一些考虑因素,但它只是一个代理,甚至更少,它是一个前台。

DasBlog has a Web Services interface, thanks to Clemens and the crew before Omar and I, and here’s the contents of it’s EditService.asmx.cs:

DasBlog具有Web服务接口,这要感谢Clemens和Omar和I之前的工作人员,这是EditService.asmx.cs的内容:

[WebService(Namespace=”urn:schemas-newtelligence-com:dasblog:edit-services”)]

[WebService(Namespace =“ urn:schemas-newtelligence-com:dasblog:edit-services”)]

 public class EditService : EditServiceImplementation

公共类EditService:EditServiceImplementation

 {

{

 }

}

That’s it. Seriously. It lives in our main Web Project.  So, how does this work? Well, look at what the class it’s derived from. It’s not System.Web.Services.WebService (yet), it’s EditServiceImplementation.

而已。 说真的它位于我们的主要Web项目中。 那么,这是如何工作的呢? 好吧,看看它是从什么类派生的。 还不是System.Web.Services.WebService,而是EditServiceImplementation。

RULE: Don’t mix your implementation with your presentation

规则:请勿将您的实施与演示文稿混在一起

A Web Services endpoint is arguably just a presentation of some logic. Hopefully that logic exists somewhere that’s NOT the ASMX file. The ASMX file is just a way to call something somewhere else.

Web服务端点可以说只是一些逻辑的表示。 希望该逻辑存在于ASMX文件以外的地方。 ASMX文件只是在其他地方调用某些内容的一种方法。

For example, here’s part of the source for EditServiceImplementation.cs that’s in a totally different assembly and project.

例如,这是EditServiceImplementation.cs源代码的一部分,它位于完全不同的程序集和项目中。

public class EditServiceImplementation : WebService

公共类EditServiceImplementation:WebService

{

{

    [WebMethod]

[WebMethod]

    public string CreateEntry(Entry entry, string username, string password)

公共字符串CreateEntry(条目,字符串用户名,字符串密码)

    {

{

        SiteConfig siteConfig = SiteConfig.GetSiteConfig();

SiteConfig siteConfig = SiteConfig.GetSiteConfig();

        if (!siteConfig.EnableEditService)

如果(!siteConfig.EnableEditService)

        {

{

            throw new ServiceDisabledException();

抛出新的ServiceDisabledException();

        }

}

        if (SiteSecurity.Login(username, password).Role != “admin”)

如果(SiteSecurity.Login(用户名,密码)。角色!=“ admin”)

        {

{

            throw new Exception(“Invalid Password”);

抛出新的Exception(“ Invalid Password”);

        }

}

        // ensure that the entryId was filled in

//确保entryId已填写

        //

//

        if (entry.EntryId == null || entry.EntryId.Length == 0)

if (entry.EntryId == null || entry.EntryId.Length == 0)

        {

{

            entry.EntryId = Guid.NewGuid().ToString();

entry.EntryId = Guid.NewGuid()。ToString();

        }

}

        ILoggingDataService logService = LoggingDataServiceFactory.GetService(Context.Server.MapPath(siteConfig.LogDir));

ILoggingDataService logService = LoggingDataServiceFactory.GetService(Context.Server.MapPath(siteConfig.LogDir));

        IBlogDataService dataService = BlogDataServiceFactory.GetService(Context.Server.MapPath(siteConfig.ContentDir), logService);

IBlogDataService dataService = BlogDataServiceFactory.GetService(Context.Server.MapPath(siteConfig.ContentDir),logService);

        SaveEntry(entry, “”, null, siteConfig, logService, dataService);

SaveEntry(entry,“”, null ,siteConfig,logService,dataService);

        return entry.EntryId;

返回entry.EntryId;

    }

}

    //SNIP…

// SNIP …

This shows EditServiceImplementation (remember, NOT in the ASMX.cs file) deriving from WebService. It also shows the CreateEntry method that is marked as a [WebMethod] and exposed to the outside world.

这显示了从WebService派生的EditServiceImplementation(请记住,不在ASMX.cs文件中)。 它还显示了CreateEntry方法,该方法被标记为[WebMethod]并向外界公开。

Note that this method takes an Entry, a username and a password. Internally it checks to see if Web Services are enabled, tries to log the user in then calls the existing .NET API method “SaveEntry“.

请注意,此方法需要一个Entry,一个用户名和一个密码。 它在内部检查是否已启用Web服务,尝试登录用户,然后调用现有的.NET API方法“ SaveEntry ”。

SaveEntry already existed. the CreateEntry WebMethod is a stateless rollup of Login and SaveEntry.

SaveEntry已经存在。 CreateEntry WebMethod是Login和SaveEntry的无状态汇总。

So, in this example:

因此,在此示例中:

WebService Request -> EditService.asmx -> EditService class, deriving from EditServiceImplementation -> Validate the User, etc -> Broker to existing SaveEntry API.

WebService请求-> EditService.asmx-> EditService类,从EditServiceImplementation->验证用户等->代理到现有SaveEntry API。

We leverage the existing dasBlog DataService, we easily create other endpoints (foo.asmx) in a number of ways, the implementation doesn’t even live in the Web Project, and even then, the Implementation is ten lines of code.

我们利用现有的dasBlog DataService,以多种方式轻松创建其他端点(foo.asmx),该实现甚至不存在于Web Project中,即使如此,该实现也只有十行代码。

Don’t let your [perception] of the  ASMX file cramp your style. You can add a few small layers and not only make development of your Web Service easy, but also friendly for large development groups. Idea: If you’re concerned about collision, folks can test and work on their own atomic endpoints before rolling everything up into one WSDL. Also, remember Contract First.

不要让您对ASMX文件的理解会限制您的样式。 您可以添加一些小层,不仅使Web Service的开发变得容易,而且对大型开发组也很友好。 想法:如果您担心冲突,人们可以在将所有内容汇总到一个WSDL中之前对其进行测试并在自己的原子端点上工作。 另外,请记住合同第一。

翻译自: https://www.hanselman.com/blog/separating-a-web-services-implementation-from-the-asmx-file

asmx文件

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/142753.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • c语言代码缩进是什么意思_word文字无法换行

    c语言代码缩进是什么意思_word文字无法换行一、缩进的空格数为4个。二、“{”和“}”各自独占一行。不规范例子:for(i=0;i<student_num;i++);{if((score[i]>=0)&&(score[i])<=100)total_score+=score[i];elseprin…

    2022年10月18日
    1
  • java大数据开发需要掌握什么_大数据要学java吗

    java大数据开发需要掌握什么_大数据要学java吗​​​​​​你想过自己的未来规划吗?java大数据程序员只需要学到技术就行吗?1.如何成为大数据工程师Java开发是IT行业的经典岗位,行业当中存在普遍的需求,Web开发、Android开发、游戏开发等基本上Java语言是主力队伍。而进入大数据时代,Java又在大数据方向上有了用武之地,又该如何进行成长路线规划。在Java程序界流行着一种默认的说法叫黄金5年,也就是一个程序员从入职的时候开始算起,前五年的选择直接影响着整个职业生涯中的职业发展方向和薪资走向。2014年8月,阿里巴巴举办了

    2022年10月19日
    3
  • qq邮箱日发5万邮件群发技术(qq邮箱怎样定时发送邮件)

    将代码部署服务器,每日早上定时获取到天气数据,并发送到邮箱。也可以说是一个小人工智障。思路可以运用在不同地方,主要介绍的是思路。

    2022年4月18日
    701
  • Java框架总结

    Java框架总结本系列用来记录常用java框架的基本概念、区别及联系,也记录了在使用过程中,遇到的一些问题的解决方法,方便自己查看,也方便大家查阅。欲速则不达,欲达则欲速!一、SSH1、基本概念SSH框架是JAVAEE中三种框架所集成,分别是Struts,Spring,Hibernate框架所组成,是当前比较流行的javaweb开源框架。集成SSH框架的系统从职责上分为(Struts2–…

    2022年7月9日
    17
  • 数据库常见面试题(附答案)

    数据库常见面试题(附答案)1.事务四大特性原子性,要么执行,要么不执行隔离性,所有操作全部执行完以前,其它会话不能看到过程一致性,事务前后,数据总额一致持久性,一旦事务提交,对数据的改变就是永久的2.数据库隔离级别,每个级别会引发什么问题,mysql默认是哪个级别脏读:事务B读取事务A还没有提交的数据不可重复读:两次事务读的数据不一致幻读:事务A修改了数据,事务B也修改了数据,这时在事务A看

    2022年5月2日
    75
  • vbs表白代码+整人代码,抖音vbscript表白代码

    vbs表白代码+整人代码,抖音vbscript表白代码步骤一:在电脑上新建一个txt文件步骤二:将以下代码复制过去(中文可以改)consttitle=“爱情测试”constyourname=“嫦娥”constquestion=“你最喜欢的人是谁?请在下面的方框中输入他(她)的名字。”constinfo=“你在说谎!不要逃避,实话实说。”constscend=“你说出了你的心扉,那就向他(她)表白吧。”di…

    2022年6月8日
    46

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号