12.9 Host a Remote Object in IIS


Problem

You want to create a remotable object in IIS (perhaps so that you can use SSL or IIS authentication) instead of a dedicated component host.

Solution

Place the configuration file and assembly in a virtual directory, and modify the object URI so that it ends in .rem or .soap.

Discussion

Instead of creating a dedicated component host, you can host a remotable class in Internet Information Services (IIS). This allows you to ensure that the remotable classes will always be available, and it allows you to use IIS features such as SSL encryption and Integrated Windows authentication.

To host a remotable class in IIS, you must first create a virtual directory. The virtual directory will contain two things: a configuration file that registers the remotable classes and a bin directory where you must place the corresponding class library assembly (or install the assembly in the GAC).

The configuration file for hosting in IIS is quite similar to the configuration file you use with a custom component host. However, you must follow several additional rules:

  • You must use the HTTP channel (although you can use the binary formatter for smaller message sizes).

  • You can't specify a specific port number for listening. IIS listens on all the ports you have configured in the IIS manager. Typically, this will be ports 80 and 443 (for secure SSL communication).

  • The object URI must end with .rem or .soap.

  • The configuration file must be named Web.config, or it will be ignored.

Here's an example Web.config file that registers the remote class shown in recipe 12.7.

 <configuration>   <system.runtime.remoting>     <application>       <service>         <wellknown mode="SingleCall"             type="RemoteObject.ProductsDB, RemoteObject"             objectUri="RemoteObject.rem" />       </service>       <channels>         <channel ref="http">         <!-- Uncomment the following tags to use the binary formatter              (instead of the default SOAP formatter). -->         <!--           <serverProviders>             <formatter ref="binary"/>           </serverProviders>          -->         </channel>       </channels>     </application>   </system.runtime.remoting> </configuration> 

A client can use an object hosted in IIS in the same way as an object hosted in a custom component host. However, the virtual directory name will become part of the object URI. For example, if the Web.config file shown in the preceding code is hosted in the virtual directory http://localhost/RemoteObjects , the full URL will be http://localhost/RemoteObjects/RemoteObject.rem .

Note  

When hosting an object with IIS, the account used to execute the object is the ASP.NET account defined in the machine.config file. If this account doesn't have the rights to access the database (which is the default situation), you will receive an error when you try this example. To solve the problem, refer to recipe 7.17.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net