Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

WCF service dependency injection

// 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
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# check number is odd or even 
Csharp :: cache.TryGetValue in MemoryCache c# .net 
Csharp :: c# validate username and password 
Csharp :: Permutation and Combination in C# 
Csharp :: one to many relationship in asp net entity framework with role 
Csharp :: what is string args in c# 
Csharp :: how to use external resource.resx file in c# 
Csharp :: Smooth Sentences c# 
Csharp :: Get the current culture in a controller asp.net-core 6 
Csharp :: universities in greece 
Csharp :: unity async await 
Csharp :: listview android studio java 
Csharp :: how to not overwrite a text file in c# 
Csharp :: minimum value int C# 
Csharp :: Insertion sort in c# 
Csharp :: C# String Manipulation: 
Csharp :: c# download file from url 
Csharp :: urp set postprocessing value 
Csharp :: c# convert ad objectguid to string 
Csharp :: how to do Employing defensive code in the UI to ensure that the current frame is the most top level window in c# 
Html :: opem link in new tab html 
Html :: enter key vue 
Html :: how to make a whatsapp hyperlink html 
Html :: textarea placeholder css 
Html :: tab in html 
Html :: html select required 
Html :: bsc chain rpc 
Html :: add icon to website tab 
Html :: html favicon 
Html :: how to validate mobile number in html form 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =