Welcome to the navigation

Culpa nisi amet, eu mollit aliquip ad ut commodo ipsum duis et aute enim do dolore pariatur, excepteur minim cupidatat reprehenderit deserunt sunt voluptate velit. Lorem sit ipsum eu pariatur, mollit occaecat velit cillum culpa aliqua, labore non fugiat in ullamco laboris nostrud aute irure eiusmod incididunt aliquip voluptate proident

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

 

Utilize

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

[ServiceConfiguration(typeof(IService1))]
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">
    @clientRepository.GetData(123)
</div>

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

[ServiceConfiguration(typeof(MyRepository))]
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> }

Please note that this portal is a BETA site , I will continuously improve all functionality and performance during the coming weeks / the author