Show / Hide Table of Contents

    Core framework

    This example leverages the core framework to build, run, and call a service.

    Important

    Using a single project for service, interface, and hosting code is not recommended and is only done for simplicity in these examples.

    Service SDK

    IMyService.cs

    using System.ServiceModel;
    using System.Threading.Tasks;
    
    namespace ServiceSdkExample
    {
        //Copper leverages System.ServiceModel for defining service/data contracts.
        [ServiceContract]
        public interface IMyService
        {
            [OperationContract]
            public Task<string> MyMethod(string args);
        }
    }
    

    MyService.cs

    using System;
    using System.Threading.Tasks;
    
    namespace ServiceSdkExample
    {
        //Write your service as a pure .NET class.
        public class MyService : IMyService
        {
            public Task<string> MyMethod(string args)
            {
                Console.WriteLine($"Received {args}!");
                return Task.FromResult($"Response for {args}!");
            }
        }
    }
    

    Hosting.cs

    using Copper.Remoting.Service;
    using Copper.Service;
    
    namespace ServiceSdkExample
    {
        public static class Hosting
        {
            //Defines a host for MyService.
            public static ServiceHostBuilder Register()
            {
                return ServiceHostBuilder.Create("MyService")
                    //Appends a MyService instance to the service type.
                    .AppendServiceInstance<MyService>()
                    //Appends a Remoting listener for the IMyService contract of the MyService instance.
                    .AppendRemotingServiceInstanceListener<IMyService, MyService>("MyListener", requestServiceInstanceListenerBuilder => requestServiceInstanceListenerBuilder
                        //Configures the remoting listener to use the MassTransit transport.
                        .UseMassTransit(massTransitRequestReceiverBuilder => massTransitRequestReceiverBuilder
                            //Configures the MassTransit transport to connect with a local RabbitMQ broker.
                            .UseDefaultRabbitMq("rabbitmq://localhost", "MyServiceQueueName")));
            }
        }
    }
    

    Host.cs

    using System;
    using System.Threading.Tasks;
    using Copper.Hosting;
    
    namespace ServiceSdkExample
    {
        public static class Host
        {
            public static async Task Main()
            {
                //Copper uses an application model similar to Azure Service Fabric.
                //Defines a host that runs applications.
                await FabricHostBuilder.Create()
                    //Adds an application type named MyApplication to the host.
                    .AddApplicationType("MyApplication", () => ApplicationHostBuilder.Create()
                        //Adds a service type named MyService to the application type.
                        .AddServiceType("MyService", () => Hosting.Register())
                        //Adds an instance named MyServiceInstance of the MyService service type to the application type.
                        .AddService("MyServiceInstance", "MyService"))
                    //Adds an instance named MyApplicationInstance of the MyApplication application type to the host.
                    .AddApplication("MyApplicationInstance", "MyApplication")
                    //Builds the host.
                    .Build()
                    //Runs the host.
                    .RunAsync();
    
                Console.WriteLine("Hosted...");
                Console.WriteLine("Enter to close");
                Console.ReadLine();
            }
        }
    }
    

    ServiceSdkExample.csproj

    <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
          <OutputType>Exe</OutputType>
          <StartupObject>ServiceSdkExample.Host</StartupObject>
        </PropertyGroup>
        <ItemGroup>
            <PackageReference Include="Copper.Service.DotNet.ServiceFabric" Version="$(CopperPackagesVersion)" />
            <PackageReference Include="Copper.Remoting.MassTransit.Service.DotNet.ServiceFabric" Version="$(CopperPackagesVersion)" />
            <PackageReference Include="Copper.Hosting.DotNet.ServiceFabric" Version="$(CopperPackagesVersion)" />
        </ItemGroup>
    </Project>
    
    Note

    Define a CopperPackagesVersion MSBuild property or replace the reference with the latest Copper version.


    Client SDK

    Program.cs

    using System;
    using System.Threading.Tasks;
    using Copper.Remoting.Client;
    using Copper.Remoting.Common;
    using ServiceSdkExample;
    
    namespace ClientSdkExample
    {
        public static class Program
        {
            public static async Task Main()
            {
                //Defines a service proxy.
                var serviceProxy = ServiceProxyBuilder.Create()
                    //Builds the service proxy.
                    .Build();
    
                //Defines a request submitter builder
                var requestSubmitterBuilder = RequestSubmitterBuilder.Create()
                    //Configures the request submitter to use the MassTransit transport.
                    .UseMassTransit(massTransitRequestSubmitterBuilder => massTransitRequestSubmitterBuilder
                        //Configures the MassTransit transport to connect with a local RabbitMQ broker.
                        .UseDefaultRabbitMq("rabbitmq://localhost", "MyServiceQueueName"));
    
                //Creates an IMyService proxy.
                var iMyService = serviceProxy.For<IMyService>(requestSubmitterBuilder);
    
                //Invokes the MyMethod operation on the IMyService proxy.
                var result = await iMyService.MyMethod("Args").ConfigureAwait(false);
    
                Console.WriteLine(result);
                Console.ReadLine();
            }
        }
    }
    

    ClientSdkExample.csproj

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Copper.Remoting.MassTransit.Client" Version="$(CopperPackagesVersion)" />
      </ItemGroup>
      <ItemGroup>
        <ProjectReference Include="..\ServiceSdkExample\ServiceSdkExample.csproj" />
      </ItemGroup>
    </Project>
    
    Note

    Define a CopperPackagesVersion MSBuild property or replace the reference with the latest Copper version.


    • Improve this Doc
    Back to top Generated by DocFX