Subject: Access properties & methods from a WebService
Posted By: sloesch Post Date: 1/16/2006 10:51:57 AM
I’m fairly new to .NET.  I’ve been developing in .NET for about 6 months.  I have over 10 years experience with VB in general.

Here’s what I have done so far:

1.    Project is entirely .NET
2.    Working with VS.net 2003, Framework 1.1, and WSE 2.0
3.    Created 1 Webservice
4.    Added 9 projects to it.  All developed in VB.net (6) and C# (3)
5.    Added all the necessary references to the .asmx to access these projects

Here is the issue I have:

1.    How do I expose the properties and methods residing in my external projects I added to my Webservice Solution, so my Web App can access them?
2.    Each of these external projects have many classes in them with the properties and methods I need.
3.    I added a reference to my Web App to my Service.

I have tried adding Imports statements to the .asmx but I still have no access to the properties and methods.

I tried creating a Public Class and adding an Inherits statement to my external project, so my Web App can gain access to these classes though my Web Service.  However I am unable to just add a Parent Inherits statement without drilling down to the lowest child level.  If I don’t, it asks for a Type Expected.  Is there a better way to do this?

Just an FYI, all 9 of these projects are in my Web App currently and I want to pull them out and access them though my Web Service instead.  Any help you can provide would be greatly appreciated.

Thanks,
Reply By: planoie Reply Date: 1/17/2006 10:57:13 AM
First, I'd like to clarify something (the terms you used don't seem entirely correct.  From what I'm reading, this is what you have:

- Visual studio solution with 10 projects
- 1 of the projects is a web service project (truly a web application project)
- Web service project references the other 9 projects

1 question regarding "I added a reference to my Web App to my Service.": What web app are you referring to? Is the "service" you refer to the web service project?


Web services function my exposing methods written in the web service class (the *.asmx.vb file(s)) as "WebMethod"s.  If you have standalone methods in your supporting projects that you can call independently then you could create matching method signatures on your web service class and then call the support class methods from them, returning the result on the web method.

If you have more extensize processes that need to go on, then you might want to look into a remoting solution.

-Peter
Reply By: sloesch Reply Date: 1/17/2006 1:30:41 PM
Sorry for the confusion.  Yes, the Service I am referring to is my Web Service Project (i.e. Service1.dll).  To clarify things I have a:

1.    Web Service Solution with 9 other projects associated with it.
2.    Web Application Solution

My Web App Solution contains a reference to my Web Service.  Since my Web Service and these various projects have different Root Namespaces I have to fully qualify them using the Imports Statement in my .asmx.  

When I do this, shouldn’t I have access to those classes in these projects with their properties and methods from my Web App, so long as I have a reference in my Web App pointing to this Web Service?  I would think so, but I don’t.

Here’s what my code looks like in my Service:

Imports Project1
Imports Project1.Data
Imports Project1.Model
Imports Project1.DataAccess
Imports Project1.Security
Imports Project1.SystemConfig

Imports System
Imports System.Xml
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols

' WSE Namespaces

Imports Microsoft.Web.Services2
Imports Microsoft.Web.Services2.Addressing
Imports Microsoft.Web.Services2.Messaging
Imports Microsoft.Web.Services2.Security
Imports Microsoft.Web.Services2.Security.Tokens
Imports Microsoft.Web.Services2.Security.X509
Imports Microsoft.Web.Services2.Security.Policy

<System.Web.Services.WebService(Namespace:="http://tempuri.org/ApolloService/ApolloService")> _

Public Class ApolloService

    Inherits System.Web.Services.WebService

End Class

It’s very basic right now until I start adding Security.  Currently the first 6 Imports are References in my Web App but I want to pull them out, and have them referenced in my Web Service instead, then just have a single reference in my Web App to this Service.

quote:
Originally posted by planoie

First, I'd like to clarify something (the terms you used don't seem entirely correct.  From what I'm reading, this is what you have:

- Visual studio solution with 10 projects
- 1 of the projects is a web service project (truly a web application project)
- Web service project references the other 9 projects

1 question regarding "I added a reference to my Web App to my Service.": What web app are you referring to? Is the "service" you refer to the web service project?


Web services function my exposing methods written in the web service class (the *.asmx.vb file(s)) as "WebMethod"s.  If you have standalone methods in your supporting projects that you can call independently then you could create matching method signatures on your web service class and then call the support class methods from them, returning the result on the web method.

If you have more extensize processes that need to go on, then you might want to look into a remoting solution.

-Peter



Reply By: planoie Reply Date: 1/17/2006 3:15:39 PM
The Imports statement does nothing more than allow you to "abbreviate" class names. For example, if you use "Imports System.Data.SqlClient" you can access the "SqlCommand" type without having to fully qualify it ("System.Data.SqlClient.SqlCommand").

Project references are unrelated to the Imports keyword.  In order to use classes in another assembly, you must reference that assembly.  You can do all of that without a single "Imports" statement.

When you create a web reference to a web service, Visual Studio will generate a service proxy class that creates a class named with the name you gave for the reference.  It will then create methods on that class that match all the exposed web methods of the service as defined in the services WSDL.  Other assemblies referenced by that service can be USED by the code for that service, but are not implicitly exposed by the service.  You have to explicitly create web methods that exposed the functionality you wish to make public.

-Peter
Reply By: sloesch Reply Date: 1/17/2006 3:48:24 PM
In other words, if I what to access say Project1.Data.GetARecord Method from my Web App, I would need to create a Public Class in my MyService.asmx like this:

<System.Web.Services.WebService(Namespace:="http://tempuri.org/MyService")> _
Public Class GetARecord
    Inherits Project1.Data.GetARecord
End Class

and remove my Imports since I have the Reference to Project1.Data in my MyService.asmx already.


quote:
Originally posted by planoie

The Imports statement does nothing more than allow you to "abbreviate" class names. For example, if you use "Imports System.Data.SqlClient" you can access the "SqlCommand" type without having to fully qualify it ("System.Data.SqlClient.SqlCommand").

Project references are unrelated to the Imports keyword.  In order to use classes in another assembly, you must reference that assembly.  You can do all of that without a single "Imports" statement.

When you create a web reference to a web service, Visual Studio will generate a service proxy class that creates a class named with the name you gave for the reference.  It will then create methods on that class that match all the exposed web methods of the service as defined in the services WSDL.  Other assemblies referenced by that service can be USED by the code for that service, but are not implicitly exposed by the service.  You have to explicitly create web methods that exposed the functionality you wish to make public.

-Peter



Reply By: planoie Reply Date: 1/23/2006 11:32:42 AM
You need to create a web service class (ASMX) that exposes all the methods you wish to utilize.  For example, if you want to expose the methods of Project1.Data thru web services, then you could do something like this:

<System.Web.Services.WebService(Namespace:="http://tempuri.org/MyService")> _
Public Class Project1Data
    Inherits System.Web.Services.WebService
    '*** You must inherit from the class that makes this a web service. ***

    '*** Here, expose each underlying class method as a web method ***
    '*** The method signature should match the underlying method ***
    <WebMethod()> Public Function GetARecord(criteria As String) As DataSet
          Return Project1.Data.GetARecord(criteria)
    End Function

    '****************************
    'Create as many functions as you need to provide
    'access to the underlying methods of the other classes.
    '****************************

End Class

-Peter
Reply By: sloesch Reply Date: 1/23/2006 12:29:52 PM
This is what I thought you were saying, and what I have seen elsewhere on the Internet.  

If I have to recreate my functions as <WebMethod()> in my ASMX, then what is the purpose of a WebService?  I must be under the wrong impression of what the uses of a WerService is.  I was under the impression that WebServices gave you the ability for other Apps to reuse code rather than recreate it each time.

quote:
Originally posted by planoie

You need to create a web service class (ASMX) that exposes all the methods you wish to utilize.  For example, if you want to expose the methods of Project1.Data thru web services, then you could do something like this:

<System.Web.Services.WebService(Namespace:="http://tempuri.org/MyService")> _
Public Class Project1Data
    Inherits System.Web.Services.WebService
    '*** You must inherit from the class that makes this a web service. ***

    '*** Here, expose each underlying class method as a web method ***
    '*** The method signature should match the underlying method ***
    <WebMethod()> Public Function GetARecord(criteria As String) As DataSet
          Return Project1.Data.GetARecord(criteria)
    End Function

    '****************************
    'Create as many functions as you need to provide
    'access to the underlying methods of the other classes.
    '****************************

End Class

-Peter



Reply By: planoie Reply Date: 1/23/2006 3:06:00 PM
The purpose of the web service is to provide a HTTP interface to executing your methods.  The WebService class from which you derive your ASMX classes handle all the underlying details of constructing this HTTP interface.  It creates the WSDL so that consumers of the service can discover what you expect from them and what they expect from you.  It handles all the de/serialization of the incoming and outgoing data so that you don't have to write all of that.

How is this recreating code?  Sure, you need to duplicate the method signature but that isn't asking much.  Think of it like this: when you create an internal variable in a class and want to expose it to the public for the class' consumer to see, you create a public property on the class.  It expect the type to match the internal variable.  A web method is like a more complicated version of that.  You are providing public access via the web service to a private method inside your classes.

-Peter
Reply By: sloesch Reply Date: 1/24/2006 1:02:12 PM
Thanks Peter.



quote:
Originally posted by planoie

The purpose of the web service is to provide a HTTP interface to executing your methods.  The WebService class from which you derive your ASMX classes handle all the underlying details of constructing this HTTP interface.  It creates the WSDL so that consumers of the service can discover what you expect from them and what they expect from you.  It handles all the de/serialization of the incoming and outgoing data so that you don't have to write all of that.

How is this recreating code?  Sure, you need to duplicate the method signature but that isn't asking much.  Think of it like this: when you create an internal variable in a class and want to expose it to the public for the class' consumer to see, you create a public property on the class.  It expect the type to match the internal variable.  A web method is like a more complicated version of that.  You are providing public access via the web service to a private method inside your classes.

-Peter




Go to topic 39122

Return to index page 386
Return to index page 385
Return to index page 384
Return to index page 383
Return to index page 382
Return to index page 381
Return to index page 380
Return to index page 379
Return to index page 378
Return to index page 377