Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Einfaches WCF-Beispiel ohne komplizierte Config

Einfaches WCF-Beispiel ohne komplizierte Config
September 9, 2014 3 min read BEN ABT
.NET Csharp WCF

Dieser Artikel stammt aus dem Jahr 2014. Versionen, Werkzeuge und Empfehlungen können sich seitdem geändert haben. Neuere Beiträge findest du unter .NET.

Unter anderem durch die Aktivität im myCSharp.de-Forum ist mir aufgefallen, dass viele Entwickler doch Probleme mit der Konfiguration von WCF-Diensten haben. Zugegeben; ich bin auch kein großer Fan des .NET-Konfigurationsframeworks. Es vereinfacht manches, aber es verkompliziert auch vieles, da unnötige Werte Verwirrung stiften.

Das finde ich wirklich schade. Denn dabei ist ein WCF-Service fundamental in wenigen Zeilen geschrieben. Ihr braucht dafür nur 3 Projekte:

<img src="/contents/2016/WCFCalcExample_Projektuebersicht.png"

Das Betreiben von WCF in Konsolen-Anwendungen nennt man “self-hosted”. Entgegen dem zentralen Betreiben auf Webservern wie dem IIS, kann der WCF hier in einer Anwendung gehalten werden, um zum Beispiel eine Process-To-Process-Kommunikation zu ermöglichen; im Gegensatz zur klassischen IPC ist es hiermit jedoch einfacher auch Maschinen-übergreifend zu kommunizieren.

Beispielhaft habe ich hier ein WCF-Service (CalcService) erstellt, der die grundlegenste, bidirektionale Kommunikation darstellt: eine einfache Addition auf Server-Seite. Der Client liefert dafür die beiden Zahlen-Werte und erhält die Summe als Antwort.

Code Client:

C#
 1// Client Source
 2class Program
 3{
 4    public const String Host = "net.tcp://localhost";
 5    public const Int32 Port = 31227;
 6    public const String ServiceName = "MathService";
 7 
 8    static void Main( string[ ] args )
 9    {
10        // Address
11        string svcAddress = Host + ":" + Port + "/" + ServiceName;
12 
13        // Binding
14        NetTcpBinding tcpb = new NetTcpBinding( SecurityMode.Message );
15        ChannelFactory<ICalcService> chFactory = new ChannelFactory<ICalcService>( tcpb );
16 
17        // Endpoint
18        EndpointAddress epAddress = new EndpointAddress( svcAddress );
19 
20        // Create Channel
21        ICalcService calcService = chFactory.CreateChannel( epAddress );
22 
23        Console.WriteLine( "Connected to service '" + svcAddress + "'." );
24        var result = calcService.Add( 4, 5 );
25 
26        Console.WriteLine( "The result of 4+5 is '" + result + "'." );
27 
28        Console.WriteLine( "Press key to quit." );
29        Console.ReadKey( );
30 
31        chFactory.Close( );
32    }
33}

Code Server:

C#
 1class Program
 2{
 3    public const String Host = "net.tcp://localhost";
 4    public const Int32 Port = 31227;
 5    public const String ServiceName = "MathService";
 6 
 7    static void Main( string[ ] args )
 8    {
 9        string svcAddress = Host + ":" + Port + "/" + ServiceName;
10        Uri svcUri = new Uri( svcAddress );
11  
12        using ( ServiceHost sh = new ServiceHost( typeof( CalcService ), svcUri ) )
13        {
14            // Binding
15            NetTcpBinding tcpBinding = new NetTcpBinding( SecurityMode.Message );
16 
17            // Behavior
18            ServiceMetadataBehavior behavior = new ServiceMetadataBehavior( );
19            sh.Description.Behaviors.Add( behavior );
20            sh.AddServiceEndpoint( typeof( IMetadataExchange ), MetadataExchangeBindings.CreateMexTcpBinding( ), "mex" );
21 
22            //Endpoint
23            sh.AddServiceEndpoint( typeof( ICalcService ), tcpBinding, svcAddress );
24 
25            // Open
26            sh.Open( );
27 
28            Console.WriteLine( "Service started '" + svcAddress + "' ...Press key to quit." );
29            Console.ReadKey( );
30 
31            Console.WriteLine( "Quit. Press key to close." );
32            Console.ReadKey( );
33 
34            // Close
35            sh.Close( );
36        }
37     }
38}

Client und Server müssen beide die Schnittstelle des Services kennen. Diese Logik ist in einem dritten Projekt ausgelagert.

Code Service

C#
 1[ServiceContract]
 2public interface ICalcService
 3{
 4    [OperationContract]
 5    int Add( Int32 n1, Int32 n2 );
 6}
 7 
 8public class CalcService : ICalcService
 9{
10    public int Add( Int32 n1, Int32 n2 )
11    {
12        return n1 + n2;
13    }
14}

Download / CodePlex

Der vollständige Quellcode ist unter CodePlex als WCFCalcExample verfügbar.

Related articles

Production Rate Limiting in ASP.NET Core

Nov 3, 2026 - 16 min read

Production Rate Limiting in ASP.NET Core

Most ASP.NET Core rate-limiting examples stop after registering a fixed window. That is enough to demonstrate the middleware, but it is not …

The Big Decimal Bugfix with .NET 11

Nov 2, 2026 - 11 min read

The Big Decimal Bugfix with .NET 11

For more than two decades, (decimal)1.23 returned 1.23m. It looked correct, it passed every test and it was a polite lie.

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026 - 10 min read

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled and Alpine are often compared as three equivalent Linux distributions. They are not. Alpine is a distribution built …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

Let's Work Together

Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact