Windows Communication Foundation

from Wikipedia, the free encyclopedia
Windows Communication Foundation
Basic data

developer Microsoft
Current  version 4.5
(October 9, 2012)
operating system Version 3.x: from Windows XP with SP2
Version 4.x: from Windows XP with SP3
category platform
License Proprietary software
German speaking Yes
microsoft.com

The Windows Communication Foundation ( WCF , former code name Indigo ) is a service-oriented communication platform for distributed applications in Microsoft Windows . It brings together many network functions and makes them available to the programmers of such applications in a standardized way.

Through the WCF, the communication technologies DCOM , Enterprise Services , MSMQ , WSE and Web Services are combined under a uniform programming interface . The .NET remoting will be replaced with the WCF . WCF can be used for developing service-oriented architectures . The Windows Communication Foundation also enables interoperability with Java Web Services, which were implemented using Web Services Interoperability Technology .

The Windows Communication Foundation has been an integral part of the .NET Framework since .NET Framework Version 3.0. The version numbers of WCF are based on the version of the .NET Framework.

For the new .NET Core Framework, WCF is not available in its previous form, but it is to be replaced by a Core WCF that is still to be developed (in a community project) , but on the client side, access to existing .NET WCF servers is to be made possible . As an alternative, Microsoft recommends the use of gRPC , which is supported from .NET Core 3.0.

concept

The WCF abstracts the concept of the end point by separating it into A ddress, B inding and C ontract (ABC principle).

  • The Address is a URI that describes the location of the service and thus identifies its availability for the service consumers.
  • The binding describes the type of communication, which includes the characteristics of the coding and the protocol .
  • The contract represents the service definition, in particular including the methods made available.

Address

The location of the service is, depending on the transport scheme used, the name of the target computer, the network or a resource of the type pipe , communication port or queue .

Binding

Binding includes parameters such as protocol ( HTTP , TCP , UDP and Windows' own protocols) and coding ( binary , SOAP document, own format), as well as security aspects ( encryption , authentication ).

The .NET Framework provides ready-made bindings for common use cases that can still be configured. It is also possible to develop your own bindings (for example for the XML-RPC protocol).

Contract

At development time, contracts are written as interfaces in any .NET language and at runtime by the WCF in a communication protocol e.g. B. SOAP implemented. The use of this standard is essential for platform-independent service access.

Behaviors

The behavior of a WCF connection is defined by behaviors . Depending on the area, a distinction is made between:

  • Service Behavior
  • Endpoint Behavior
  • Contract Behavior
  • Operation Behavior

Examples

Examples of implementing a simple WCF service are shown below.

Implementation of a contract

The contract is used by both the host and the client application and should therefore be in its own assembly .

using System.ServiceModel;

namespace HelloWorld.Contract
{
    [ServiceContract(Namespace = "http://de.wikipedia.org/wiki")]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello();
    }
}

Implementation of the service

using HelloWorld.Contract;

namespace HelloWorld.Service
{
    public class HelloWorldService : IHelloWorldService
    {
        public string SayHello()
        {
            return "Hello World";
        }
    }
}

Imperative implementation of a host application

using System;
using System.ServiceModel;
using HelloWorld.Contract;
using HelloWorld.Service;

namespace HelloWorld.ConsoleHost {
    class Program
    {
        static void Main()
        {
            using (var host = new ServiceHost(typeof (HelloWorldService), new Uri("http://localhost:8000/HelloWorld")))
            {
                host.AddServiceEndpoint(typeof (IHelloWorldService), new BasicHttpBinding(), "");
                host.Open();
                Console.WriteLine("Service running. Press ENTER to stop.");
                Console.ReadLine();
            }
        }
    }
}

Implementation of a client

using System;
using System.ServiceModel;
using HelloWorld.Contract;

namespace HelloWorld.ConsoleClient
{
    class Program
    {
        static void Main()
        {
            var endpointAddress = new EndpointAddress("http://localhost:8000/HelloWorld");
            var proxy = ChannelFactory<IHelloWorldService>.CreateChannel(new BasicHttpBinding(), endpointAddress);
            var helloWorld = proxy.SayHello();
            Console.WriteLine(helloWorld);
        }
    }
}

Declarative implementation of a host application

using System;
using System.ServiceModel;
using HelloWorld.Contract;
using HelloWorld.Service;

namespace HelloWorld.ConsoleHost
{
    class Program
    {
        static void Main()
        {
            using (var host = new ServiceHost(typeof (HelloWorldService)))
            {
                host.Open();
                Console.WriteLine("Service running. Press ENTER to stop.");
                Console.ReadLine();
            }
        }
    }
}

The behavior of the service is declared in the App.configor Web.configfile. This is usually done with the Service Configuration Editor ( svcconfigeditor.exe) and can also be configured manually in a text editor by experienced developers and administrators .

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="basicHttpBindingConfiguration" />
            </basicHttpBinding>
            <mexHttpBinding>
                <binding name="mexHttpBindingConfiguration" />
            </mexHttpBinding>
        </bindings>
        <behaviors>
            <serviceBehaviors>
                <behavior name="metadataExchangeBehavior">
                    <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="metadataExchangeBehavior" name="HelloWorld.Service.HelloWorldService">
                <endpoint
                    address=""
                    binding="basicHttpBinding"
                    bindingConfiguration="basicHttpBindingConfiguration"
                    name="basicHttpEndpoint"
                    contract="HelloWorld.Contract.IHelloWorldService" />
                <endpoint
                    address="mex"
                    binding="mexHttpBinding"
                    bindingConfiguration="mexHttpBindingConfiguration"
                    name="mexEndpoint"
                    contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8000/HelloWorld" />
                    </baseAddresses>
                </host>
            </service>
        </services>
    </system.serviceModel>
</configuration>

By activation of the metadata exchange (English: Metadata Exchange, or "mex") by a corresponding bindings (. Eg mexHttpBinding, mexHttpsBinding, mexTcpBinding, mexNamedPipeBinding), the proxy classes and the application configuration with the can Service Metadata Utility ( svcutil.exe) are automatically created.

svcutil /d:"C:\ … \HelloWorld\ConsoleClient" /o:"HelloWorldProxy.cs" /config:"App.config" http://localhost:8000/HelloWorld/mex

The generated proxy classes and application configuration can be easily consumed by a client.

using System;
using de.wikipedia.org.wiki; // service namespace as declared in the contract

namespace HelloWorld.ConsoleClient
{
    class Program
    {
        static void Main()
        {
            var proxy = new HelloWorldProxy(); // no using block here!
            var helloWorld = proxy.HelloWorld();
            Console.WriteLine(helloWorld);
        }
    }
}

WCF Service Host and WCF Test Client

Another possibility is to host the service directly using the WCF Service Host ( wcfsvchost.exe).

wcfsvchost /service:"C:\ … \HelloWorld.Service.dll" /config:"C:\ … \App.config"

Running services that support the metadata exchange can be analyzed and tested with the WCF Test Client ( wcftestclient.exe).

wcftestclient http://localhost:8000/HelloWorld/mex

literature

  • Michele Leroux Bustamante: Learning WCF: A Hands-on Guide . 2nd Edition. O'Reilly, 2007, ISBN 978-0-596-10162-6 (English).
  • Juval Lowy: Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus . 3. Edition. O'Reilly, 2010, ISBN 978-0-596-80548-7 (English).
  • John Sharp: Windows ® Communication Foundation 4 Step by Step . 1st edition. Microsoft Press, 2010, ISBN 978-0-7356-4556-1 (English).

Web links

Individual evidence

  1. Web Services Interoperability Technology Features. Oracle Sun developer network, accessed November 19, 2011 .
  2. ^ Main repository for the Core WCF project. Contribute to CoreWCF / CoreWCF development by creating an account on GitHub. Core WCF, June 22, 2019, accessed June 23, 2019 .
  3. ^ Supporting the community with WF and WCF OSS projects. June 7, 2019, Retrieved July 20, 2019 (American English).
  4. Why isn't WCF supported in .Net Core? Retrieved July 20, 2019 (UK English).
  5. JunTaoLuo: gRPC services with ASP.NET Core. Retrieved September 24, 2019 (German).
  6. heise online: Build 2019: Microsoft specifies the plans for .NET 5.0. Retrieved September 24, 2019 .