views:

24

answers:

1

I'm trying to call an ASP.NET .asmx webservice, from javascript, using other than the default namespace that Visual Studio uses when it creates it.

When I use the Visual Studio wizard to create a webservice named Hello, in the folder WebServices, it creates this:

namespace MyWebSite.WebServices
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class Hello : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

It's made visible to the javascript in the browser like this:

ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);

ServiceReference serviceReference = new ServiceReference();
serviceReference.Path = "~/WebServices/Hello.asmx";
serviceReference.InlineScript = false;

scriptManager.Services.Add(serviceReference);

And it's called from Javascript like this:

MyWebSite.WebServices.Hello.HelloWorld(function(rval){{alert(rval);}});

That works fine. My problem is that my calling javascript is in a separate assembly, as a part of a server control, that expects a different namespace. I either need to rework how the assembly works - making the namespace of the webservice a parameter, in some way, or I need to change the namespace of the webservice, so that it matches the call.

Currently, I'm exploring the latter. (Because changing the assembly will mean changing the other website that also uses this assembly, and I'd rather not, unless it's necessary).

So, I want my Javascript call to look like this:

OtherNamespace.WebServices.Hello.HelloWorld(function(rval){{alert(rval);}});

I'd thought I could just change the namespace in the .asmx file:

namespace OtherNamespace.WebServices
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    ...

But that doesn't work. I get an "OtherNamespace is not defined" error. Clearly, even though the namespace in the .asmx file and the prefix in the javascript call have the same value, they aren't referencing the same thing.

Where does the MyWebSite.WebServices. prefix, in the calling javascript come from? Where is it defined? And how can I change it so that it is something else?

+2  A: 

Did you update both the ASMX and teh code-behind files too? There is a reference in the ASMX markup too.

Brian
Nope. That was what I was missing. VS had been bringing up the designer, when I opened the .asmx file, which shows nothing of use. I opened the markup explicitly, and saw what needed to be changed.
Jeff Dege