[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 785
  • Last Modified:

The name 'InitializeComponent' does not exist in the class or namespace 'HelloService.HelloService'

I'm trying to use the following ASP.NET web services tutorial, http://abstractvb.com/code.asp?A=1006. I'm using VS 2003 C#. I got to the point of running the service (F5) and I get the error listed in the title. I'm thinking that it may be a version issue but I'm too new to dot-net to recognize this for sure. My code duplicates the code listed on the site, so I won't paste it here.

Essentially, the InitializeComponent() method isn't recognized within the HelloService() method.

Any ideas? If you need more info, please let me know. I'm trying to pick up bits and pieces of dot-net knowledge here and there, so this isn't a mission-critical task.

Thanks all.
Michael Stelly
0
Oracle4All
Asked:
Oracle4All
  • 2
  • 2
1 Solution
 
AGBrownCommented:
Micheal,

Its just that in the example code on that page he's half left out some of the autogenerated code that is created when you create your own web service. The bit that is missing is:
            #region Component Designer generated code
                        
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
            }
            
            #endregion

InitializeComponent is used by the IDE to write the code when you use the property pane in the IDE to wire up event handlers and set properties of objects on the designer surface. You could just as easily remove the call to "InitializeComponent();" but it's probably better just to paste the code above back into your file.

Andy
0
 
Oracle4AllAuthor Commented:
It's still failing on the compile. What did I do wrong?  Here's my code after copying your solution.

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace HelloService
{
      [WebService(
             Namespace="http://abstractvb.com/",
             Name="Hello Web Service in C#",
             Description="My Description")]
      public class HelloService : System.Web.Services.WebService
      {
            public HelloService()
            {
                  #region Component Designer generated code
                   
                  /// <summary>
                  /// Required method for Designer support - do not modify
                  /// the contents of this method with the code editor.
                  /// </summary>
                  private void InitializeComponent(){}
         
                  #endregion
            }

                  [WebMethod]
                  public string SayHello(String strMyName)
            {
                  return "Hello " + strMyName;
            }

                  [WebMethod]
                  public string SayGoodBye()
            {
                  return "Goodby!";
            }
      }      
}

And here are the errors:

c:\inetpub\wwwroot\WebService1\Service1.asmx.cs(18): } expected
c:\inetpub\wwwroot\WebService1\Service1.asmx.cs(18): } expected
c:\inetpub\wwwroot\WebService1\Service1.asmx.cs(31): Expected class, delegate, enum, interface, or struct
c:\inetpub\wwwroot\WebService1\Service1.asmx.cs(37): Expected class, delegate, enum, interface, or struct
c:\inetpub\wwwroot\WebService1\Service1.asmx.cs(42): Type or namespace definition, or end-of-file expected


Thanks for your quick response.
Michael Stelly
0
 
AGBrownCommented:
That's ok, your code should read as below. When I say "should" I just mean that that is what the web service will be generated as when you right click your project in VS2003's solution explorer and do Add -> Add Web Service ... . By pasting my code inside your Constructor (the "public HelloService()" method) you had managed to do all sorts of things that the compiler won't be able to understand, such as nesting a method inside a method (not allowed) and putting regions inside a method (also not allowed). I think, therefore, that the code below will compile.

To understand _why_ a little better I would suggest you try creating a new Web Service inside your project (note that the project itself is an application that contains many web services, not a web service itself). Click Add -> Add Web Service ... from the project's right-click context menu in solution explorer. Then compare the code for the new service with the code below, and also with your original code at the start of the question.

The basic problem to start with was that your constructor was calling InitializeComponent(), which didn't exist as the walkthrough had left it out. InitializeComponent, as I say, is an auto-generated method used by the designer surface. To see what it does, try dragging a SQLConnection from your VS toolbox onto your designer surface in your new web service, and then go see what has happened to InitializeComponent. You'll see the line "this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();" appear. This is why you don't ever edit InitializeComponent directly - it is managed entirely by the IDE. Yes, there are always exceptions to every rule, before some smart-ass pipes up ;).

Here's your new code:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace HelloService
{
      [WebService(
             Namespace="http://abstractvb.com/",
             Name="Hello Web Service in C#",
             Description="My Description")]
      public class HelloService : System.Web.Services.WebService
      {
            public HelloService()
            {
                  //CODEGEN: This call is required by the ASP.NET Web Services Designer
                  InitializeComponent();
            }

            #region Component Designer generated code
            
            //Required by the Web Services Designer
            private IContainer components = null;
                        
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if(disposing && components != null)
                  {
                        components.Dispose();
                  }
                  base.Dispose(disposing);            
            }
            
            #endregion

            [WebMethod]
            public string SayHello(String strMyName)
            {
                  return "Hello " + strMyName;
            }

            [WebMethod]
            public string SayGoodBye()
            {
                  return "Goodby!";
            }
      }
}
0
 
Oracle4AllAuthor Commented:
I see what I did wrong, now. That whole 'method in a method' thing. Thanks for your help!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now