Hosting a Web Service in a Web Application

Julio Bello 221 Reputation points
2021-04-27T12:42:53.357+00:00

Hi, Everybody!...

I wish to make our web server perform "double duty"... One as a web application... The other as a web service. Http://Server should perform as a web application (as it always had). Http://Server/ServicePath/ServiceName.asmx should perform as a web service. When I include the NameSpace, the build returns the following error (See ServiceName.asmx, first attempt):

Could not create type 'NameSpace.ServiceName'.

When I did not include NameSpace, the build returned the following error (See ServiceName.asmx, second attempt)...

Could not create type 'ServiceName'

What am I doing wrong?

ServiceName.asmx, first attempt

<%@ WebService Language="VB" CodeBehind="*ServiceName*.asmx.vb" Class="*NameSpace*.*ServiceName*" %>  

ServiceName.asmx, second attempt

<%@ WebService Language="VB" CodeBehind="*ServiceName*.asmx.vb" Class="*ServiceName*" %>  

ServiceName.asmx.vb

Imports System.Web.Services  
Imports System.ComponentModel  
  
Namespace *WebApplication*.*WebServices* ' First attempt.  Also commented out on the second attempt  
    ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.  
    ' <System.Web.Script.Services.ScriptService()> _  
    <System.Web.Services.WebService(Namespace:="http://*OurDomain*/")> _  
    <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _  
    <ToolboxItem(False)> _  
    Public Class *ServiceName*  
        Inherits *OtherNameSpace*.*OtherServiceName*  
    End Class  
End Namespace ' First attempt.  Also commented out on the second attempt  

I have exhausted every trick I know. I have run out of ideas. I hope someone can help me.

One thing I did find interesting... After a Rebuild Solution, NameSpace.ServiceName (or ServiceName when NameSpace is not used) would appear in the Object Browser after searching for ServiceName. It would display the following upon clicking ServiceName (See below). If one were to click NameSpace (or WebApplication when NameSpace is not used) below, it would "jump" to WebApplication, but it would NOT be listed under it. Where IS it listed?

Public Class ServiceName
Inherits OtherNameSpace.OtherServiceName
Member of NameSpace (or WebApplication when NameSpace is not used)

Note: Server, ServerPath, ServiceName, OtherServiceName, NameSpace, OtherNameSpace, WebApplication and OurDomain, and when they are surrounded by asterisks ("*"), are placeholders for their corresponding values. They are NOT the actual values.

UPDATE

I finally found where NameSpace (or ServiceName when NameSpace was not used) was listed in the Object Brower. There is actually TWO instances of the WebApplication listed in the Object Browser... One containing all the classes (except mine)... Another one containing my class ONLY. When I clicked on NameSpace (or WebApplication when NameSpace was not used), it would “jump” to the FIRST instance of the already expanded WebApplication, where my class was NOT listed. It was not until I collapsed the first instance of WebApplication that I "discovered" the second instance. Therein was my class. This, however, does not resolve my problem. Question: How do I properly reference my class in the SECOND instance of WebApplication in my ASMX file?

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,243 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,197 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,563 questions
{count} votes

Accepted answer
  1. Viorel 111.7K Reputation points
    2021-04-28T12:02:15.647+00:00

    After creating simple ServiceName.asmx and ServiceName.asmx.vb files in the root folder of existing ASP.NET application, it did not work (“Cannot create type” error). But after including ServiceName.asmx into project using “Add Existing Item” command in Visual Studio, then launching the application from Visual Studio, it started to work.

    The contents are shown in separate comment, since the length is limited here.

    The application includes a .vbproj file, however there are Web applications that have a different structure. Do you have a .vbproj file?


1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,211 Reputation points
    2021-04-27T16:53:46.727+00:00

    You're are on the wrong path IMHO thinking that a legacy ASP.NET Web solution can pull this double duty. I don't think a legacy Web Service and a legacy Webform in the same project share the same pipeline, and what you are doing is not viable.

    However, an ASP.NET MVC WebAPI project using MVC controllers for the views and WebAPI controllers for services can be in the same ASP.NET MVC WebAPI project as shown in the below tutorial, because the both can use the same ASP.NET MVC pipeline. So in theory, an ASP.NET WebAPI project can pull the double duty.

    https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-1