Using VS 2008 with Visual Basic I am trying to develop a web service to provide data from my web site's database to a Windows program.
1. Using the templates I created a web service in a new project within the solution for my GDBChart Windows program. At this stage the web service contains a class, service.vb, with the default "Hello World" function only. This tests OK: select service.asmx and press F5 displays the "Service Web Service web page showing HelloWorld, clicking HelloWorld shows an [Invoke] button and the SOAP 1.1, SOAP 1.2, and HTML POST formats, and finally clicking [Invoke] returns XML <string>Hello World</string>. Everything is OK so far.
2. I added a service reference to the GDBChart project, naming it GDBWebService. Solution Explorer now shows: -
... (various other stuff, not relevant)
3. Within Form1 I added an event handler to use the web service. Within this I wrote
Dim GDBW as New GDBWebService. ...
and VS2008 found GDBWebService and gave options of
Shouldn't I have a "HelloWorld" method? The examples of Web Services all seem to have the equivalent of "HelloWorldString = GDBW.HelloWorld". I tried using HelloWorldRequest and ServiceSoapClient, but neither produced the results I wanted, and usually failed with a VB runtime error.
I'm sure that there is something simple that I've misunderstood and am getting wrong, and once this is sorted I'll be away and can get on to real web services that access the database in useful ways. What have I screwed up?
Thank you, Robert Barnes
you should be able to type gdbwebservice without the new or dim and get some intellisence.
You need to get an instance of the web service class before you can call the HelloWorld method. My VB syntax is more than a little rusty, but something like:
Dim GDBW as New GDBWebService()
This is exactly what I had expected to write, based on the book I am trying to follow. However when I write
Dim GDBW As New GDBWebService()
I get an error message "Type Expected".
If I write
Dim GDBW As New GDBWebService.ServiceSoapClient
then this compiles without a message, as does the following statement
Dim HelloWorldString as String = GDBQ.HelloWorld
but it fails on execution, with message "InvalidOperationException was unhandled" with details "An endpoint configuration section for contract 'GDBWebService.ServiceSoap' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name. "
I suspect that I should be able to write code like yours, but I'm missing something that defines GDBWebService the right way. Perhaps an Imports statement?
Intellisense for GDBWebService, without Dim or New, is the same as before except that the entry "ServiceSoapClient" doesn't appear, but "ServiceSoap" and "ServiceSoapChannel" do. Either way, I think I need to create an instance of this class with Dim xxx As New GDBWebService. My book says "Call the XML Web Service through the proxy. ... You instantiate it as you would any other class" and the book's example is "QuoteServiceSoapClient quoteservice = new QuoteServiceSoapClient()"
How did you create the web service? If you just create an empty asmx file, it should work as with my previous code.
The code fragment I posted should work for a vanilla asmx file (the "Web Service" template in Visual Studio) -- but such a file shouldn't include the other methods that you've mentioned.
The web service was created from the VS2008 template, with no changes. When I created the service reference, I clicked the [advanced] button and checked "Generate Asynchronous Methods", as I am going to need these later. I believe that these are why there are the extra methods: according to my book if this is checked then they are generated AS WELL AS the synchronous methods, and by choosing whether to use "HelloWorld" or "BeginHelloWorld" you choose whether to implement a synchronous or asynchrounous call. My problem is that I see neither "HelloWorld" or "BeginHelloWorld", instead I see things like "HelloWorldRequest".
The asmx file contains only one line: -
<%@ WebService Language="vb" CodeBehind="~/App_Code/Service.vb" Class="Service" %>
Oh, now I see. It sounds like you have both a "Web Service" and a "Service Reference". The problem is that a "Service Reference" is meant to refer to a WCF service, not to a web service (confusing, I know).
Try deleting the Service Reference and adding a "Web Reference" in its place.
BTW, the code for your web service is in the code behind file, in App_Code/Service.vb
>The problem is that a "Service Reference" is meant to refer to a WCF service, not to a web service (confusing, I know).
Grrrr! If this is the case, why couldn't they have labelled it "WCF Service Reference". Actually I think I want to use WCF, but the book that I'm trying to follow, "ASP.NET 3.5" by George Shepherd, illustrates WCF in Ch 21 by reference to ASP.NET Services (Ch 20), so I was going to get a basic XML service running first.
How do I add a "Web Reference"? When I right-click the VB program GDBChart (VS2008) there is no option for this, the options are: -
Add (=> New Item, Existing Item, New Folder, Windows Form, User Control, Component, Module, Class) : this is oriented to adding components into the project, not references.
Add Reference => the reference dialog, with tabs .NET, COM, Project, Browse, recent: this is oriented to adding namespaces
Add Service Reference => a dialog that allows you to search for web references. This appears to be the one we want, but this is what I did before. I tried it again, and got the same result - I added a namespace allowing me to reference GDBWebService.HelloWorldRequest etc, but not a method "HelloWorld".
Is your application an ASP.NET website? For me, when I right click on my website project, I see "Add Web Reference..." and "Add Service Reference..."
Maybe it's different for your project type? Which edition of VS 2008 are you using?
VS2008, SP1. My web projects are build with ASP .NET 3.5.
Like you, I see "add web reference" when I click on a web project, including the web service. But I was trying to add the web service to GDBChart, a Windows program. When I click on the Windows project there are only the other 3 Add options, as per my previous post.
Right click on Service References
Choose Add Service Reference - don't enter anything on that dialog but rather..
At the bottom of that dialog, choose "Advanced"
At the bottom of the next dialog choose "Add Web Reference"
Fill in your URL and select GO
Name your web service and then select "Add Reference"
You should then be able to do as you had expected.
Thanks CAnuszczyk. I was able to get it going eventually with your help.
According to the dialog, I used this option to ensure compabiltity with ASP .Net 2.0 (I am using VS2008, with ASP .Net 3.5). Why is this relevant? After all, I should not have had a compability issue since both my test Windows program, and the test Web Service, were generated at the same time from VS2008.
That, I don't know. I just ran across the same problem and thought I would share it what I did find.
I'm still researching the whole thing and the changes made to VS 2008 for it. I'll post when I find something.
I am using Visual Web Developer 2008, Express Edition but I cannot autogenerate the .DISCO and WDSL files.
I can create and test a simple WebService on the localhost.
Do I need to install and run IIS to be able to autogenerate .WSDL och .DISCO files? Or what is the problem?
Should it work work with the default unchanged HelloWorld webmethod? Somebody seemed to have problems with the namespaces, that prevented the autogeneration of the .disco file. Obviously, all "guidelines", "tutorials" and "samples" assume some preconditions that I don't have.
Please start a new thread on this - your issue probably has nothing to do with the issue that started this thread.
John Saunders | MVP – Connected System Developer
I am also stuck in the same problem. I am trying to consume a web service in a windows form.
The service is running on an IIS server and when we access it in a browser, it shows the test page and respond in a usual way (The service is up and running).
I am using VS2008 and create a windows project and try to add the service reference to the project. I have tried both the options separately
1- By simply adding a service reference
2- By going to the advanced option and choosing the Web service reference in the Dotnet 2.0 style.
The problem I am facing is, that most of the tutorials says that when you add a web service reference a proxy class will be automatically added to the project. So that we can create an object of the proxy class to use the web service.
The problem is that when we add the reference it does not create the proxy class with the name of the service "MyWebService" (MyWebService being the service to be used). There are some classes created in the new namespace, these are named like ...Request, ...RequestBody, ...Response, ...ResponseBody, and ...ServiceSoapClient.
Now I am unable to understand which class to be used as a proxy to instantiate the object to use the web service. I think it is related to this thread.
Kindly help find out, what I am missing.