WCF: using my "custom" ServiceHost in my app.config / web.config for my wcf service?

I have created a simple custom ServiceHost that inherits ServiceHost and overrides the InitializeRuntime method.

How can I change my app.config / web.config to use the user host of the service, so an InitializeRunTime override is done.

I see attributes in the configuration file, such as behaviorConfiguration, etc., but there is nothing obvious where I can get it to use my Custom ServiceHost

My ServiceHost is simple: -

public class UnityServiceHost : ServiceHost
{
    protected override void InitializeRuntime()
    {

        //DO MY UNITY INJECTION HERE 
        //Console.WriteLine("");
        base.InitializeRuntime();
    }


}

Any help is really appreciated.

thanks

+3
source share
3 answers

, , Spring.NET: http://orand.blogspot.com/2006/10/wcf-service-dependency-injection.html

WCF " ", , DI.

1) IInstanceProvider GetInstance, , :

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return _container.Resolve(_serviceType);
    }

2) IServiceBehaviour, IInstanceProvider .

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
        {
            ChannelDispatcher cd = cdb as ChannelDispatcher;
            if (cd != null)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    ed.DispatchRuntime.InstanceProvider = 
                        new YourCustomInstanceProvider(serviceDescription.ServiceType);
                }
            }
        }
    }

3) OnOpening

    protected override void OnOpening()
    {
        this.Description.Behaviors.Add(new CustomServiceBehavior());
        base.OnOpening();
    }

, , , UnityContainer IInstanceProvider, .

+2

( IIS WAS), UnityServiceHost ServiceHost.

IIS WAS, ServiceHostFactory

0

You need to create a custom ServiceHostFactory and use it to create a UnityServiceHost. You specify the ServiceHostFactory to use in the SVC file. See MSDN docs for SVC syntax.

0
source

Source: https://habr.com/ru/post/1719626/


All Articles