Skip to content
Tsuyoshi Ushio edited this page Aug 12, 2016 · 7 revisions

Programming Microsoft Azure Service Fabric source code revision history

  1. Chapter 2 The First Version

1.1. P34: ServiceInstanceListener

CalculatorService.cs

         protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
         {
              return new[]
              {
                  new ServiceInstanceListener(context =>
                     this.CreateServiceRemotingListener(context))
              };
          }

Code Sample

1.2. P38: RegisterServiceType

Program.cs

                ServiceRuntime.RegisterServiceAsync("CalculatorServiceType",
                    context => new CalculatorService(context)).GetAwaiter().GetResult();

[Code Sample] (https://github.com/TsuyoshiUshio/AzureServiceFabricSample/blob/4cf37a68634018072b2efeaa6d47709820067e96/Chapter02/CalculatorApplication/CalculatorService/Program.cs#L24-L25)

  1. Chapter 2 The Second version

P39: Add / Subtract methods

        public Task<string> Add(int a, int b)
        {
            return Task.FromResult<string>(string.Format("Instance {0} returns: {1}",
                this.Context.InstanceId,
                a + b));
        }

        public Task<string> Subtract(int a, int b)
        {
            return Task.FromResult<string>(string.Format("Instance {0} returns: {1}",
                this.Context.InstanceId,
                a - b));
        }

Code Sample

  1. Chapter 2 The third version

P42: CreateServiceInstanceListeners()

        protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners()
        {
            return new[]
            {
                new ServiceInstanceListener(context =>
                    new WcfCommunicationListener<ICalculatorService>(
                        wcfServiceObject:this,
                        serviceContext:context,
                        endpointResourceName: "ServiceEndpoint",
                        listenerBinding: WcfUtility.CreateTcpListenerBinding()
                    )
            )};
        }

Code Sample

P48: OpenAsync

This revision is enable us to route http://localhost:80/webapp/api/... as the book says.

        public OwinCommunicationListener(IOwinAppBuilder startup, ServiceContext serviceContext, string appRoot)
        {
            this.startup = startup;
            this.serviceContext = serviceContext;
            this.appRoot = appRoot;
        }
        public Task<string> OpenAsync(CancellationToken cancellationToken)
        {
            EndpointResourceDescription serviceEndpoint = this.serviceContext.CodePackageActivationContext.GetEndpoint("ServiceEndpoint");
            int port = serviceEndpoint.Port;
            this.listeningAddress = String.Format("http://+:{0}/{1}/", port, appRoot);
                             :

Code Sample

This document is now up to Chapter 2. However, I've done up to Chapter 4 already. Enjoy via code until adding more info. Release notes for versioned sample

  1. Chapter 5 Code package

The code of the book works fine. However, we have a pitfall.

The batch file(cmd)'s default character encoding is different from Visual Studio. If you create cmd file via Visual Studio, it might be UTF-8 with BOM. In Japan, cmd's character encoding default is Shift_JIS.

The cmd will execute in Shift_JIS. Please check your default encoding of batch files of your country.

Code Sample

  1. Chapter 5 Data package

P99 Data package 4.

            while (!cancellationToken.IsCancellationRequested)
            {
                var dataPackage = this.Context.CodePackageActivationContext.GetDataPackageObject("TestData");
                var text = File.ReadAllText(Path.Combine(dataPackage.Path, "data.txt"));
                ServiceEventSource.Current.ServiceMessage(this, text);
                await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
            }

Code Sample