I've created a scriptservice in my vb.net 4.0 web app (or web site... not sure which it is - does it matter?) because I want to call it from the client side. I get client errors saying my namespace is not recognized (HomepageService in the code below). I've tried qualifying it with the name configured on the project as "Root namespace", but the js says it doesn't recognize that namespace either.
The app is old - we recently converted it from dotnet 2.1 to 4.0.
I found the following related topic, because when I try to Import System.Web.Extensions in my HomepageServices.asmx.vb, visual studio says it doesn't recognize the thing even though i can see it, listed right there in studio under References.
[System.Web.Extensions Assembly cannot be resolved][1]
I tried posting a followup question to that topic, becauxse I tried following the instructions in the answer but they didn't work (I don't have a "Target Framework" in Project > Properties > Application), but someone deleted it because I guess i'm not allowed to ask followup questions?
I've consulted various sites and followed instructions, here's an example: http://www.asp.net/ajax/documentation/live/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx
Here is contents of HomepageService.asmx sitting in the root folder of my site:
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Collections.Generic
<System.Web.Services.WebService(Namespace:="http://localhost/appname")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<System.Web.Script.Services.ScriptService()> _
Public Class HomepageService
Inherits System.Web.Services.WebService
Shared _rand As Random = New Random(Environment.TickCount)
<WebMethod()> _
Public Function Test(ByVal s As String) As Integer
Return _rand.Next(0, 120)
End Function
End Class
Snippet from master page:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="Scriptmanager1" runat="server" EnablePageMethods="true">
<Scripts>
<asp:ScriptReference Path="CallWebServiceMethods.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="HomepageService.asmx" />
</Services>
</asp:ScriptManager>
and at the top of my page i import the js:
PageRequestManager.js:
HomepageService.set_defaultSucceededCallback(
OnLookupComplete);
HomepageService.set_defaultFailedCallback(
OnError);
function OnLookup() {
HomepageService.Test(stb.value);
}
function OnLookupComplete(result, userContext) {
// userContext contains symbol passed into method
var res = document.getElementById("_resultLabel");
res.innerHTML = userContext + " : <b>" + result + "</b>";
}
function OnError(result) {
alert("Error: " + result.get_message());
}
My web.config is a mess, but I'll be happy to post it...
Finally got it working. Here's how I did it. Not sure what the problem was though.. but following the below process did the trick.
Step 1. Follow the walkthrough at http://msdn.microsoft.com/en-us/library/bb532367(v=vs.90).aspx, using a new website project.
It works. I'll call this the model project.
Step 2. Create files in my existing web app and copy all the code from the model project: - created WebService HelloWorld.asmx (studio also gives me a nested file, Helloworld.asmx.vb) in my project's root folder. This is different from the model project, because when I created the webservice in that project, it put the .asmx in my root folder but it sticks the .asmx.vb in the App_Code folder. Not sure whether I should be moving it manually or something? In the past when I've tried to use App_Code or App_data folders with this web app, everything has gone to hell...
Step 3. Edit the webservice to work with my web app instead of the website: - Namespace Samples.Aspnet commented out, not replaced with a different namespace since my app has a default namespace 'Fubar', which is the same as my app's project name.
Here is the resulting code in HelloWorld.asmx.vb in root folder of my web app:
Imports System
Imports System.Web
Imports System.Collections
Imports System.Web.Services
Imports System.Web.Services.Protocols
'Namespace Samples.Aspnet
<WebService([Namespace]:="http://mycompany.org/"), _
WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1), _
System.Web.Script.Services.ScriptService()> _
Public Class HelloWorld
Inherits System.Web.Services.WebService
Public Sub New()
End Sub 'New
'Uncomment the following line if using designed components
'InitializeComponent();
<WebMethod()> _
Public Function Greetings() As String
Dim serverTime As String = _
String.Format("Current date and time: {0}.", DateTime.Now)
Dim greet As String = "Hello World. <br/>" + serverTime
Return greet
End Function 'Greetings
End Class 'HelloWorld
'End Namespace
And here is the code in HelloWorld.asmx, also in the root folder:
<%@ WebService Language="vb" CodeBehind="HelloWorld.asmx.vb" Class="Fubar.HelloWorld" %>
Step 4. Create the js "HelloWorld.js", stick it in root folder like it is in the model project, and paste the code from the model project. Make one edit to match my app's namespace. Here's the resulting code:
var helloWorldProxy;
// Initializes global and proxy default variables.
function pageLoad() {
// Instantiate the service proxy.
// helloWorldProxy = new Samples.Aspnet.HelloWorld();
helloWorldProxy = new Fubar.HelloWorld();
// Set the default call back functions.
helloWorldProxy.set_defaultSucceededCallback(SucceededCallback);
helloWorldProxy.set_defaultFailedCallback(FailedCallback);
}
// Processes the button click and calls
// the service Greetings method.
function OnClickGreetings() {
var greetings = helloWorldProxy.Greetings();
}
// Callback function that
// processes the service return value.
function SucceededCallback(result) {
var RsltElem = document.getElementById("Results");
RsltElem.innerHTML = result;
}
// Callback function invoked when a call to
// the service methods fails.
function FailedCallback(error, userContext, methodName) {
if (error !== null) {
var RsltElem = document.getElementById("Results");
RsltElem.innerHTML = "An error occurred: " +
error.get_message();
}
}
if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
Step 5. Add code to my ScriptManager tag in my master page. This corresponds to the model project's code in Default.aspx; the ScriptManager tag below is identical to the one in the model project:
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" ID="ScriptManager">
<Services>
<asp:ServiceReference path="~/HelloWorld.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Path="~/HelloWorld.js" />
</Scripts>
</asp:ScriptManager>
Step 5. Add controls to my test page like they have in Default.aspx in the model project:
<div id="divTestWebServices">
<button id="Button1" onclick="OnClickGreetings(); return false;">Greetings</button>
<div>
<span id="Results"></span>
</div>
</div>
Step 6. Run Fubar and navigate to test page, then hit the button.
It works!!! I don't know why it wouldn't work before, but after three days on this I'm ready to move on now ...