Advertisements

Hosting a WCF service from an AspNetCore application

Hosting a WCF service from an AspNetCore application

March 7, 2018 Packages 1

TLDR

If you want to host a WCF service in AspNetCore and don’t mind running on .Net Framework, check out Solid.AspNetCore.Extensions.Wcf on Nuget.

Background

In my current company, we are working on a project where we need to use a specific nuget package that is made for AspNetCore. There is however a requirement for the application to serve SOAP services as well as RESTful services. Originally, we were going to host seperate sattelite WCF service that would process the requests by making REST requests to the host application. We decided against that approach for various reasons and opted to investigate hosting a WCF service within an AspNetCore application.

There are a couple of libraries out there that can help you host a SOAP service in AspNetCore. The most prevalent is¬†SoapCore. The best thing about SoapCore is that it is .net standard. However, we didn’t want to rely on a custom SOAP stack with possible sporatic support. Since we wanted to rely on the WCF stack from Microsoft we were stuck using AspNetCore on .Net Framework.

The Research and Implementation

Our original idea was to implement listener interfaces in WCF that would be connected to AspNetCore middleware that would hook into a singleton ServiceHost that would reside within the ServiceProvider. This proved to be difficult and actually futile. What we didn’t know at the time is that, even if you’ve implemented your own listeners, ServiceHost will listen on the defined base addresses when you call ServiceHost.Open(). We tried going around this, but ultimately decided to not write too much custom code and use the stack as defined.

The plan at this point was that each WCF service within the AspNetCore application would get a random port to listen on. There would be AspNetCore middleware that would act as a proxy to the WCF service. What we also wanted to achieve was full integration with the ServiceProvider.

Installation

First off, your application needs to build to .Net Framework 4.6.1 or above. This is because the System.ServiceModel assembly hasn’t been implemented in .net standard for the server side of the WCF stack and, as far as I know, Microsoft hasn’t planned on doing it at all.

Second, you need to get the Solid.AspNetCore.Extensions.Wcf package from Nuget.

Example

Now you can add the WCF service to your AspNetCore application.

Model

[DataContract]
public class EchoObject
{
    [DataMember]
    public string Value { get; set; }
}

Contract

[ServiceContract]
public interface IService
{
    [OperationContract]
    string Echo(string str);
    [OperationContract]
    EchoObject ObjectEcho(EchoObject obj);
}

Service

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class Service : IService
{
    public string Echo(string str)
    {
        return str;
    }

    public EchoObject ObjectEcho(EchoObject obj)
    {
        return obj;
    }
}

Startup

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddWcfServiceWithMetadata();
    }
        
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseWcfService<Service, IService>("/service");
    }
}

Run your project

Yep, that’s it. That’s all you need for your basic WCF service using the BasicHttpBinding. There are more integrations¬†integration points that you can read about in the repository wiki.

Advertisements

 

One Response

  1. gislikonrad says:

    We will soon be releasing a blog post about more complex bindings for services. Stay tuned.