"WCF service dependency injection" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "WCF service dependency injection" answered properly. Developers are finding an appropriate answer about WCF service dependency injection related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like WCF service dependency injection. Enter your desired code related query in the search bar and get every piece of information about C# code related question on WCF service dependency injection. 

WCF service dependency injection

By Open OxOpen Ox on Apr 30, 2021
// You'll need to implement a combination of custom ServiceHostFactory, ServiceHost and IInstanceProvider.
// Given a service with this constructor signature:
//public MyService(IDependency dep)

// Register MyServiceHostFactory in your MyService.svc file, or use MyServiceHost directly in code for self-hosting scenarios.

public class MyServiceHostFactory : ServiceHostFactory
{
    private readonly IDependency dep;

    public MyServiceHostFactory()
    {
        this.dep = new MyClass();
    }

    protected override ServiceHost CreateServiceHost(Type serviceType,
        Uri[] baseAddresses)
    {
        return new MyServiceHost(this.dep, serviceType, baseAddresses);
    }
}

public class MyServiceHost : ServiceHost
{
    public MyServiceHost(IDependency dep, Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    {
        if (dep == null)
        {
            throw new ArgumentNullException("dep");
        }

        foreach (var cd in this.ImplementedContracts.Values)
        {
            cd.Behaviors.Add(new MyInstanceProvider(dep));
        }
    }
}

public class MyInstanceProvider : IInstanceProvider, IContractBehavior
{
    private readonly IDependency dep;

    public MyInstanceProvider(IDependency dep)
    {
        if (dep == null)
        {
            throw new ArgumentNullException("dep");
        }

        this.dep = dep;
    }

    #region IInstanceProvider Members

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return this.GetInstance(instanceContext);
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return new MyService(this.dep);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
        var disposable = instance as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }

    #endregion

    #region IContractBehavior Members

    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        dispatchRuntime.InstanceProvider = this;
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
    }

    #endregion
}

Source: stackoverflow.com

Add Comment

0

All those coders who are working on the C# based application and are stuck on WCF service dependency injection can get a collection of related answers to their query. Programmers need to enter their query on WCF service dependency injection related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about WCF service dependency injection for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of WCF service dependency injection while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "WCF service dependency injection". Visit this developer's friendly online web community, CodeProZone, and get your queries like WCF service dependency injection resolved professionally and stay updated to the latest C# updates. 

C# answers related to "WCF service dependency injection"

View All C# queries

C# queries related to "WCF service dependency injection"

Browse Other Code Languages

CodeProZone