Search code examples
.netweb-serviceswcf.net-core

Alter a WCF Service for Compatibility with both .NET Framework & .NET Core Callers


TL;DR I have an existing WCF service hosted as a Windows Service which I want to modify so that it can be called by an existing .NET Framework 4.7 app as well as a new .NET Core 2.2 app. What's the simplest approach to solving this problem?

Full Explanation: I have an existing Web App (call it 'A') running on .NET Framework 4.7. It talks to an existing WCF Service 'B' (a Windows Service) also on .NET 4.7. They communicate over a NetTcpBinding.

I'm building a new Web App 'C' on .NET Core 2.2. I need it to talk to the existing service B. I can modify A and B as much as needed, but time and money are limited. Also, B consumes an API that runs only on .NET Framework, so it can not be converted to run on .NET Core, much to my frustration.

Service B is tiny. It's entire exposed contract is 5 methods. Security is really minimal. It's an intranet-only app at a tiny company.

What is the fastest, simplest way to modify the existing WCF Service B (and A if needed) so that it can talk to both apps A and C?

I'm guessing I should just change the WCF Binding on apps A and B to something I can easily talk to using an HttpClient on C; if so, which binding is that and how do I use the HttpClient to interface with it? Also, are there any hidden "gotchas" to changing the WCF Binding from NetTcpBinding to something else that are going to bite me?


Solution

  • Yes, we could publish the service with Basichttpbinding and WebHttpBinding, given that the Core is not well compatible with WS-binding.
    Using basichttpbinding to create the service is simple, just replace the System.servicemodel section with the below code snippets.

      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpBinding" scheme="http" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
    

    Then we could call the service by adding service reference. enter image description here
    Or we publish restful style service by using Webhttpbinding, just like Asp.Net WebAPI.
    https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-web-http-programming-model
    Then we could call the service by constructing an HTTP request, GET/POST/PUT/Delete, with a request body. It is supported by both Core and .netframework since Http protocol is generic.