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


相关推荐

  • Python爬取热搜数据之炫酷可视化[通俗易懂]

    Python爬取热搜数据之炫酷可视化[通俗易懂]可视化展示看完记得点个赞哟微博炫酷可视化音乐组合版来了!项目介绍背景现阶段、抖音、快手、哗哩哗哩、微信公众号已经成为不少年轻人必备的“生活神器”。在21世纪的今天,你又是如何获取外界的信息资源的?相信很多小伙伴应该属于下面这一种类型的:事情要想知道快,抖音平台马上拍;微博热搜刷一刷,聚焦热点不愁卖;闲来发呆怎么办,B站抖音快手来;要是深夜无聊备,微信文章踩一踩;哈哈哈,小小的活跃一下气氛在这个万物互联的时代,已不再是那个“从前慢,车马慢….

    2022年5月20日
    51
  • 判断IPV6地址格式是否正确

    判断IPV6地址格式是否正确1 判断 IPV6 格式字串是否正确此功能代码实现判断 IPV6 地址是否正确 正确返回 1 错误误返回 0 defineH x unsignedchar amp x 0 defineL x unsignedchar amp x 1 char abbr ipv6 字符串 unsignedchar ret buf

    2025年8月21日
    3
  • 迭代与递归的区别「建议收藏」

    迭代与递归的区别「建议收藏」迭代和递归的区别:从“编程之美”的角度看,可以借用一句非常经典的话:“迭代是人,递归是神!”来从宏观上对二者进行把握。递归:重复调用函数自身实现循环称为递归;    递归实际上不断地深层调用函数,直到函数有返回才会逐层的返回,递归是用栈机制实现的,每深入一层,都要占去一块栈数据区域,因此,递归涉及到运行时的堆栈开销(参数必须压入堆栈保存,直到该层函数调用返回为止),所以有可能导致堆…

    2022年6月6日
    26
  • socket技术详解(看清socket编程)

    socket技术详解(看清socket编程)socket编程是网络常用的编程,我们通过在网络中创建socket关键字来实现网络间的通信,通过收集大量的资料,通过这一章节,充分的了解socket编程,文章用引用了大量大神的分析,加上自己的理解,做个总结性的文章1:socket大致介绍   socket编程是一门技术,它主要是在网络通信中经常用到   既然是一门技术,由于现在是面向对象的编程,一些计算机行业的大神通过抽象的理念,在现…

    2022年7月13日
    16
  • 微信小程序+php 授权登陆,完整代码

    微信小程序+php 授权登陆,完整代码先上图实现流程:1、授权登陆按钮和正文信息放到了同一个页面,未授权的时候显示登陆按钮,已授权的时候隐藏登陆按钮,显示正文信息,当然也可以授权和正文分开成两个页面,在授权页面的onload里判断是否

    2022年7月3日
    26
  • 程序员,在北上广深杭赚够100万,就逃回二三四线城市生活,靠谱吗?

    程序员,在北上广深杭赚够100万,就逃回二三四线城市生活,靠谱吗?无意间刷到一个话题,北漂程序员,赚够100万,然后回三四线城市享受生活,不确定这个想法靠不靠谱。讲真,我觉得我在这个话题上还是挺有话语权的。就来给大家唠唠,看能不能提供一丢丢参考价值。我没有在北京搬砖过,但在苏州搬砖过,苏州和北上广深杭比起来,肯定有差距,但不得不承认,苏州的发展速度确实是有目共睹的。目前已经在三线城市洛阳生活了多年,生活品质不能说有多高,但生活的基础条件我觉得都还过得去。尤其是其中很重要的一点,教育资源是能跟得上的。01、十分怀念那苏州说实话,还是挺怀念苏州那段时光的。苏州不

    2022年7月26日
    7

发表回复

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

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