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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 我的第一次WebService接口开发

    我的第一次WebService接口开发前言最近项目上需要对接WebService接口,之前从来没有用过,这次都遇见了。记录下基础的使用和我遇见的问题。正文概述WebService接口百度一搜,各个介绍的都非常详细,由于刚开始没接触,看的也不是很懂。首先记住一句话:WebService是一种跨编程语言和跨操作系统平台的远程调用技术。跨编程语言和跨操作系统平台:也就是说Asp.net开发的WebService我用java代码调用…

    2022年6月12日
    53
  • springBoot入门简介,使用spring initializer快速创建spring Boot项目

    springBoot入门简介,使用spring initializer快速创建spring Boot项目springBoot入门简介,使用spring initializer快速创建spring Boot项目

    2022年4月24日
    145
  • 2018 蓝桥杯省赛 B 组模拟赛(一)-年龄

    2018 蓝桥杯省赛 B 组模拟赛(一)-年龄

    2021年9月28日
    62
  • vim查找选中的文本

    vim查找选中的文本在vim中按/查找的时候,不想每次都键盘输入查找内容,希望能够查找选中的文本。方法如下:第一步:使用y复制选中的文本(yank操作会将文本存入默认寄存器”)第二步:按/键(进入查找模式)第三步:按ctrl+r(访问寄存器)第四步:按”键(粘贴寄存器”的内容)参考资料:https://superuser.com/questions/41378/how-to-search-for-selected-text-in-vim…

    2022年6月18日
    69
  • 501,502,503,504的区别_412状态码

    501,502,503,504的区别_412状态码502badgateway顾名思义网关错误后端服务器tomcat没有起来,应用服务的问题(前提是接入层7层正常的情况下)。应用服务问题一种是应用本身问题;另一种是因为依赖服务问题比如依赖服务RT高,依赖的服务有大的读取(mysql慢查,http等),以至于调用方超过超时read时间;服务集群压力大时,也会出现502超时(502理解为不可响应或响应不过来,其实还是不可响应)。504…

    2022年4月19日
    249
  • datagrip2021.1激活-激活码分享

    (datagrip2021.1激活)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~4D5UJRVIF9-eyJsaWNlb…

    2022年3月30日
    515

发表回复

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

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