views:

31

answers:

1

I have a class library. In one of the classes, I am adding a script reference on the page like this:

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        if (this.Page != null)
        {
            ScriptManager sm = ScriptManager.GetCurrent(this.Page);
            ServiceReference reference = new ServiceReference("~/Admin/Services/ContactsService.asmx");
            reference.InlineScript = true;
            sm.Services.Add(reference);
        }
    }

For the ServiceReference file path, is there a way to add an embedded file instead? I want to keep everything self-contained in my class library instead of dropping a file into the website folder.

A: 

If I understand correctly what you're asking, this won't be possible to do because a WebService is an actual compiled class and a WebMethod is an actual compiled method.

The GetWebResourceUrl method does what the name implies, returns a resource. Client script (Javascript) is just a string resource, it does not need to be executed on the server, it is simply sent to the client as text. Web services (and their methods) need to have a physical endpoint capable of responding to client requests and executing this code.

In theory, if you were using WCF, you could determine the URL of the service endpoint at runtime, because WCF decouples the endpoint from the behaviour and defines it in a specific place in your web.config. That wouldn't allow you to provide the whole "service" as a WebResource but it would avoid the need to hard-code the URL. If you're using ASMX WebServices, then you don't even have that option, because ASMX uses a markup file similar to an ASP.NET page.

If you're worried that the URL might change, i.e. because you deploy this web site on multiple servers/domains, then the best thing to do would be to externalize the URL as an application setting. That way you would only need to edit the web.config to change what URL is used for the script reference.

Aaronaught