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


相关推荐

  • DrawerLayout侧滑栏

    DrawerLayout侧滑栏1.DrawerLayout是一个侧滑的布局控件2.以及可以拖拽的一个布局资源3.首先要现在布局文件里面设置好布局,在进行编写代码;第一步:这是最基本的一个布局文件,里面有主界面布局,下面是包含一个button的按钮;<android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.c…

    2022年6月25日
  • 戴尔r740服务器重装系统_戴尔r740安装server2016

    戴尔r740服务器重装系统_戴尔r740安装server2016RAID配置:1.开机F2进入bios2.选择devicesetting3.IntegratedRaidcontroller1xxxxxx(raid卡型号)一般是第一行4.选择MainMenu5.ConfigurationManagement6.选择CreateVirtualDisk7.Raid级别,之后点击selectphysicalDisks8.根据硬盘类型接口选择硬盘,选择硬盘后应用applychanges9.此处可以设定

    2022年10月5日
  • PyCharm许可证过期解决方案

    PyCharm许可证过期解决方案PyCharm许可证过期解决方案1.选择enterlicense2.选择licenseserver3.输入http://idea.imsxm.com4.点击ok

    2022年8月26日
  • php curl_init undefined,php运行出现Call to undefined function curl_init()的解决方法

    php curl_init undefined,php运行出现Call to undefined function curl_init()的解决方法在装好PHP后,执行类似$ch=curl_init();这样的语句,出现Calltoundefinedfunctioncurl_init()的错误提示。解决方法如下:1、在php.ini中找到extension=php_curl.dll,去掉前面的,php.ini一般在c:\windows下面。2、在php.ini中找到extension_dir=“ext”,去掉前面的;,改为ex…

    2022年7月13日
  • [EE261学习笔记] 4.常用的几个傅里叶变换相关公式

    [EE261学习笔记] 4.常用的几个傅里叶变换相关公式在本文开始前,需要说明一点,以下推导出的各项公式,只是为了实际计算中方便,并不都有其对应的物理意义。首先,我们写出符号f−(t)=f(−t)f−(t)=f(−t)f^-(t)=f(-t),显然,对于奇函数而言,f−=−ff−=−ff^-=-f;对于偶函数而言,f−=ff−=ff^-=f。根据前文傅里叶变换推导,我们知道…

    2022年7月17日
  • 网络-访问控制

    网络-访问控制1.访问控制访问控制就是限制访问主体对访问客体的访问权限控制,决定主体对客体能做什么和做到什么程度访问主体(主动):用户,进程,服务访问客体(被动):数据库,资源,文件2.访问控制的两个过程认证:先由认证来检验主体(用户)的合法身份,在访问控制之前授权:由管理员决定和限制主体(用户)对资源的访问级别!!注意:审计也在主体对客体访问的过程中,但是,审计是访问过程中,对访问情况的记录和审查,他只是产生一些log,用来分析安全事故产生的原因,和访问控制无关,就是个辅助用的,可要可不要。3.访问控制

    2022年7月23日

发表回复

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

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