简体   繁体   English

创建 web 服务以从 C# ASP.NET webforms 应用程序访问 class object 公共方法

[英]Create web service to access class object public methods from C# ASP.NET webforms application

In my C# ASP.NET webforms application, there are data access classes which have public methods to insert/delete database records.在我的 C# ASP.NET webforms 应用程序中,有一些数据访问类具有插入/删除数据库记录的公共方法。 These are triggered with certain user activity during a browser session.这些是在浏览器 session 期间由某些用户活动触发的。

In order to not reinvent the wheel and to avoid duplicating code, I would like to instantiate these class objects and call those methods from outside the application, possibly using a web service called from a SQL Server stored procedure, on a monthly schedule using a SQL Server Agent Job.为了不重新发明轮子并避免重复代码,我想实例化这些 class 对象并从应用程序外部调用这些方法,可能使用从 SQL 服务器存储过程调用的 web 服务,每月使用 SQL服务器代理作业。

Is it even possible for a web service to consume these data access public methods in this manner? web 服务是否有可能以这种方式使用这些数据访问公共方法?

Sure, you can adopt some web api's and web methods with webforms.当然,您可以通过 webforms 采用一些 web api 和 web 方法。

The $100 quesiton then becomes are you planning to call these web services from another site, or the same site?那么 100 美元的问题就变成了你打算从另一个站点还是同一个站点调用这些 web 服务?

I mean, in "most" cases, if this is the same site, then LITTLE reason to go though all that song and dance, since code behind can direcly use that class anyway, and no messay web methods or client side JavaScript is required.我的意思是,在“大多数”情况下,如果这是同一个站点,那么 go 就没什么理由了,尽管所有这些歌曲和舞蹈,因为后面的代码无论如何都可以直接使用 class,并且不需要杂乱的 web 方法或客户端 JavaScript。

Of course, if one is looking to "spruce" up a given web page, reduce the number of post-backs?当然,如果想要“美化”给定的 web 页面,减少回发的数量? Sure, by all means you can adopt ajax calls to those web methods.当然,您可以通过 ajax 调用那些 web 方法。

You can create a separate page (url) with web service, or you can simple add some web methods to a existing web page.您可以使用 web 服务创建一个单独的页面(url),或者您可以简单地将一些 web 方法添加到现有的 web 页面。

I often do this (place the web methods in the say web page), since it makes sense to have such code behind in the SAME web page that you will use/want to call such web methods.我经常这样做(将 web 方法放在 web 页面中),因为将此类代码隐藏在您将使用/想要调用此类 web 方法的 SAME web 页面中是有意义的。

And you have quite a few ways to call/consume such web methods.你有很多方法可以调用/使用这样的 web 方法。

Built in, they support SOAP calls (XML, not used much these days), or jQuery.AJAX, and they support out of the box both XML and JSON, and do so without any extra coding on your part.内置,它们支持 SOAP 调用(XML,这些天使用不多),或 jQuery.AJAX,它们支持开箱即用的 XML 和 JSON,无需您进行任何额外编码。

And you can even if you wish use a URL with query parameter's (so, that would be a REST call, and again all of these 3 options work, and work without changes to the web method).即使您希望使用带有查询参数的 URL 也可以(因此,这将是一个 REST 调用,并且所有这 3 个选项都有效,并且无需更改 web 方法即可工作)。

called from a SQL Server stored procedure从 SQL 服务器存储过程调用

Ouch!!!哎哟!!! - we were doing so good up to that point!!! - 到那时我们做得很好!!!

I am not aware that SQL server has ANY provisions for calling web services and web methods.我不知道 SQL 服务器有任何调用 web 服务和 web 方法的规定。 So, a web site with some code behind, or even code that consumes some SQL server data?那么,一个 web 站点有一些隐藏代码,甚至是消耗一些 SQL 服务器数据的代码? Sure, no big deal, but this introduction of having SQL server call some web service?当然,没什么大不了的,但是这个让 SQL 服务器调用一些 web 服务的介绍? gooly gee, no, that is not common, nor even supported as far as I can tell. gooly gee,不,据我所知,这并不常见,甚至不受支持。

What you can do is of course adopt some .net code, and create SQL server stored procedures written in .net code (and that WOULD get you use of web service calls from SQL server).你可以做的当然是采用一些 .net 代码,并创建用 .net 代码编写的 SQL 服务器存储过程(这将使你使用来自 SQL 服务器的 web 服务调用)。 In other words, you would have to change some of the SQL server permissions (to allow T-SQL to call/consume .net code. This of course is a great option, and is a way to write SQL server stored procedure code in c# (or vb.net).换句话说,您必须更改一些 SQL 服务器权限(以允许 T-SQL 调用/使用 .net 代码。这当然是一个很好的选择,并且是在 c# 中编写 SQL 服务器存储过程代码的一种方法(或 vb.net)。

So, for the most part, SQL server and its code language (T-SQL) certainly does not allow nor have any means to call/consume/use web methods from a web site.因此,在大多数情况下,SQL 服务器及其代码语言 (T-SQL) 肯定不允许也没有任何方式从 web 站点调用/使用/使用 web 方法。

However, if you introduce CLR code to that SQL server (and assuming it supports that option (some hosting sites don't allow CLR code with SQL server. SQL Azure did not for a long time, but I believe of recent, they allow this now, but its a security risk).但是,如果您将 CLR 代码引入该 SQL 服务器(并假设它支持该选项(某些托管站点不允许 SQL 服务器使用 CLR 代码。SQL Azure 已经很长时间没有了,但我相信最近,他们允许这样做现在,但它存在安全风险)。

is it even possible for a web service to consume these data access public methods in this manner? web 服务是否有可能以这种方式使用这些数据访问公共方法?

If you create and expose some web methods?如果您创建并公开一些 web 方法? Then sure, but that is a VAST different issue then SQL server consuming such web methods.那么可以,但这是一个 VAST 不同的问题,然后 SQL 服务器使用这样的 web 方法。

But, yes, can you add/have/create/use/enjoy web methods that you create and add to the webforms site?但是,是的,您可以添加/拥有/创建/使用/享受您创建并添加到网络表单站点的 web 方法吗? Yes, that option to create such web methods is quite much a option like near most web systems.是的,创建此类 web 方法的选项与大多数 web 系统非常相似。 You have to of course create + write + setup such web methods, but the answer is yes, of course you can.您当然必须创建 + 编写 + 设置这样的 web 方法,但答案是肯定的,您当然可以。

And as noted, you can call/use/consume such web methods with JavaScript, jQuery (what I use most), or if that client side code is in the current web page?如前所述,您可以使用 JavaScript、jQuery(我最常使用的)调用/使用/使用此类 web 方法,或者该客户端代码是否在当前 web 页面中?

then in addition to js and jQuery, you can also use what are called "page methods".那么除了js和jQuery之外,还可以使用所谓的“页面方法”。 This is just a "much" more clean way of calling/using those web methods, but all 3 choices (js, jQuery, js and web method) are available.这只是调用/使用那些 web 方法的一种“更”简洁的方法,但是所有 3 个选择(js、jQuery、js 和 web 方法)都可用。

I'm going for a coffee break, but I will come back and post the most simple example of how a web method can be created for webforms - it not really much different then other later flavors of asp.net.我要去喝杯咖啡休息一下,但我会回来发布一个最简单的示例,说明如何为 Web 表单创建 web 方法 - 它与 asp.net 的其他后续版本并没有太大区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 C# 应用程序调用 ASP.net Web 服务 - Calling ASP.net Web Service from C# Application vb.net asp.net webforms application> c#asp.net mvc razor应用程序 - vb.net asp.net webforms application > c# asp.net mvc razor application 请求失败,HTTP 状态为 401:已授权从 C# asp.net 访问 sharepoint Web 服务 - The request failed with HTTP status 401: authorized for access sharepoint web service from C# asp.net 如何从Web服务代码(C#)访问固有的ASP.NET对象? - How do I access intrinsic ASP.NET objects from web service code (C#)? 为什么从asp.net c#应用程序进行的Web服务调用返回null? - why the web service call from asp.net c# application returns null? 将JSON从ASP.net Web服务反序列化为C#对象 - Deserializing JSON from ASP.net web service into C# object 从ASP.Net中的常规类访问应用程序对象 - Application object access from regular class in ASP.Net ASP.NET Core 2.1,C#应用程序,C#Web服务,访问SQL数据库 - asp.net core 2.1, C# app, c# web service, access SQL database 将类对象从C#应用程序上载到.asp Web API - Upload class object from C# application to .asp Web API C#控制台应用程序和ASP.NET WebForms应用程序之间的共享存储 - Shared storage between C# console application and ASP.NET WebForms application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM