Creating a web service without an ASMX file? - asp.net

I have written an ASP.NET composite control which includes some Javascript which communicates with a web service.
I have packaged the classes for the control and the service into a DLL to make it nice and easy for people to use it in other projects.
The problem I'm having is that as well as referencing the DLL in their project, the consumer of my control must also include a .ASMX file for the web service. Whilst it isn't a complicated file (just a one-liner which refers to the class in the DLL), I would like to avoid having it if I can.
Is there any way to avoid having to have the .ASMX file?
Can I register the service with the web server in Application_Start?
Can I make a web.config change to reference it somehow?
All suggestions gratefully received!
UPDATE: The article linked to in John Sheehan's response (below) does work - but not if you want to call the web service using AJAX. Does anybody know of an AJAX friendly version?

Try something like this. I don't know if it will work though. I got this idea from ELMAH, which creates a handler for a page that doesn't physically exist and then serves it up from the assembly.
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="*WebService.asmx" type="MyHandler.WebServiceHandler, MyHandler" />
</httpHandlers>
</system.web>
</configuration>
EDIT: I was close, see this article (in VB though): http://www.codeproject.com/KB/aspnet/wsinaclasslibrary.aspx

I know this is very old question, but it hasn't been answered properly, so here it is:
Every *.ASMX request is by default handled by System.Web.Services.Protocols.WebServiceHandlerFactory.
Looking into source code of this class in .NET reflector, it's possible to have webservice without ASMX file but you will need to call internal method CoreGetHandler through reflection.
Following method will take your webservice and return its IHttpHandler.
public IHttpHandler GetHttpHandlerForWebService(WebService webService, HttpContext context)
{
var webServiceType = webService.GetType();
var wshf = new System.Web.Services.Protocols.WebServiceHandlerFactory();
var coreGetHandler = wshf.GetType().GetMethod("CoreGetHandler");
var httpHandler = (IHttpHandler)coreGetHandler.Invoke(wshf, new object[] { webServiceType, context, context.Request, context.Response });
return httpHandler;
}
Once you have your httphandler, it's just matter of calling
httpHandler.ProcessRequest(context)
Done. No ASMX and no web.config entries.

Here is a very good working article about your problem:
Creating Web Services in a Class Library project on Codeproject.

Consider trying page methods (see this blog post). All you have to do is add the web method attribute to a static method in the aspx code behind. Then access the PageMethod object from your clientside code (javascript). No ASMX files needed. Hope this helps.

Short answer is no. The ASMX is the entry point for any web service. There are alternatives if you use WCF, but that's not entirely the same thing.

Related

Something is seriously wrong with my ASP.NET MVC

I have been programming ASP.NET Web Forms for a long time and recently decided to learn ASP.NET MVC.
However, I am clearly having some issues that I cannot tell how. No one that I have come across in web had the same problems.
#1: Web.config issues
When I first created my MVC project in VS2019, there was no web.config so I added it via project > add new item> Web Configuration File.
All I needed it for was to store my connection string. When added it had only two lines of code:
<configuration>
</configuration>
I added my connection string and the file looked like this:
<configuration>
<connectionStrings>
<add name="connection" connectionString="myconnectionstringblablabla" />
</connectionStrings>
</configuration>
In my controller I tried to fetch the connection string by doing,
string constr = ConfigurationManager.ConnectionStrings["connection"].ConnectionString;
And this happened:
Same things happens when fethcing AppSetting keys from web.config.
Is web.config not meant to be used for ASP.NET MVC?
#2 Global.asax
Unlike web.config, Global.asax file doesn't even show up on the add new item menu:
Tried this solution: How to add a Global.asax file to ASP.NET MVC5 project Didnt work either.
#3 Session & HttpContext
In ASP.NET Web.Forms, I would normally use Session variables like this:
Session["ClientID"] = somevalue;
I can't do that in ASP.NET MVC, although I have seen people doing it.
Also I can't access to HttpContext from a non-controller class:
HttpContext.Current.Request.Cookies["mycookie"];
It throws HttpContext does not contain a definition for 'Current'...
====================================
Is Something wrong with my program. I don't know how to check if I am using correct references or assemblies, but I think I am missing something.
Please help

Allow Html Content to be posted

Recently i have faced an interview question.
You have finished an ASP.NET application using .NET framework 3.5.
You plan to submit text that contains HTML code to a page in the application.
You need to ensure that the HTML code can be submitted successfully without affecting other applications that run on the web server.
What would be your option?
Add the following in Web.Config
<system.web>
<pages validateRequest="false"/>
</system.web>
Add the following in Machine.Config
<system.web>
<pages validateRequest="false"/>
</system.web>
I guess the correct answer could be (1). Just wish to confirm the answer from SO experts. Please help me.
You can also decorate the single action method with
[ValidateInput(false)]
public ActionResult Index()
{
return View();
}
This will result in more security because you have the
control when request validation happens.
1) is correct. by putting the code in machine.config you will affect the whole machine as this are global settings.
You plan to submit text that contains HTML code to a page in the
application.
Since it's just "a page," it would be much better to disable validation checking for that page alone, rather than for the entire application (web.config), much less for all apps on that server (machine.config).
To do that, set ValidateRequest="false" on the #Page directive.
without affecting other applications that run on the web server
This is the key, web.config will only affect the one application. Wheras machine.config will affect all over applications running on the server.

Custom VirtualPathProvider not being used in IIS6

I added the following lines to Application_Start method in global.asax:
var provider = new TestVirtualPathProvider();
HostingEnvironment.RegisterVirtualPathProvider(provider);
Yet the 'TestVirtualPathProvider' is never used when deploying this application in IIS6 (it does in the ASP.NET Development Server).
Edit: the default path provider has always done its job correctly and served (non-embedded) views correctly. The problem is simply that I want to use my own path provider to provide embedded views. So, initially, I already had the following wildcard mapping configured:
Any possible reasons why this does not work in IIS6?
Are there any other factors (handlers for example) wich might influence the used VirtualPathProvider?
UPDATE: the fact that you want to handle extension-less URL's is an important point that's not mentioned in the question. Please see this page for help in setting up MVC with IIS 6: http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx. This should cover your scenario as well.
Most likely the same issue that I answered in this thread: http://forums.asp.net/t/995633.aspx
Basically, add this in your web.config:
<httpHandlers>
<add path="*" verb="GET,HEAD,POST" type="System.Web.StaticFileHandler" validate="true" />
</httpHandlers>
That other thread has some details that explain why this is necessary.
For the combination Custom VPP + IIS6 + Precompiled site, we need to add the VPP from AppInitailize();
public static class AppStart
{
public static void AppInitialize()
{
// code to be executed automatically by the framework
}
}
See also:
http://sunali.com/2008/01/09/virtualpathprovider-in-precompiled-web-sites/
I believe that you need to use an ISAPI filter in IIS6 to intercept URLs without extensions. Problem is that ISAPI will need to be done in c/c++.
IIS6 is configured to allow only certain extensions to be processed by the ASP.net pipeline.
To findout how you can redirct requests check out the post by DocV.

ASP.Net: Create handler within project

Sorry if this is a stupid question. I want to create an HTTP handler within my project. To do this, I created a new class, and tried to implement IHttpHandler.
Each time I do this, I get the error 'End of statement expected'.
I know that normally you can create a new class library and create a class to implement a handler. But why is it not possible to do this in an existing web project?
WT
Create a new object called an ashx or Generic Handler. From there you can put in server side code and output the exact result you would like to dish out. Honestly though I would use WCF though, its a lot easier and you can spit out the data in any data type you like.
Even though its not a best practice to put it in the web project, are you referencing it in the web.config correctly, so it knows to look in the App_Code folder?
<httpHandlers>
<add verb="*" path="*.yourextention" type="YourHandler, App_Code"/>
</httpHandlers>

What's the difference between making a handler w/ ashx/axd and using something I made up myself in ASP.NET?

This is probably very simple but it's really confusing me. When I implement the IHttpHandler, I create a handler and then register it like so in the web.config:
IIS6 Portion:
<httpHandlers>
<add verb="*" path="*.randomextension" type="MyProgramNameSpace.MyHandler" />
</httpHandlers>
IIS7 Portion:
<handlers>
<add name="mine" verb="*" path="*. randomextension" type ="MyProgramNameSpace.MyHandler" />
</handlers>
It seems to work quite well and get to use different handlers and options for it. It let's me skip the Page class and so forth by directly accessing the pipeline. However, every so often I keep running into documentation where it says I need to use something about ashx or axd with something.
What is all this about? How does this have to do w/ handler creations?
This is probably very easy but for some reason I'm completely confused when going about this ashx or axd handler.
The .asxh handler is simply a pre-existing/pre-defined generic handler mapping. Unlike the .aspx handler, you are not restricted to deriving from Page, and you don't get the full ASP.NET page handler event pipeline. Generally, you use .ashx files to handle non-page requests that take as input or return as output non-standard content.
The different from an .ashx handler and a custom IHttpHandler is not much, really. A lot of configuration is predefined for .ashx files, but, you are bound to that extension. With a full custom IHttpHandler, you have complete and total freedom, but need to configure it from the ground up.
There really isn't a difference. .ashx files implement IHttpHandler just like you are doing. Only .ashx is a pre-registered handler so you don't need to add it to the web.config yourself it's already done for you.
If you are deciding to use the extension by file type your handler is appropriate.
If on the other hand, you are trying to return data, without a particular extension, the ashx/ahd extension is just as good.
For example, if you have a collection of images stored in a database, you could register a .JPG handler, that would pull the image from the database instead of the hard drive. You could also create an ASHX that could return any image type.
Registering an extension could make the url look more "normal" to the end user, while an ashx looks more generic (even geeky).

Resources