0

I think I got the web.config right but I'd want to know how to add the service reference or the web service itself on the aspx page so my script could access it.

here's what I did but it doesn't work:

<%@ ServiceHost Language=C# Service="WebService" CodeBehind="http://urlToMyService.svc"%>

<asp:ScriptManager ID="ScriptManager1" runat="server">
   <Services>
      <asp:ServiceReference 
         Path="http://urlToMyService.svc"/>
   </Services>
</asp:ScriptManager>

I think I need to add the <%@ ServiceHost %> and <asp:ScriptManager> but I'd like a clearer example on every parameters I need to include.

Bahamut
  • 1,929
  • 8
  • 29
  • 51

1 Answers1

1

You can add only references to local services (i.e. services that exist in your ASP.NET web application). So typical service reference goes like

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Services>
        <asp:ServiceReference Path="~/HelloWorldService.svc" />
    </Services>
</asp:ScriptManager>

on aspx page (or master page or user control). You don't need <%@ ServiceHost %> etc - that will appear in svc file.

Note that adding service reference to ScriptManager generates a java script service proxy that will simplify calling your web service from java-script. This is not useful for calling the service from code (on server side). See this tutorial to get started on invoking services from java-script: http://www.codeproject.com/KB/aspnet/wcfinjavascript.aspx

For calling services from server side code, you have to add service reference from Visual Studio (right-click on your project and choose Add Service Reference context menu) - in such case, VS generates the proxy code to make call to the service.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • Btw I am trying to use AjaxPro2 to consume the web service in javascript but it can't detect the reference. So what I did was set the path to ~/Service References/MyWebService/MyWebService.wsdl but I'm having some errors about js method. I'll edit my main post after trying some things out. – Bahamut Sep 16 '11 at 08:39
  • @Bahamut, where is your service located? The service end-point (url) has to be within the web application. What you are using is a path to WSDL which won't work. It needs to be a local url (e.g. url to asmx or svc file within your project). – VinayC Sep 16 '11 at 08:58
  • I see. It's hosted on a separate machine. Could I use the url to it instead? Going to mark this as the answer since the main point of the question was already answered. – Bahamut Sep 16 '11 at 10:48
  • @Bahamut, calling service from a separate machine will make it cross-domain AJAX call. Browsers typically put lot of restrictions on cross-domain calls. You can either create a local proxy service within your application that will redirect calls to remote service OR you can use techniques such as JSONP for cross-domain calls. – VinayC Sep 16 '11 at 10:59
  • does the service reference I added counts as the proxy service? and could I use JSONP for a SOAP web service? – Bahamut Sep 16 '11 at 11:58
  • @Bahamut, adding service reference creates a proxy client code - it is not exposed as a web service by your application. So you need add svc end-point that will forward calls to actual service using the added service reference. JSONP is not SOAP - so you cannot access SOAP end-point. Service needs to support JSONP end-point. – VinayC Sep 19 '11 at 04:32