Wednesday, May 11, 2022

CoreWCF Is Released

CoreWCF, the .NET Core version of Windows Communication Foundation is finally released. The 1.0 release of CoreWCF is compatible with .NET Standard 2.0 so that it will work with,
  • .NET Framework 4.6.2 (and above)
  • .NET Core 3.1
  • .NET 5+
In this post, let's have a look at a sample implementation of WCF on top of .NET 6.

I have an ASP.NET Core Web API (with Minimal API support) created and installed the following packages.
  <PackageReference Include="CoreWCF.Http" Version="1.0.1" />
  <PackageReference Include="CoreWCF.Primitives" Version="1.0.1" />
Then I created the following services. I am exposing two services as I want to show the support for different Bindings.

public interface IGreetService
    string Greet(string message);
public class GreetService : IGreetService
    public string Greet(string message)
        return $"You said: {message}";
public interface IAnotherGreetService
    string AnotherGreet(string message);
public class AnotherGreetService : IAnotherGreetService
    public string AnotherGreet(string message)
        return $"You said another: {message}";
Now I am modifying the Startup.cs as follows.

using CoreWCF;
using CoreWCF.Configuration;
using CoreWCF.Description;
using CoreWcfDemo.Server.Services;
var builder = WebApplication.CreateBuilder(args);
// Add WSDL support
builder.Services.AddSingleton<IServiceBehavior, UseRequestHeadersForMetadataAddressBehavior>();
WebApplication? app = builder.Build();
app.UseServiceModel(builder =>
    // This service only supports BasicHttpBinding
        .AddServiceEndpoint<GreetService, IGreetService>(new BasicHttpBinding(), 
    // This service supports BasicHttpBinding and WSHttpBinding
        .AddServiceEndpoint<AnotherGreetService, IAnotherGreetService>(new BasicHttpBinding(), 
        .AddServiceEndpoint<AnotherGreetService, IAnotherGreetService>(new WSHttpBinding(SecurityMode.Transport), 
var serviceMetadataBehavior = app.Services.GetRequiredService<ServiceMetadataBehavior>();
serviceMetadataBehavior.HttpGetEnabled = true;
serviceMetadataBehavior.HttpsGetEnabled = true;
serviceMetadataBehavior.HttpGetUrl = new Uri("http://localhost:5051/metadata");
serviceMetadataBehavior.HttpsGetUrl = new Uri("https://localhost:7051/metadata");
Now, I am adding a Console Application and adding Service References to the project.
Add Service Reference
Add Service Reference: WCF Web Service
Discover Services
Once the Service is discovered, I have selected Next and opted to use the defaults. And once the Service Reference is created, I have the following code to call the different WCF Service methods using different bindings.
using ServiceReference1;
// BasicHttpsBinding
var greetServiceClient = new GreetServiceClient(
var result = await greetServiceClient.GreetAsync("Hello");
// WSHttpBinding
var anotherGreetServiceClient = new AnotherGreetServiceClient(
result = await anotherGreetServiceClient.AnotherGreetAsync("Hello");
And now when I run the Console App while the Server App is running, I can see everything is working as expected.

You can find the complete code sample here:

Hope this helps.

Happy Coding.


No comments:

Post a Comment