问题
I have a REST based web service which runs under IIS with name "WebHandler". I usually run the webservice under IIS and then write functional test code.
So everytime I run/debug a functional test, I have to keep the webservice running under IIS. Is there a way that in the functionaltest code, I can somehow host the webservice programatically so that the webservice is part of same process (NO IIS is involved) ? Benefits from this which I perceive is:
- modify webservice code on fly and test it.
- no more efforts for compiling the new webservice dll, putting it in IIS's bin folder and resetting IIS.
Something like Self hosting WCF service for e.g.
using (ServiceHost host = new ServiceHost(typeof(SaleClassLibrary.SaleService), baseAddress))
{
...
host.Open();
// do the end-to-end test
host.Close();
}
The above snippet I have taken from http://www.codeproject.com/Tips/622260/WCF-Self-Hosting-with-Example section 1 [self hosting]
Following is the snippet of my web.config if that is required
<system.webServer>
<handlers>
<add name="WebHandler" path="*" verb="*" type="Com.Tata.WebHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
</handlers>
</system.webServer>
回答1:
Let me know if this helps you :
If you create a new WCF Service Application project, you'll have access to Web tab in the Properties window of the project, in which you can directly host your project on IIS and with every debug or run effort, you have the updated version of your service going.
来源:https://stackoverflow.com/questions/23558405/self-hosting-asp-net-restful-webservice