Remoting Server and Client in same ASP.NET application
I've been using ASP.NET applications as remoting clients for a while now. Recently I wanted one of the applications to also act as a server and I wanted to expose a remoted object from the ASP.NET application. This is a bit tricky.
When the application was just a client I had my registration information in the web.config file. I used the Application_Start method in Global.asax to execute RemotingConfiguration.Configure() so the wellknown types got registered when the application started up (on the first call to the app).
When I wanted to extend the ASP.NET application to offer some remotable objects and act as a server i added a <service> element to my web.config file. This causes IIS to read the web.config file and do registration automatically for the service. It does not, however, register WellKnownClientTypes. The combination of a <service> element and a call to RemotingConfiguration.Configure() will cause (I presume) the following chain of events, given a newly deployed application and a request url of the service (i.e. "http://localhost/myApp/MyService.soap?wsdl"):
1. ASP.NET starts the application and calls Application_Start
2. RemotingConfiguration.Configure is called and registers wellknown client types and channels.
3. IIS detects the service and tries to autoconfigure
4. The channel is already configured and you'll get something like:
RemotingException: The channel http is already registered.
5. The wellknown types are already configured and you'll get something like:
Attempt to redirect activation of type 'Type, Assembly' which is already redirected.
The registration of channels can be omitted by either not specifying any channels in the web.config (IIS uses http on port 80 by default) or by manually unregistering like this (so that IIS can reregister for the service):
foreach(IChannel channel in ChannelServices.RegisteredChannels)
ChannelServices.UnregisterChannel(channel);
This won't do much good though because the last problem (described by Ingo Rammer here) don't seem to be able to workaround with the use of config files.
The options to make it work are:
1. Drop the call to RemotingConfiguration.Configure and your service will work fine. You'll have to do registration of WellknownClientTypes manually in code(put the URI in appSettings instead of system.runtime.remoting).
2. Create a new virtual directory for your service and deploy in a separate assembly with a separate config file.
This problem occurs on IIS because remoted objects server applications are automatically configured by IIS with the web.config file. Clients are not. This means that if you have a <service> element inside the <system.runtime.remoting> in the web.config file, IIS will register the channels automatically to make the service availible.