question

JulioBello-4525 avatar image
0 Votes"
JulioBello-4525 asked Viorel-1 answered

Hosting a Web Service in a Web Application

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?


dotnet-csharpdotnet-visual-basicdotnet-aspnet-general
· 18
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JulioBello-4525,
As far as I think,there are two "current" web services technologies available in .NET. The old one is ASP.NET Web Services, known as "ASMX" services because of the .asmx file type used for the web service endpoints. This is old technology and should be avoided if possible.The correct way to create web services for .NET is to use Windows Communications Foundation. It is about 1000% better than the old ASMX technology. See http://msdn.microsoft.com/wcf/ for the WCF Developer Center on MSDN.
Best regards,
Yijing Sun

0 Votes 0 ·

Hi, Yijing...

I agree with you 1000%... WFC is far superior than ASMX. If I were to write new services, I would certainly implement them using WCF.

However, this is not new development. What I am trying to do is to consolidate all existing Legacy web services under one target server.

Oh... And let us not forget yet another web service technology available in .NET: ASP.NET Web API.








0 Votes 0 ·

I think that it is not uncommon to have ASPX, ASMX, ASHX in a single Web Application. It seems to work well if you create a new project in Visual Studio 2019, selecting “ASP.NET Web Application (.NET Framework)”, then “Web Forms”. Then add a new “Web Service (ASMX)” using “Add New Item” command. Both of default ASPX and ASMX sample items can be started from Visual Studio and accessed from browser. Check if such new projects work in your case.

How to reproduce the problem?


0 Votes 0 ·

Hi, Viorel...

I am so happy to hear from you!

I believe it is possible to have ASPX, ASMX and ASHX in a single Web Application. Lamentably, this is a Legacy application, with "oodles" of web pages, user controls, image files, etc., running .NET 4.6, using Visual Studio 2012. Upgrading the Solution/Project files to Visual Studio 2019 is a project in itself. I'm afraid that is not an option at this time.

To reproduce the error, simply create the ServiceName.asmx (first attempt) and corresponding ServiceName.asmx.vb in an existing web application. You may comment-out the Inherits clause and leave the class "empty" (i.e. no properties, no methods, etc.). The problem is getting the builder to recognize the NameSpace.ServiceName class in the ASMX file. I just need the build to process successfully. I don't care about its execution at this time.

Your insights have always been helpful. I am truly grateful for your help.

0 Votes 0 ·

I have worked in ASP.NET Web solutions for many many years as a full-time employee and as a contractor across the US. I have never seen or heard any legacy ASP.NET Web solution doing this double duty as a browser solution and as a Web service. It is uncommon, and no solution like this would ever make it out of a professional code review. It would surly get tossed out with the baby in the bath water.

And what port would this legacy ASP.NET solution doing double duty be listening on where you got browser traffic and Web service traffic being serviced by basically the same solution? What happens if a DoS attack is run on the Web service exposed to the public that would deny service to the browser users most likely that browser users couldn't get a response back from the solution on the the browser side?

0 Votes 0 ·

Wow!... Given your extensive experience as a full-time employee and as a contractor across the US, I am surprised that you have never seen or heard any legacy ASP.NET web solution doing "double duty" as a browser solution and as a web service. Heck!... I only have 36 years experience doing software development and software quality assurance and I have seen it plenty of times. Oh... And I only worked all over the USA and internationally (e.g., Sacramento, CA, Olympia, WA, Columbus, OH, Frankfort, KY (These were state contracts), Montreal, CA, London, England, Kingdom of Saudi Arabia, etc.).

And what port would this legacy ASP.NET solution, doing "double duty" be listening on, where there's browser traffic and web service traffic being serviced by basically the same solution? Well... Port 80, of course!

To be continued...

1 Vote 1 ·
Show more comments
Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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?

· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


The contents of the files are:

 ' File “ServiceName.asmx”
 <%@ WebService Language="vb" CodeBehind="ServiceName.asmx.vb" Class="MyWebApplication.ServiceName" %>

and

 ' File “ServiceName.asmx.vb”
 Imports System.ComponentModel
 Imports System.Web.Services
 Imports System.Web.Services.Protocols
    
 <System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
 <System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
 <ToolboxItem(False)> _
 Public Class ServiceName
     Inherits System.Web.Services.WebService
    
     <WebMethod()> _
     Public Function HelloWorld() As String
         Return "Hello World"
     End Function
    
 End Class

where “MyWebApplication” is the name of Web application and also the root namespace, which is seen in Project Properties.


0 Votes 0 ·

Hi, Viorel...

I did as you suggested... Lamentably, it did not work.

However, here is the Good News!...

As I wrote earlier...

Your insights have always been helpful...

You asked me if the project contained a .vbproj file. It does not. This caused me to re-think my problem statement.

I wish to make our web server perform "double duty"... One as a web application...

At that instance, I realized that I had made an assumption... The project is NOT a web application... It is a web site! They are distinct projects. A web application employs a .vbproj file... A web site does not. The App_Code folder is one of several "reserved App folders"... App_Browser, App_Data, App_Web_References, are among this set of "reserved App folders". All .aspx files and their corresponding .aspx.vb files reside OUTSIDE the "reserved App folders", organized in their own distinct folders.

To be continued...

0 Votes 0 ·

So... I moved the .asmx.vb file to the App_Code folder. I rebuilt the Solution... And voilà!... Problem solved.

0 Votes 0 ·
Show more comments
DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered DuaneArnold-0443 commented

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://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-1

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Yes, it should work. I've done it lots of times. There is no problem in adding both .aspx and .asmx files to the same project in a legacy ASP.NET application.
I always let Visual Studio do it (Add -> New item), and it automatically adds the correct namespace to each file, both the .asmx and the .asmx.cs.

0 Votes 0 ·

I agree with you, Alberto... It should work. I just don't know why I am getting this error: Could not create type 'NameSpace.ServiceName'.







0 Votes 0 ·

Maybe what you are trying to do has been depreciated.

0 Votes 0 ·