Here's a self hosted sample that does the things I believe you are trying to do. It's a console app. You will need to start visual studio as an administrator in order to get netsh to register localhost:55001
I'm using castle 3.1.
The source code file:
namespace WcfSelfHost
{
using System;
using Castle.Windsor;
using Castle.Facilities.WcfIntegration;
using System.ServiceModel;
using Castle.MicroKernel.Registration;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using Castle.MicroKernel;
public interface ILog
{
void LogMessage(string message);
}
public class ConsoleLogger : ILog
{
public void LogMessage(string message)
{
Console.WriteLine(message);
}
}
[ServiceBehavior]
public class CastleCreatedLoggingServiceBehavior : IServiceBehavior
{
private readonly ILog logger;
public CastleCreatedLoggingServiceBehavior(ILog logger)
{
this.logger = logger;
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
this.logger.LogMessage("in AddBindingParameters");
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
this.logger.LogMessage("in ApplyDispatchBehavior");
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
this.logger.LogMessage("in Validate");
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
void TheOneOperation(string data);
}
public class ServiceImplementation : IService
{
private readonly ILog logger;
public ServiceImplementation(ILog logger)
{
this.logger = logger;
}
public void TheOneOperation(string data)
{
this.logger.LogMessage("service received message:");
this.logger.LogMessage(data);
}
}
public class ConsoleApplication
{
public static void Main()
{
//making this a variable to show the requirement that the names match
string serviceName = "TheService";
//configure the container with all the items we need
IWindsorContainer container = new WindsorContainer()
.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
.Register(
Component.For<CastleCreatedLoggingServiceBehavior>(),
Component.For<ILog>()
.ImplementedBy<ConsoleLogger>()
.LifestyleSingleton(),
Component.For<IService>()
.ImplementedBy<ServiceImplementation>()
.LifestyleSingleton()
.Named(serviceName)
);
//setup our factory with that has knowledge of our kernel.
DefaultServiceHostFactory factory = new DefaultServiceHostFactory(container.Kernel);
//create a host for our service matching the name of the castle component. Not adding any additional base addresses.
using (ServiceHostBase host = factory.CreateServiceHost(serviceName, new Uri[0]))
{
host.Open();
Console.WriteLine("server listening for messages");
//and here's the client..
IWindsorContainer clientContainer = new WindsorContainer()
.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero)
.Register(
Component.For<IService>()
.AsWcfClient(WcfEndpoint.FromConfiguration("serviceEndpointDefinition"))
);
IService client = clientContainer.Resolve<IService>();
client.TheOneOperation("data from client");
Console.ReadLine();
}
}
}
}
Here's the app.config file for the console application. We could have used the fluent API to configure all of this in the source code, but separating out the services & client config is pretty normal, so I chose to go config file route. Let me know if you want a c# fluent API version.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="overrideMessageSize_forBasicHttpBinding" maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647"/>
</basicHttpBinding>
</bindings>
<services>
<service name="WcfSelfHost.ServiceImplementation">
<host>
<baseAddresses>
<add baseAddress="http://localhost:55001/baseaddress"/>
</baseAddresses>
</host>
<endpoint
contract="WcfSelfHost.IService"
binding="basicHttpBinding"
bindingConfiguration="overrideMessageSize_forBasicHttpBinding"
address="http://localhost:55001/baseaddress/serviceimplementation"
/>
</service>
</services>
<client>
<endpoint
name="serviceEndpointDefinition"
contract="WcfSelfHost.IService"
binding="basicHttpBinding"
bindingConfiguration="overrideMessageSize_forBasicHttpBinding"
address="http://localhost:55001/baseaddress/serviceimplementation"
/>
</client>
</system.serviceModel>
</configuration>