Welcome to the navigation

Laboris ut sint eiusmod anim non esse tempor fugiat ad dolore sunt ea duis dolor nulla est consectetur exercitation proident, veniam, voluptate aliqua, laborum, commodo. Est in occaecat laborum, ex sit esse aliquip ea amet, anim elit, deserunt eu incididunt in quis aute ipsum fugiat lorem aliqua, eiusmod exercitation enim

Yeah, this will be replaced... But please enjoy the search!

Register an external WCF service using the Episerver ServiceLocator

There is a neat trick to register external services in ServiceLocators, this also goes for the ServiceLocator in Episerver. This is a useful way to invert the control between client and service classes. It also introduce loose coupling of WCF services and constructs a way for concrete implementations with the interface using the factory class.

I wont go into depth of SOAP/WCF services in this post instead I will examplify a simple usage example.

The WCF service

I created a WCF service, left it as it was

public class Service1 : IService1
    public string GetData(int value)
        return string.Format("You entered: {0}", value);

    public CompositeType GetDataUsingDataContract(CompositeType composite)
        if (composite == null)
            throw new ArgumentNullException("composite");
        if (composite.BoolValue)
            composite.StringValue += "Suffix";
        return composite;

Test run, simply hit F5

Add the service

Add the service reference to your Episerver project



So far everything is normal, from this point you can simply run

var client = new Service1Client(); // don't do this
var result = client.GetData(345);

To implement the client in the ServiceLocator create a class and let it inherit the Service1Client and register the interface in the locator

public class Service1Repository : Service1Client
    // any custom code, optional    

From this point on the WCF service is registered in the ServiceLocator and can be resolved like this

var clientRepository = ServiceLocator.Current.GetInstance<Service1Repository>();

Which means this 

<div class="alert alert-info" role="alert">

Will return

All done, use it clever

Fun stuff

Since the service is registered in the Episerver container via structuremap constructor injection is active, hit youtube for a couple of days to learn this voodoo if you are in the dark. If you need to use the service in a class simply add it as a constructor argument, in example

public class MyRepository
    private IService1 _service1;
    public MyRepository(IService1 service1) // structuremap will construct this
        _service1 = service1;

    public string DoStuff()
        return $"The serice1 returned: '{_service1.GetData(123)}'";

The usage could be

// the service1 will automatically be constructed when creating the myRepository
// instance through the servicelocator var myRepository = ServiceLocator.Current.GetInstance<MyRepository>(); <div class="alert alert-info" role="alert"> @myRepository.DoStuff() </div> }