Jan
5
2012

C#.Net How To: Publish a Web service in IIS using Visual Studio 2010

Let’s learn to publish Web Service in this C# tutorial. I am assuming you have already created a web service to publish. If not then i would suggest to refer my previous article on how to create a web service application. You can also refer another article that explains how to consume web service.

Publishing a web service in c# & .net is very easy task. Follow below steps to create web service and publish a web service in IIS.

Steps to publish a web service in iis using c#.net

  1. Open your Web Service Application project. Go to Solution Explorer and right click on your Web Service Project. Select “Publish” from the drop down menu.
    C# - Publishing web service through .Net IDE
    Publishing web service through .Net IDE
  2. A “Publish Web” window will appear.
    C# - Publish Web Service Window
    C# – Publish Web Service Window
  3. Rename the Publish Profile name from “Profile1” to “MyFirstWebServiceProfile”. Select “File System” as Publish Method. And specify the target location to publish the Web Service. I am keeping target location under “C:\inetpub\wwwroot\MyFirstPublishedWebService”.
    C# - Select Profile for Publishing WebService
    C# – Select Profile for Publishing WebService

    Click on Publish button to publich the Web Service Application.

  4. You will find following files and folder in your published folder.
    1. bin folder – this folder contains the .dll file.
    2. Web.config file – This is a configuration file.
    3. Service1.asmx file
  5. Now open IIS (Internet Information Service).
    IIS - Internet Information Service
    IIS – Internet Information Service

    Here, you can see your published folder under “Default Web Site”. In the next step we would convert it to application format.

  6. Right click on the published folder and select “Convert to Application”.
    IIS - Adding Web Service Application
    IIS – Adding Web Service Application

    You can provide different Alias and application pool. The application pool must have a Read Access to the physical path. Hitting on Ok button will publish your Web Service in IIS.

  7. Now you will need to enable the Directory Browsing of our newly created Web Service Application. While selecting “MyFirstPublishedWebService” application double click on “Directory Browsing”.
    IIS - Directory Browsing for WebService
    IIS – Directory Browsing for WebService
  8. Click on enable to enable the Directory Browsing.
    C# - IIS - Enable Directory Browsing
    IIS – Enable Directory Browsing

    Now restart the IIS to take effect the changes.

  9. Now browse “http://localhost/MyFirstPublishedWebService/Service1.asmx” in internet browser. And here is your first published web service interface.

In this article you have learn to publish a web service in IIS using c#. I hope this articles will meet your expectations. If you find this article helpful, then could you please share the article on your social network?

Following articles might be of your interest…

About the Author: Ashish Ramteke

Ashish Ramteke is a Software Professional, Part time blogger and founder of C# Tutorials, a popular blog on C#.Net. Ashish is having 6 years of work experience and an engineering degree in Computer Science and Engg.

18 Comments+ Add Comment

  • instead of giving url “http://localhost/MyFirstPublishedWebService/Service1.asmx” if i write “http://localhost/MyFirstPublishedWebService” then it won’t work.. is there any way to start webservice in address bar(URL) automatically

    • You could try adding your “Service1.asmx” to the Default Documents of your web site.

  • is this really working?

  • Thank helped me. Thanks!

  • Unbeliveable! Amazing! Awesome!
    Many thanks, it saved our souls!

  • thanks

  • thanks

  • good

  • Very helpful article.

    Thanks
    Rafael

  • Hi Ashish,

    I have followed your article but I am getting the error message while opening the web service in internet explorer browser, please find the error message mentioned below and let me know changes need to be done :

    Server Error in ‘/WebServiceTest1’ Application.
    ——————————————————————————–

    Failed to access IIS metabase.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Web.Hosting.HostingEnvironmentException: Failed to access IIS metabase.

    The process account used to run ASP.NET must have read access to the IIS metabase (e.g. IIS://servername/W3SVC). For information on modifying metabase permissions, please see http://support.microsoft.com/?kbid=267904.

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

    Stack Trace:

    [HostingEnvironmentException: Failed to access IIS metabase.]
    System.Web.Configuration.MetabaseServerConfig.MapPathCaching(String siteID, VirtualPath path) +637
    System.Web.Configuration.MetabaseServerConfig.System.Web.Configuration.IConfigMapPath2.MapPath(String siteID, VirtualPath vpath) +9
    System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull) +174
    System.Web.Hosting.HostingEnvironment.MapPathInternal(VirtualPath virtualPath, Boolean permitNull) +51
    System.Web.CachedPathData.GetConfigPathData(String configPath) +341
    System.Web.CachedPathData.GetConfigPathData(String configPath) +234
    System.Web.CachedPathData.GetApplicationPathData() +38
    System.Web.CachedPathData.GetVirtualPathData(VirtualPath virtualPath, Boolean permitPathsOutsideApp) +8823599
    System.Web.Configuration.RuntimeConfig.GetLKGRuntimeConfig(VirtualPath path) +117

    • Hi Shankrayya,

      Looking at the stacktrace it seems; you do not have “metabase permissions on the IIS Admin Objects”. Please refer below link from Microsoft.
      http://support.microsoft.com/?kbid=267904

      I hope this should solve your problem. If still problem persist, post it here.

  • hello Ashish, as I can consume the web service remotely and tried it and log in from another machine tells me that “El formulario de prueba sólo está disponible para solicitudes del equipo local..”
    that would make me settings missing?

    • Hi, thanks for giving me an opportunity to help you out-

      Well, add below codes of lines in your configuration file (web.config)and see if it solves your issue.

      <configuration>
      <system.web>
      <webServices>
      <protocols>
      <add name=”HttpGet”/>
      <add name=”HttpPost”/>
      </protocols>
      </webServices>
      </system.web>
      </configuration>

      If this doesn’t solve your issue then reply back again.

  • Hi Dear,Thanks you for your usefull post, I did every thing you said but when I try to browse asmx file i got error:HTTP Error 404. The requested resource is not found.

    How can i fix this?! please help me. I tried installing ServiceModelReg.exe via cmd or change app pool settings but nothing changed!

  • This comment has been removed by a blog administrator.

  • It may also help to some if you run this command to register your .net version into IIS:

    c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -iru

    otherwise you may get an exception such as this one: Could not load type ‘System.ServiceModel.Activation.HttpModule’

    replace your version folder. My used \v4.0.30319.

  • Thanks for the site. It is really very helpful to the beginners like me.

Leave a comment