Thursday, January 5, 2017

Simple SQL Server ASP.NET Web Service Part 2: Deploying to IIS

In Part 1 we walked through creating a Simple SQL Server ASP .NET Web Service. Now let's work on deploying it to IIS.

As a another reminder, I am not a .NET developer, I am a database guy. Talk to some of your developer friends if you want some advanced help on the right way to do this stuff. I am just showing a very simple web service to call a Stored Procedure.

Assuming you have your web service project open in Visual studio...
  • Click on the Build Menu and click Build Solution.
  • Assuming you have no errors then click the Build Menu again and this time click Publish Web App
  • You should see something like this (reminder I am using Visual Studio 2015)
  • Click Custom and name it Local
  • Enter your server name and the name of the web site you want to deploy to. You can give your service a name here. Small Side Conversation here: Probably a good idea to name the service whatever you called it at creation time. I of course learned this the hard way when I went to deploy it and gave it a new name in flight. So now I have a project called Website in Visual Studio and a Service named totally different on my IIS server. This is all well and go if I have one service but as you grow it will be difficult to keep track of what is what.
  • Make sure you validate connection and its green and click Next
  • Click Next on the Setting Screen
  • Click Publish on the Preview Screen.
  • If all goes well your app should be deployed...if not google it. Check your output screen for details
  • If the publish was successful, Visual Studio will try to open a browse with your web service. I received a HTTP 500 error during this preview.
  • After some googling I found the work around. Right click on your web site in solution explorer and click Property Pages
  • Click the build option on the left and select .NET 4.5 Framework as Target Framework. Click Yes to the warning message.
  • Build your Web Site again and then Publish the Web App
  • This time I received a 404.13 Forbidden document. 
  • I added Service.asmx to the end of the URL and it worked. 
  • So I then added Service.asmx under Default Document for the web site. Just click on your web site in IIS and then find Default Document. Click Add and then add Service.asmx
  • Reload your web service and it should work now.
  • Now fire up your web service Using the invoke button we did earlier.
  • Ughhh
  • If you get this the fix is simple. You want to setup a local user on your server that has access to EXECUTE a stored proc on your SQL Server. 
  • You then want to create a new Application Pool in IIS. This will be used to call web services that need access to SQL.
  • Click Application Pools and on the Right side of the screen you should see a link for Add Application Pool
  • Give the Pool a name and click OK
  • Now right click on the new pool and click Advanced Settings
  • JUST FOR THIS EXAMPLE I created an Application Pool and then set the identity to my local administrator. This is a bad bad bad idea if you plan to go showtime with this web service. Click the Ellipses and use the account you created that has access to the database.
  • You are doing this because in your C# web service on your database Connection we are using Integrated Security, so the App Pool Identity must have the ability to connect and execute the stored procedure.
  • Now Highlight your service web site and on the Right side you should see a basic settings link. Click that Link
  • Click the Select button by the app pool and choose the app pool you created
  • And hopefully this now works. Whew...crack open a celebratory Mt. Dew.

In order for this web service to work outside the friendly confides of your internal network you need to add a section to your web.config. In the <system.web> section of your config add the following Web Services section:

Again be careful here when you expose a web service to the world.

Holler if you have questions, remember Google and Stack Exchange are your friends.


Post a Comment