Brendon Randall
Brendon Randall

Reputation: 1466

C# Add WCF endpoint Dynamically to base URL

Im currently developing a system that can load up plugins that are created for it and would like for them to dynamically create a WCF endpoint.

What I am after is this,

Lets look at an example of what im tyring to do. In my testing I have a site with a base address

http://localhost:9812/

What I am trying to do is when I load my plugin, add its WCF endpoint configuration to that url, so ending up with

http://localhost:9812/MyPlugin

I do not want to do this in the web.Config file but rather in code. I have managed to get it to work as long as I change the port number it binds to i.e

http://localhost:9787/MyPlugin

This is what my code looks like so far

public void StartWcfServices()
    {
        IWcfExample endpoint = new EchoEndpoint();
        var uri = new Uri(_baseAddress);
        string address = _baseAddress + endpoint.GetName();

        var serviceHost = new ServiceHost(endpoint, uri);
        serviceHost.AddServiceEndpoint(typeof(IWcfExample), new BasicHttpBinding(), address);

        serviceHost.Open();

    }

Problem being when I start the web site I get an error

HTTP could not register URL http://+:9812/ because TCP port 9812 is being used by another application.

Any help would be greatly appreciated.

Upvotes: 0

Views: 1814

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364399

The problem here is combination of IIS hosting and self hosting. I believe your application runs ASP.NET MVC in IIS on website exposed on 9812 TCP port. In such case you cannot open ServiceHost to listen on the same port because that port is already used by IIS - you can try to use only relative endpoint address but it will probably not work because base address is defined in other process. Moreover manually opening service host in web application smells. Let IIS infrastructure to do that or host your plugin services in windows service.

If you want plugins in web application each plugin should ship with its own .svc file for every new exposed service. After plugin installation and restarting your application the service will become available on http://localhost:9812/PluginDir/ServiceName.svc

Upvotes: 1

Related Questions