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


相关推荐

  • 潜意识的牢笼——为什么这件事情这么难

    潜意识的牢笼——为什么这件事情这么难

    2021年8月26日
    63
  • 太极阴,阳虚拟框架—-各种插件大总结(烂尾)[通俗易懂]

    太极阴,阳虚拟框架—-各种插件大总结(烂尾)[通俗易懂]最近心血来潮又想起了折腾自己的安卓手机,不由得就想起来了几年前的Xposed框架.于是又开始跃跃欲试起来然而在网上冲浪许久后,虽然人们七嘴八舌但我大概还是看出来了Xposed对于高版本android好像已经不太能用了,更何况我用的还是MIUI于是,我发现了一个新的玩意—-太极框架.(咳,应该也不是啥新东西了只不过我才关注到而已)当然,现在还有好多类似的东西,但这不是我们的主题….

    2022年6月4日
    166
  • js有几种数据类型,基本数据类型有哪些_js中简单数据类型

    js有几种数据类型,基本数据类型有哪些_js中简单数据类型js中数据类型分为原始数据类型与引用数据类型原始数据类型:number//数值类型:值只能为数字vara=12string//字符串类型:值都是以引号包裹的vara=’你好’boolean//布尔类型:值只存在真假vara=truevara=falsenull//空类型vara=nullundefined//未定义类型:定义了变量a,但是没有a赋值varaconsole.log(a

    2025年9月18日
    6
  • ubuntu完全卸载CUDA

    ubuntu完全卸载CUDACUDA的卸载方法网上都有很多,但是几乎都是错的,我在卸载cuda时基本试了个遍,各种踩坑。能查到的方法一般都是从官方文档搬过来的,然而这种方法并不能将cuda完全卸掉。这里把官方文档的方法贴出来:sudoapt-get–purgeremove”*cublas*””*cufft*””*curand*”\”*cusolver*””*cusparse*””*npp*””*nvjpeg*””cuda*””nsight*”我运行过这个命令,运行完之后,命令行输入nvcc-

    2022年5月30日
    80
  • Oracle与Sql Server复制表结构和数据

    Oracle与Sql Server复制表结构和数据

    2022年1月10日
    53
  • 字符串中最长的回文字符串长度

    字符串中最长的回文字符串长度1、回文字符串  回文字符串是指aba类型的字符串,即字符串关于中间字符对称。判断字符串中是否含有回文、得到最长回文字符串的长度、得到不同回文字符串的个数等等,是经常考察的编程题目。2、之前采用的一种比较笨的得到最长回文字符串的方法  思想:双重指针遍历,根据回文字符串的特点,回文开始的字符与结尾处字符相同……那么一个指针i从前向后遍历,一个指针j从后向前遍历,如果出现

    2022年6月4日
    35

发表回复

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

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