Simple example of IServiceBehavior and ApplyDispatchBehavior
Asked Answered
A

1

6

I am trying to Plug Unity into a WCF Service Library with a Service Behavior.

I need a simple bare bones example of a Service Behavior.

All I want to do is setup my IOC Unity Container on startup of the WCF Service.

NOTE: I am not using a WCF Service Application. So I don't have access to ANY of the ASP.NET ways of doing this. From a concept point of view, a service behavior seems like the most elegant method. But I don't know how to set one up (what code do I need, were do I update the config files, etc).

Anorthosite answered 30/6, 2011 at 19:19 Comment(0)
C
7

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();
    }
}
Coypu answered 30/6, 2011 at 19:32 Comment(6)
Unless I miss my guess, this is a way to get the WCF Service under test. So this fires on every "Instance" creation. (IInstanceProvider). I need a way to fire just once at startup. I am not actually interested in the WCF Service right now. I just want to be able to init my IOC Container at start up (But I will be used for my Business Layer objects, not the WCF Service Objects.Anorthosite
I see. Are you hosting your service on IIS (i.e., using a .svc file)? If so, you can still hook up to the global.asax Application_Start. If you're not hosting it on IIS (i.e., you're hosting it yourself), then you can initialize the IoC before you start the service host.Coypu
Both good ways to go. However, I need to be hosting agnostic. (My framework team is still working on the hosting solution for my services) So I am looking for a WCF solution that will work. I found info about IServiceBehavior and ApplyDispatchBehavior that seems to do what I want, but finding examples is hard.Anorthosite
Ok, I posted a simple example of an IServiceBehavior. If you want more information about it, you can find at blogs.msdn.com/b/carlosfigueira/archive/2011/03/22/…Coypu
The attribute for Service is given as MyServiceBehavior. I don't see MyServiceBehavior class anywhere in your code sample. Should the attribute otherwise be, MyServiceBehaviourAttribute, or am I missing something important here?Corinacorine
C# lets you omit the Attribute suffix from the class name when applying the attribute to a target. So using [MyServiceBehaviorAttribute] public class Service ... or [MyServiceBehavior] public class Service ... do essentially the same.Coypu

© 2022 - 2024 — McMap. All rights reserved.