Thursday, September 29, 2011

Migration to SharePoint 2010: Part 2 - ASP.NET Web Service

In continuation of Part 1, the decision was made to create a bridge between our third-party web services and SharePoint due to the native SOAP service connector not being compatible with the WSDL format.

To do this I created an ASP.NET Web Service solution in Visual Studio 2010.
  1. Open Microsoft Visual Studio 2010 (in this case I used VS Professional).
  2. Click on File in the toolbar.
  3. Select New Web Site.
  4. Pick your poison (Visual Basic or C#).  For purposes of what I was working on, I choose C#.
    1. This is where things got interesting.  .NET Framework 4 did not appear to have a ASP.NET Web Service template.  Likely because they expect you to use WCF Service.  I don't know.
  5. Switch to .NET Framework  3.5, if necessary, at the top of the window.
  6. Click on ASP.NET Web Service from the list of templates.
  7. Click on Browse next to your Web location field at the bottom.
  8. Select Local IIS located on the left side of the screen.
    1. A warning here.  When I did this I received the following message:



      I resolved this error message by enabling the Windows feature.  This is how you can enable in Windows 7:
      1. Click on your Start icon.
      2. Select the Control Panel.
      3. Click on the Programs category heading.
      4. Click on Turn Windows Features on or off under the Programs and Features category.
      5. Branch down in the following order: Internet Information Systems => Web Management Tools => IIS 6 Management Compatibility.
      6. Put a check in the box next to IIS Metabase and IIS 6 configuration compatibility.
      7. Click OK.  You may or may not have to restart your machine.
  9. Click on Default Web Site under your list of local web servers.
  10. In my case I needed SSL enabled.  So I selected the option for  Use Secure Sockets Layer. Before you do this though, make sure you have SSL enabled in IIS for localhost.
  11. Click on Create New Web Application.  This is a small icon in the top right of the window.
  12. Enter the name of your web service.  For example, ConversionWebService.  Then click Open.
  13. Click OK.
    1. Another warning.  At this point I got the following message: "ASP.NET 2.0 has not been registered on the Web server. You need to manually configure your Web server for ASP.NET 2.0 in order for your site to run correctly, Press F1 for more details."  To correct this problem I had to revisit the control panel to access Windows features.
      1. Click on your Start icon.
      2. Select the Control Panel.
      3. Click on the Programs category heading.
      4. Click on Turn Windows Features on or off under the Programs and Features category.
      5. Branch down in the following order: Internet Information Systems => World Wide Web Services => Application Development Features.
      6. Put a check in the box next to ASP.NET.  This will also automatically place a check next to other options.  Which is fine.
      7. Click OK.  You may or may not have to restart your machine.
  14. At this point Visual Studio opened up a generic class file called "Service".  Which contains the C# code for managing the Web Service.
    Up to this point I have essentially covered creating the Web Service under IIS.  You can modify this default file or create a new web service.  In my case, I started fresh and removed the generic Service.cs and Service.asmx files.  Keep in mind that my situation may be different then your own.  I am attempting to build a web service proxy.

    So here are the steps I followed to create the new web service:
    1.  In the Solution Explorer pane to the right, right-click your solution name (should look like a URL) and select Add New Item.
    2. Select the Web Service template under your Installed Templates language of choice (again, in my case this is C#).
    3. In the name field, go ahead and name your Web Service.  If you are building the temperature conversion call (from W3C Schools) you could call the Web Service 'Convert.asmx' minus the single quotes.
    4. Place a check in the Place code in separate file box.
    5. Click on Add.
    6. At this point I deleted the original Service.asmx and Service.cs files since I had no plans on using them.
    7. This is where your C# or Visual Basic skills come into play.  The default code includes a Web Service method called HelloWorld().  Include the following code after the HelloWorld() method.  Again, I am doing C#.

      [System.Web.Services.WebMethod()]
      public double FahrenheitToCelsius(double Fahrenheit)
      {
          return ((Fahrenheit - 32) * 5) / 9;
      }
      
      [System.Web.Services.WebMethod()]
      public double CelsiusToFahrenheit(double Celsius)
      {
          return ((Celsius * 9) / 5) + 32;
      }
      

    8. Save the file.
    Now we can and should test the web service to make sure it actually works.

    1. In the Solution Explorer pane located to the right, click on your service ASMX file and hit CTRL + F5.  You should see the three operations (CelsiusToFahrenheit, FahrenheitToCelsius, and HelloWorld) at the top of the page that just loaded.
    2. Click on CelsiusToFahrenheit.
    3. In the Celsius field, enter a value (in this case I entered 37).  Then click on Invoke.
    4. If all has gone according to plan, you should see your Fahrenheit value (in my case it was 98.6).
    Sweet.  We now have a Web Service.  Since I am building a type of proxy, where one web service calls another, my code looks much different than what is listed above.  But, you can code the web services to match your situation.

    In my next post, I will go on to describe how I took my Web Service and integrated it into a page on SharePoint 2010.

    1 comment: