Jeff King
Jeff King

Reputation: 27

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. These are triggered with certain user activity during a browser 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.

Is it even possible for a web service to consume these data access public methods in this manner?

Upvotes: 0

Views: 118

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49329

Sure, you can adopt some web api's and web methods with webforms.

The $100 quesiton then becomes are you planning to call these web services from another site, or the same site?

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.

Of course, if one is looking to "spruce" up a given web page, reduce the number of post-backs? Sure, by all means you can adopt ajax calls to those web methods.

You can create a separate page (url) with web service, or you can simple add some web methods to a existing web page.

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.

And you have quite a few ways to call/consume such web methods.

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.

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).

called from a SQL Server stored procedure

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. So, a web site with some code behind, or even code that consumes some SQL server data? Sure, no big deal, but this introduction of having SQL server call some web service? gooly gee, no, that is not common, nor even supported as far as I can tell.

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). 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).

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.

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).

is it even possible for a web service to consume these data access public methods in this manner?

If you create and expose some web methods? Then sure, but that is a VAST different issue then SQL server consuming such web methods.

But, yes, can you add/have/create/use/enjoy web methods that you create and add to the webforms site? Yes, that option to create such web methods is quite much a option like near most web systems. You have to of course create + write + setup such web methods, but the answer is yes, of course you can.

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?

then in addition to js and jQuery, you can also use what are called "page methods". 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.

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.

Upvotes: 1

Related Questions