Skip to content

Latest commit

 

History

History

WcfServiceLibrary

WCF sample

Implement a Ioc attribute to mark services' classes, like here

[AttributeUsage(AttributeTargets.Class)]
public class IocAttribute: Attribute, IServiceBehavior
{
    private static readonly IContainer Container = IoC.Container.Create().Using<Configuration>();

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        var serviceType = serviceDescription.ServiceType;
        var resolver = Container.GetResolver<object>(serviceType);
        var instanceProvider = new InstanceProvider(() => resolver(Container));
        var dispatchRuntimes =
            from dispatcher in serviceHostBase.ChannelDispatchers.OfType<ChannelDispatcher>()
            from endpointDispatcher in dispatcher.Endpoints
            select endpointDispatcher.DispatchRuntime;

        foreach(var dispatchRuntime in dispatchRuntimes)
        {
            dispatchRuntime.InstanceProvider = instanceProvider;
        }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters) { }

    private class InstanceProvider: IInstanceProvider
    {
        private readonly Func<object> _factory;

        public InstanceProvider(Func<object> factory) => _factory = factory;

        public object GetInstance(InstanceContext instanceContext) => _factory();

        public object GetInstance(InstanceContext instanceContext, Message message) => _factory();

        public void ReleaseInstance(InstanceContext instanceContext, object instance) { }
    }
}

Where Configuration is the configuration of IoC container.

Implement an extension element, for instance like this

public class IocExtensionElement: BehaviorExtensionElement
{
    public override Type BehaviorType => typeof(IocAttribute);

    protected override object CreateBehavior() => new IocAttribute();
}

Add several elements to the configuration file, for example

Add the Ioc behavior to the WCF service:

<behaviors>
  <serviceBehaviors>
    <behavior>

      <Ioc />

    </behavior>
  </serviceBehaviors>
</behaviors>

Add the Ioc behavior extension:

<system.serviceModel>
  <extensions>
    <behaviorExtensions>

      <add name="Ioc" type="WcfServiceLibrary.IocExtensionElement, WcfServiceLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
 
   </behaviorExtensions>
  </extensions>
</system.serviceModel>

Add Ioc attribute for WCF service implementations, for example

[Ioc]
public class Service : IService
{
    public string GetData(int value) => $"You entered: {value}";
}