If you want to control the instancing of the WCF service instances, you'll need a service behavior to plug an IInstanceProvider
for that. You can find a simple provider implementation (for an IoC container) in the post about that interface at http://blogs.msdn.com/b/carlosfigueira/archive/2011/05/31/wcf-extensibility-iinstanceprovider.aspx.
Per the comments, if all you need is a simple IServiceBehavior, here's a sample implementation you can use.
public class StackOverflow_6539963
{
public class MyServiceBehaviorAttribute : Attribute, IServiceBehavior
{
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
Console.WriteLine("In MyServiceBehaviorAttribute.ApplyDispatchBehavior");
// do whatever initialization you need
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
[ServiceContract]
public interface ITest
{
[OperationContract]
string Echo(string text);
}
[MyServiceBehavior]
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.Open();
Console.WriteLine("Host opened");
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}