1

Firstly I must apologise if my terminology is off. This is the first time I've explored .asmx so I've probably gotten some things wrong. I'm mentioning this because I want you to correct me as it is an invaluable learning experience and I need to get it right.

I'm writing an Ajax enabled .asmx web service to allow two browser based systems to talk to each other. In order for this to work I must initially authenticate with one of the systems to get the URL to use for all subsequent API calls to that same system. I only want to do this one time when the service is started and then remember this across all calls to all webmethods.

  • How do I run a method only one time when the service is loaded by IIS?
  • How do I store the result globally so that it can be accessed by every call to my webmethods?

I've seen two posts that touch on this ( 1 | 2 ) but neither made enough sense to help me.

I think I need to add a Global.asax file and store the details in there, but I'm not sure if this is right and I still don't know how to run the method only once. Can I just override Application_Start in here? This is what I would do on an ASPX page but I don't know how similar these two technologies are.
I've seen recommendations to use the constructor to run the method but it's my understanding that this may be called more than once so would be incorrect for my scenario.

For clarity my project currently only has two files, handler.asmx and handler.cs.

Handler.asmx

<%@ WebService Language="C#" CodeBehind="~/App_Code/handler.cs" Class="Handler" %>

Handler.cs

[WebService(Namespace = "http://localhost/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[ScriptService]
public class Handler : WebService {

    public Handler () {
    // empty
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetSomeInformation() {
    // does some stuff and then returns a string
    }
}

This is what the Global.asax looked like after I added it:

Global.asax

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
    }

    void Application_End(object sender, EventArgs e) 
    {
    }

    // and all the other methods here like error, session start/end etc
Community
  • 1
  • 1
Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • Yes, you can create a global.asax and add the standard event handlers such as application_start, etc. – OldProgrammer Nov 30 '15 at 16:38
  • I've added a `Global.asax` file but I can't see that `Application_Start` ever fires. I'm running `IISRESET` each time which should fire this event. I've tried inserting a break point in there and also writing a local file, but neither seem to happen. I'm unsure what might be wrong. – Equalsk Nov 30 '15 at 17:06
  • Do you have something like this: public class Global : System.Web.HttpApplication { public Global() { InitializeComponent(); } protected void Application_Start(Object sender, EventArgs e) { } – OldProgrammer Nov 30 '15 at 17:08
  • I could be wrong but I didn't think ASMX services derived from HttpApplication so I have nothing like that in my project. For clarity I have added a basic outline of what the service looks like above. There's really not much more code than that. Further reading seems to suggest that this is also the reason why global.asax might not work but I can't find concrete evidence either way so I'm a little confused! – Equalsk Nov 30 '15 at 17:17
  • Not asmx services, the global.asax does – Oguz Ozgul Nov 30 '15 at 17:23
  • Can you add your global.asax file? Asp.Netdhould call the application_start once during start-up – Oguz Ozgul Nov 30 '15 at 17:25
  • I've added the global.asax already, it's the last piece of code in the question. – Equalsk Nov 30 '15 at 17:25
  • This should work. Does your breakpoint not hit when you start with F5 – Oguz Ozgul Nov 30 '15 at 17:29
  • If don't want to deal with this, define a static constructor for your service class an deal with things there.. – Oguz Ozgul Nov 30 '15 at 17:30
  • It will be invoked only once, but should implement it with extra caution. Any failure is Fatal and will require a recycle – Oguz Ozgul Nov 30 '15 at 17:32
  • No, the breakpoint is never hit. I'd read about using the constructor but there were issues with this and I was trying to avoid it if possible. Global.asax definitely seems like the right solution, I'm just at a loss as to why it doesn't work. I have to leave now so I'll play with this overnight and see what I can do. Thanks for your help so far. – Equalsk Nov 30 '15 at 17:34
  • Why don't you instead implement an on-demand approach? Get the necessary information when it is needed, cache it, and return from cache for the rest of the calls. – Oguz Ozgul Nov 30 '15 at 17:34

0 Answers0