Show / Hide Table of Contents

    IDesign Method conventions

    This example leverages the IDesign Method conventions on top of the core framework to build, run, and call a Manager 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

    IMyManager.cs

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

    MyManager.cs

    using System;
    using System.Threading.Tasks;
    using MySystem.Manager.My.Interface.V1;
    
    namespace MySystem.Manager.My.Service.V1
    {
        //Write your service as a pure .NET class.
        public class MyManager : IMyManager
        {
            public Task<string> MyMethod(string args)
            {
                Console.WriteLine($"Received {args}!");
                return Task.FromResult($"Response for {args}!");
            }
        }
    }
    

    Hosting.cs

    using System.Collections.Generic;
    using Copper.Conventions.Method.Service.V1;
    using Microsoft.Extensions.Configuration;
    using MySystem.Manager.My.Service.V1;
    
    namespace MySystem.Manager.My.Hosting
    {
        public static class Hosting
        {
            //Defines a host for MyManager.
            public static ManagerBuilder Register()
            {
                //Defines configuration used by the Method conventions.
                var configurationDictionary = new Dictionary<string, string>
                {
                    { "Copper:ConnectionStrings:MessageBusConnectionString", "rabbitmq://localhost" }
                };
    
                return ManagerBuilder.For<MyManager>()
                    //Configures the host.
                    .Configure(configurationBuilder => configurationBuilder
                        //Adds the configuration to the host.
                        .AddInMemoryCollection(configurationDictionary));
            }
        }
    }
    

    Host.cs

    using System;
    using System.Threading.Tasks;
    using Copper.Conventions.Method.Hosting.V1;
    using Copper.Hosting;
    using MySystem.Manager.My.Service.V1;
    
    namespace MySystem.Microservice.My
    {
        //Hosts your microservices
        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 Microservice type named MySystem.Microservice.My to the host.
                    .AddMicroserviceType("MySystem.Microservice.My", () => MicroserviceBuilder.Create()
                        //Adds a MyManager service type to the Microservice type.
                        .AddManagerType<MyManager>(() => Manager.My.Hosting.Hosting.Register())
                        //Adds an instance of the MyService service type to the Microservice type.
                        .AddManager<MyManager>())
                    //Adds an instance named MySystem.Microservice.My of the MySystem.Microservice.My Microservice type to the host.
                    .AddMicroservice("MySystem.Microservice.My", "MySystem.Microservice.My")
                    //Builds the host.
                    .Build()
                    //Runs the host.
                    .RunAsync().ConfigureAwait(false);
    
                Console.WriteLine("Hosted...");
                Console.WriteLine("Enter to close");
                Console.ReadLine();
            }
        }
    }
    

    ServiceSdkExample.csproj

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <StartupObject>MySystem.Microservice.My.Host</StartupObject>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Copper.Conventions.Method.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.Collections.Generic;
    using System.Threading.Tasks;
    using Copper.Conventions.Method.Client.V1;
    using Microsoft.Extensions.Configuration;
    using MySystem.Manager.My.Interface.V1;
    
    namespace ClientSdkExample
    {
        public static class Program
        {
            public static async Task Main()
            {
                //Defines configuration used by the Method conventions.
                var configurationDictionary = new Dictionary<string, string>
                {
                    { "Copper:ConnectionStrings:MessageBusConnectionString", "rabbitmq://localhost" }
                };
    
                //Defines a Manager proxy.
                var managerProxy = ManagerProxyBuilder.Create()
                    .Configure(configurationBuilder => configurationBuilder
                        .AddInMemoryCollection(configurationDictionary))
                    //Builds the Manager proxy.
                    .Build();
    
                //Creates an IMyManager proxy.
                var iMyManager = managerProxy.For<IMyManager>("MySystem.Microservice.My");
    
                //Invokes the MyMethod operation on the IMyManager proxy.
                var result = await iMyManager.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.Conventions.Method.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
    In This Article
    • Service SDK
    • Client SDK
    Back to top Generated by DocFX