Solved

the simplest Web Service

Posted on 2006-07-13
17
352 Views
Last Modified: 2008-03-04
Hi Experts,

I just want to create the simplest Web Service to see how it works.  Below is what I did.  However, it is not working, when I build the project, it is last forever, I have to cancel the building process.  Do I miss something to complete a work cycle of web service.  

1.  create web service file

Imports System.Web.Services

<System.Web.Services.WebService(Namespace := "http://tempuri.org/Appendix_A/Calculator")> _
Public Class Calculator
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function Add(ByVal intA As Integer, _
    ByVal intB As Integer) As Integer
        Return (intA + intB)
    End Function

End Class

2.  Invoke the Web Service from an ASP.NET page

Public Class callws
    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim objCalc As New Calculator
        lblMessage.Text = objCalc.Add(1, 5)

    End Sub

End Class
0
Comment
Question by:5281
  • 9
  • 8
17 Comments
 
LVL 42

Expert Comment

by:frodoman
ID: 17102709
Do you have IIS running on your workstation?  I believe that Visual Studio is going to attempt to contact IIS to resolve the web service so if IIS isn't running you may have a hang.

Also I've never attempted to build a web service and an application to call that service in the same project.  I'm not certain that you can't do it, but maybe there's an issue with that?  I'd try building your web service project and then build a project to consume it.
0
 

Author Comment

by:5281
ID: 17102991
Thanks frodoman.

I create two new projects, one holds web service, the other is to invoke that web service.  I can build both now, however, the page which invoke the web service doesn't recognize the Web Service object: calculator, calculator is highlighted with a blue curve underline, do I miss something?  Do I need to register dll or something?  

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim objCalc As New calculator                           <------------- there is a  blue curve underline under calculator
        lblMessage.Text = objCalc.Add(1, 5)
End Sub
0
 
LVL 42

Expert Comment

by:frodoman
ID: 17103176
You need to add the web reference to your project - I'm guessing you haven't done that.  Rt-click in the project exporer then Add -> Web Reference.

Your project doesn't recognize that "Calculator" exists until you provide the reference to it.
0
 

Author Comment

by:5281
ID: 17107908
frodoman,

In that Web Service project, I added a web reference (I browse the web services on my local machine), it is automatically called 'localhost', but when I go to the project which invoke the web service, web service object 'calculator' still can not be recoganized, it is still highlighted.  I don't know what's wrong.
0
 
LVL 42

Expert Comment

by:frodoman
ID: 17108128
You need to invoke the object with the name that matches the web service.  For example, when I create a web reference I would name it "WSCalculator" then in my code I would have:

  Dim objCalc as New WSCalculator()

Obviously it doesn't matter what you name it, but your web service reference creates a class and the object you instantiate needs to match that class name.
0
 

Author Comment

by:5281
ID: 17108272
I renamed the web reference, and I even tried to delete the existed web reference, add a new web reference, it still can't recoganized.  

I renamed web reference as MyCal, in my code I use:
Dim objCalc as New MyCal()

It is not working, web reference should be added in the web service project, not the project invoke web service, right?
0
 
LVL 42

Expert Comment

by:frodoman
ID: 17108885
>>> web reference should be added in the web service project, not the project invoke web service, right?

No - the web reference is added to the project that invokes the web service.  Your code says to create a new Calculator object.  The web reference is what tells your application to go out to the web service to find what a "Calculator" object is.
0
 

Author Comment

by:5281
ID: 17109969
Hi frodoman,

I added reference to right project now.  It can find the object ( I named the reference as MyCal), however, when I  build the project, it says "type expected" for MyCal.  Do you know why?  Thank you.

Dim objCalc As New MyCal                      <---- type expected for MyCal
lblMessage.Text = objCalc.Add(1, 5)

If I change to below, it says 'Add' is not a member of 'asptest.MyCal.calculator'

Dim objCalc As New MyCal.calculator      
lblMessage.Text = objCalc.Add(1, 5)       <---------- 'Add' is not a member of 'asptest.MyCal.calculator'
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:5281
ID: 17111084
Do I need implement the Proxy Class to be able to make it work?
0
 
LVL 42

Expert Comment

by:frodoman
ID: 17111698
As far as I can see, this code should work:

Dim objCalc As New MyCal.calculator      
lblMessage.Text = objCalc.Add(1, 5)  


When you built your web service did you test it to make sure it's deployed and running okay?
0
 

Author Comment

by:5281
ID: 17111919
Yes, after I build web service, I checked that web service site, http://localhost/.../calculator.asmx, it seems ok, I can input two number and invoke, it will give me a total in xml file.  That's all I did.

0
 

Author Comment

by:5281
ID: 17112030
I don't know if I need do something else, such as run wsdl.exe etc.?

I don't understand why it says 'Add' is not a member of 'asptest.MyCal.calculator', I know there is a Add function in Web Service.
0
 
LVL 42

Expert Comment

by:frodoman
ID: 17121602
I think there's something wrong with how you've added the reference to your project - maybe we should walk through it step by step?  Are you using Visual Studio?  If so, which version?
0
 

Author Comment

by:5281
ID: 17131103
frodoman,

Sorry for the delay, I was dedicated to another project yesterday.  You are right, I re-add the web reference, now it can compile without problem, the only problem is when I access the web page, it shows the following error:
 
The request failed with HTTP status 401: Access Denied.

It seems permission error to access web service.  Any idea?


 
0
 
LVL 42

Accepted Solution

by:
frodoman earned 500 total points
ID: 17132468
It's probably just a matter of modifying the permissions in IIS and/or on the physical folder to allow access.  You can try setting it to allow 'Everyone' access to the virtual directory and folder and that should let you make the first connection.  Of course for a permanent solution you'll want to only allow the appropriate access but I'd start with 'Everyone' just to get it working the first time.

0
 

Author Comment

by:5281
ID: 17146093
frodoman,

Sorry for the delay.  I finally make my first web service work, thank you for giving me lots of help, I really appreciate.
0
 
LVL 42

Expert Comment

by:frodoman
ID: 17146669
It wasn't that long ago I had to go through this and ran into some of the identical problems so I'm glad I was able to help.

cheers!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now