[From Krzys Ostrowski] Here are:
- Simple steps to create and run a web service.
- Suggestions on how to run multiple instances of it on the same machine.
- Example of service state management.
Probably the simplest way to achieve the goal of being able to run multiple instances of your code on the same machine is through the use of "virtual directories" in IIS. A single web server may host multiple applications, accessible as http://hostname/application1, http://hostname/application2 etc. The names application1, application2 above correspond to "aliases" of virtual folders within IIS. A single folder holds a whole separate directory structure, sufficient for running a service or website. By creating multiple virtual folders in IIS and simply replicating contents of one to all the others, by a trivial command-line operation, eg. xcopy, you can have any instances of your service running on the same machine totally independently of one another.
Here's how to create a virtual directory:
- Go to "Internet Information Services (IIS) Manager", either through Control Panel -> Administrative Tools -> Computer Management -> Services and Applications, or through other means, depending on the specific version of Windows you are using.
- Go to Web Sites -> Default Web Site, right click on "Default Web Site".
- Choose New -> Virtual Directory, give some Alias (the name under which it will be accessible from the browser) and some location (that's where its contents, including files you're going to write, will be kept). For example, "Foo" and "C:\Foo". Leave all the other options set to defaults.
You may, of course, want to create multiple such directories.
Now, if you create your web service directly in a virtual directory, eg. through the following steps, all it takes to achieve the desired effect is indeed to copy the whole contents of the virtual folder to the other folders. You do it by just copying files, as mentioned above, not through the ISS admin.
- In Visual Studio, create a new C# project of type "ASP.NET Web Service". As location, give http://localhost/Foo, or in general just use the alias you've chosen in the last step instead of "Foo".
- Click on the project source window to switch to code view, uncomment the "HelloWorld" method together with the preceding attribute.
- Build the solution.
- Your service is available at the address http://localhost/Foo/Service1.asmx.
Now, you may replace the body of the HelloWorld method by something like:
int counter;
Application.Lock();
if (Application["counter"] == null)
Application["counter"] = counter = 0;
else
Application["counter"] = counter = (int) Application["counter"] + 1;
Application.UnLock();
return "Counter = " + counter.ToString();This code maintains some internal state within the service, and increments the counter at each invocation. If you create another virtual folder and copy all the contents of C:\Foo in there, run it like you've run Foo above, you will see that the two services maintain separate state: the two counters live their own two independent lives.
There are two types of state: application state (like in the example above) and session state (for client sessions). Indeed, an ASP.NET web service created and deployed like I described above is not going to keep objects live in between invocations, so anything that needs to be persistent should be kept in the "application state" like in the example above.