├── .gitignore ├── .vscode └── launch.json ├── LICENSE ├── README.md ├── SECURITY.md ├── generators ├── AddService │ ├── index.js │ └── templates │ │ └── dummyfile.txt ├── CoreCLRStatefulActor │ ├── index.js │ └── templates │ │ ├── interface │ │ ├── ActorInterface.cs │ │ └── project.csproj │ │ ├── main │ │ ├── build │ │ │ ├── build.cmd │ │ │ └── build.sh │ │ ├── common │ │ │ └── dotnet-include.sh │ │ └── deploy │ │ │ ├── deploy.ps1 │ │ │ ├── deploy.sh │ │ │ ├── un-deploy.ps1 │ │ │ ├── un-deploy.sh │ │ │ ├── upgrade.ps1 │ │ │ └── upgrade.sh │ │ ├── service │ │ ├── app │ │ │ └── appPackage │ │ │ │ ├── ApplicationManifest.xml │ │ │ │ └── servicePackage │ │ │ │ ├── Code │ │ │ │ └── entryPoint.sh │ │ │ │ ├── Config │ │ │ │ ├── Settings.xml │ │ │ │ └── _readme.txt │ │ │ │ ├── Data │ │ │ │ └── _readme.txt │ │ │ │ ├── ServiceManifest.xml │ │ │ │ └── ServiceManifest_Linux.xml │ │ └── class │ │ │ ├── ActorEventListener.cs │ │ │ ├── ActorEventSource.cs │ │ │ ├── ActorImpl.cs │ │ │ ├── Program.cs │ │ │ └── project.csproj │ │ └── testclient │ │ ├── class │ │ ├── Program.cs │ │ └── project.csproj │ │ └── testscripts │ │ ├── testclient.cmd │ │ └── testclient.sh ├── CoreCLRStatefulService │ ├── index.js │ └── templates │ │ ├── main │ │ ├── build │ │ │ ├── build.cmd │ │ │ └── build.sh │ │ ├── common │ │ │ └── dotnet-include.sh │ │ └── deploy │ │ │ ├── deploy.ps1 │ │ │ ├── deploy.sh │ │ │ ├── un-deploy.ps1 │ │ │ ├── un-deploy.sh │ │ │ ├── upgrade.ps1 │ │ │ └── upgrade.sh │ │ └── service │ │ ├── app │ │ └── appPackage │ │ │ ├── ApplicationManifest.xml │ │ │ └── servicePackage │ │ │ ├── Code │ │ │ └── entryPoint.sh │ │ │ ├── Config │ │ │ ├── Settings.xml │ │ │ └── _readme.txt │ │ │ ├── Data │ │ │ └── _readme.txt │ │ │ ├── ServiceManifest.xml │ │ │ └── ServiceManifest_Linux.xml │ │ └── class │ │ ├── Program.cs │ │ ├── ServiceEventSource.cs │ │ ├── ServiceImpl.cs │ │ └── project.csproj ├── CoreCLRStatelessService │ ├── .yo-rc.json │ ├── index.js │ └── templates │ │ ├── main │ │ ├── build │ │ │ ├── build.cmd │ │ │ └── build.sh │ │ ├── common │ │ │ └── dotnet-include.sh │ │ └── deploy │ │ │ ├── deploy.ps1 │ │ │ ├── deploy.sh │ │ │ ├── un-deploy.ps1 │ │ │ ├── un-deploy.sh │ │ │ ├── upgrade.ps1 │ │ │ └── upgrade.sh │ │ └── service │ │ ├── app │ │ └── appPackage │ │ │ ├── ApplicationManifest.xml │ │ │ └── servicePackage │ │ │ ├── Code │ │ │ ├── _readme.txt │ │ │ └── entryPoint.sh │ │ │ ├── Config │ │ │ ├── Settings.xml │ │ │ └── _readme.txt │ │ │ ├── Data │ │ │ └── _readme.txt │ │ │ ├── ServiceManifest.xml │ │ │ └── ServiceManifest_Linux.xml │ │ └── class │ │ ├── Program.cs │ │ ├── ServiceEventListener.cs │ │ ├── ServiceEventSource.cs │ │ ├── ServiceImpl.cs │ │ └── project.csproj ├── app │ ├── index.js │ └── templates │ │ └── dummyfile.txt └── utility.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "type": "node", 6 | "request": "launch", 7 | "name": "azuresfcsharp generator", 8 | "program": "/usr/local/bin/yo", 9 | "args": [ "azuresfcsharp" ], 10 | "cwd": "/tmp", 11 | "console": "integratedTerminal", 12 | "internalConsoleOptions": "neverOpen" 13 | } 14 | ] 15 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # generator-azuresfcsharp 3 | > Yeoman generator for Azure Service Fabric CSharp projects 4 | 5 | ## Installation 6 | 7 | First, install [Yeoman](http://yeoman.io) and generator-azuresfcharp using [npm](https://www.npmjs.com/) (we assume you have pre-installed [npm](http://www.npmjs.com) and [node.js](https://nodejs.org/)). 8 | 9 | ```bash 10 | npm install -g yo 11 | npm install -g generator-azuresfcsharp 12 | ``` 13 | The commands might ask for root access. Please run them with ```sudo```, if needed. 14 | 15 | 16 | Then generate your new project: 17 | 18 | ```bash 19 | yo azuresfcsharp 20 | ``` 21 | 22 | You can have a look at our [documentation](https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-create-your-first-linux-application-with-csharp) to understand how can you build and deploy the generated Service Fabric C# application 23 | 24 | 25 | ## Getting To Know Yeoman 26 | 27 | * Yeoman has a heart of gold. 28 | * Yeoman is a person with feelings and opinions, but is very easy to work with. 29 | * Yeoman can be too opinionated at times but is easily convinced not to be. 30 | * Feel free to [learn more about Yeoman](http://yeoman.io/). 31 | 32 | ## License 33 | 34 | MIT 35 | Copyright (c) Microsoft Corporation. All rights reserved. 36 | 37 | 38 | # Contributing 39 | 40 | This project welcomes contributions and suggestions. Most contributions require you to agree to a 41 | Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us 42 | the rights to use your contribution. For details, visit https://cla.microsoft.com. 43 | 44 | When you submit a pull request, a CLA-bot will automatically determine whether you need to provide 45 | a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions 46 | provided by the bot. You will only need to do this once across all repos using our CLA. 47 | 48 | This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). 49 | For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or 50 | contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. 51 | 52 | ## Steps to contribute 53 | 54 | Once you have figured out all the legalities above, you can follow the steps below - 55 | 56 | * Create a fork of this [repository](https://github.com/Azure/generator-azuresfcsharp) 57 | * Git clone the forked repository to your development box 58 | * Make the changes 59 | * You can update your local Yeo using ```npm link``` (or ```sudo npm link``` as required) at the project root-level 60 | * Create a new project with ```yo azuresfcsharp``` (ensure it picks Yeo node-module bits from your local changes) 61 | * Validate that changes are working as expected and not breaking anything regressively - following the steps mentioned in the [documentation](https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-create-your-first-linux-application-with-csharp) by creating, building and deploying the generated project on a Service Fabric cluster 62 | * Raise a pull request and share with us 63 | 64 | ## Debugging generator using vscode 65 | 66 | * Open the repository's root folder in VScode. 67 | * Run the command ```which yo``` and update the program's value in launch.json if it does not match with yours. 68 | * Press F5 to start debugging. 69 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Security 4 | 5 | Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/). 6 | 7 | If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below. 8 | 9 | ## Reporting Security Issues 10 | 11 | **Please do not report security vulnerabilities through public GitHub issues.** 12 | 13 | Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report). 14 | 15 | If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey). 16 | 17 | You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc). 18 | 19 | Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue: 20 | 21 | * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.) 22 | * Full paths of source file(s) related to the manifestation of the issue 23 | * The location of the affected source code (tag/branch/commit or direct URL) 24 | * Any special configuration required to reproduce the issue 25 | * Step-by-step instructions to reproduce the issue 26 | * Proof-of-concept or exploit code (if possible) 27 | * Impact of the issue, including how an attacker might exploit the issue 28 | 29 | This information will help us triage your report more quickly. 30 | 31 | If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs. 32 | 33 | ## Preferred Languages 34 | 35 | We prefer all communications to be in English. 36 | 37 | ## Policy 38 | 39 | Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd). 40 | 41 | 42 | -------------------------------------------------------------------------------- /generators/AddService/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var path = require('path') 4 | , generators = require('yeoman-generator') 5 | , yosay = require('yosay') 6 | 7 | var JavaGenerator = generators.Base.extend({ 8 | constructor: function () { 9 | generators.Base.apply(this, arguments); 10 | 11 | this.desc('Generate Service Fabric CSharp app template'); 12 | var chalk = require('chalk'); 13 | if (this.config.get('projName')) { 14 | console.log(chalk.green("Setting project name to", this.config.get('projName'))); 15 | } else { 16 | var err = chalk.red("Project name not found in .yo-rc.json. Exiting ..."); 17 | throw err; 18 | } 19 | }, 20 | 21 | prompting: function () { 22 | var done = this.async(); 23 | 24 | var prompts = [{ 25 | type: 'list' 26 | , name: 'frameworkType' 27 | , message: 'Choose a framework for your service' 28 | , default: this.config.get('frameworkType') 29 | , choices: ["Reliable Actor Service", "Reliable Stateless Service","Reliable Stateful Service"] 30 | }]; 31 | 32 | this.prompt(prompts, function (props) { 33 | this.props = props; 34 | this.config.set(props); 35 | 36 | done(); 37 | }.bind(this)); 38 | }, 39 | 40 | writing: function() { 41 | var libPath = "REPLACE_SFLIBSPATH"; 42 | var isAddNewService = true; 43 | if (this.props.frameworkType == "Reliable Actor Service") { 44 | this.composeWith('azuresfcsharp:CoreCLRStatefulActor', { 45 | options: { libPath: libPath, isAddNewService: isAddNewService } 46 | }); 47 | } else if (this.props.frameworkType == "Reliable Stateless Service") { 48 | this.composeWith('azuresfcsharp:CoreCLRStatelessService', { 49 | options: { libPath: libPath, isAddNewService: isAddNewService } 50 | }); 51 | } else if (this.props.frameworkType == "Reliable Stateful Service"){ 52 | this.composeWith('azuresfcsharp:CoreCLRStatefulService', { 53 | options: { libPath: libPath, isAddNewService: isAddNewService } 54 | }); 55 | } 56 | }, 57 | end: function () { 58 | this.config.save(); 59 | } 60 | }); 61 | 62 | module.exports = JavaGenerator; 63 | 64 | -------------------------------------------------------------------------------- /generators/AddService/templates/dummyfile.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/generator-azuresfcsharp/e05ea10ad9c36a1f89de47d185f0627351f61dc1/generators/AddService/templates/dummyfile.txt -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var path = require("path"), 4 | Generator = require("yeoman-generator"); 5 | 6 | var ClassGenerator = class extends Generator { 7 | constructor(args, opts) { 8 | super(args, opts); 9 | 10 | this.desc("Generate Stateful Actor application template"); 11 | this.option("libPath", { 12 | type: String, 13 | required: true 14 | }); 15 | this.option("isAddNewService", { 16 | type: Boolean, 17 | required: true 18 | }); 19 | this.libPath = this.options.libPath; 20 | this.isAddNewService = this.options.isAddNewService; 21 | } 22 | 23 | async prompting() { 24 | var utility = require("../utility"); 25 | var prompts = [ 26 | { 27 | type: "input", 28 | name: "actorFQN", 29 | message: "Enter the name of actor service : ", 30 | validate: function(input) { 31 | return input ? utility.validateFQN(input) : false; 32 | } 33 | } 34 | ]; 35 | 36 | await this.prompt(prompts).then(answers => { 37 | this.actorFQN = answers.actorFQN; 38 | var parts = this.actorFQN.split("."), 39 | name = parts.pop(); 40 | this.packageName = parts.join("."); 41 | this.dir = parts.join("/"); 42 | this.actorName = utility.capitalizeFirstLetter(name.trim()); 43 | if (!this.packageName) { 44 | this.packageName = "statefulactor"; 45 | this.actorFQN = "statefulactor." + this.actorFQN; 46 | this.dir = this.dir + "/statefulactor"; 47 | } 48 | }); 49 | } 50 | 51 | initializing() { 52 | this.props = this.config.getAll(); 53 | this.config.defaults({ 54 | author: "" 55 | }); 56 | } 57 | 58 | writing() { 59 | var serviceProjName = this.actorName + "Service"; 60 | var testClientProjName = this.actorName + "TestClient"; 61 | 62 | var actorInterfaceName = "I" + this.actorName; 63 | var interfaceProjName = this.actorName + ".Interfaces"; 64 | var actorInterfaceNamespace = this.actorName + ".Interfaces"; 65 | 66 | var appPackage = this.props.projName; 67 | var servicePackage = this.actorName + "Pkg"; 68 | var serviceName = this.actorName; 69 | var serviceTypeName = this.actorName + "Type"; 70 | var appTypeName = this.props.projName + "Type"; 71 | var appName = this.props.projName; 72 | 73 | var serviceJarName = this.actorName.toLowerCase(); 74 | var interfaceJarName = (this.actorName + "-interface").toLowerCase(); 75 | var testClientJarName = (this.actorName + "-test").toLowerCase(); 76 | 77 | var testClassName = this.actorName + "TestClient"; 78 | var serviceMainClass = this.actorName + "Service"; 79 | var endpoint = serviceName + "Endpoint"; 80 | var replicatorEndpoint = serviceName + "ReplicatorEndpoint"; 81 | var replicatorConfig = serviceName + "ReplicatorConfig"; 82 | var replicatorSecurityConfig = serviceName + "ReplicatorSecurityConfig"; 83 | var localStoreConfig = serviceName + "LocalStoreConfig"; 84 | 85 | var appPackagePath = 86 | this.isAddNewService == false 87 | ? path.join(this.props.projName, appPackage) 88 | : appPackage; 89 | var serviceSrcPath = path.join(this.props.projName, serviceProjName); 90 | var interfaceSrcPath = path.join(this.props.projName, interfaceProjName); 91 | var testClientSrcPath = path.join(this.props.projName, testClientProjName); 92 | appPackagePath = appName; 93 | 94 | var testProject = path.join( 95 | appPackage, 96 | "src", 97 | testClientSrcPath, 98 | testClientProjName + ".csproj" 99 | ); 100 | var interfaceProject = path.join( 101 | appPackage, 102 | "src", 103 | interfaceSrcPath, 104 | interfaceProjName + ".csproj" 105 | ); 106 | var serviceProject = path.join( 107 | appPackage, 108 | "src", 109 | serviceSrcPath, 110 | serviceProjName + ".csproj" 111 | ); 112 | var codePath = path.join( 113 | appPackage, 114 | appPackagePath, 115 | servicePackage, 116 | "Code" 117 | ); 118 | var testCodePath = path.join(appPackage, serviceProjName + "TestClient"); 119 | 120 | var is_Windows = process.platform == "win32"; 121 | var is_Linux = process.platform == "linux"; 122 | var is_mac = process.platform == "darwin"; 123 | 124 | var sdkScriptExtension; 125 | var buildScriptExtension; 126 | var serviceManifestFile; 127 | if (is_Windows) { 128 | sdkScriptExtension = ".ps1"; 129 | buildScriptExtension = ".cmd"; 130 | serviceManifestFile = "ServiceManifest.xml"; 131 | } else { 132 | sdkScriptExtension = ".sh"; 133 | buildScriptExtension = ".sh"; 134 | } 135 | if (is_Linux) serviceManifestFile = "ServiceManifest_Linux.xml"; 136 | if (is_mac) serviceManifestFile = "ServiceManifest.xml"; 137 | 138 | this.fs.copyTpl( 139 | this.templatePath( 140 | "service/app/appPackage/servicePackage/" + serviceManifestFile 141 | ), 142 | this.destinationPath( 143 | path.join( 144 | appPackage, 145 | appPackagePath, 146 | servicePackage, 147 | "ServiceManifest.xml" 148 | ) 149 | ), 150 | { 151 | servicePackage: servicePackage, 152 | serviceTypeName: serviceTypeName, 153 | serviceName: serviceName, 154 | serviceProjName: serviceProjName 155 | } 156 | ); 157 | if (this.isAddNewService == false) { 158 | this.fs.copyTpl( 159 | this.templatePath("service/app/appPackage/ApplicationManifest.xml"), 160 | this.destinationPath( 161 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml") 162 | ), 163 | { 164 | appTypeName: appTypeName, 165 | servicePackage: servicePackage, 166 | serviceName: serviceName, 167 | serviceTypeName: serviceTypeName 168 | } 169 | ); 170 | } else { 171 | var fs = require("fs"); 172 | var xml2js = require("xml2js"); 173 | var parser = new xml2js.Parser(); 174 | fs.readFile( 175 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml"), 176 | function(err, data) { 177 | parser.parseString(data, function(err, result) { 178 | if (err) { 179 | return console.log(err); 180 | } 181 | result["ApplicationManifest"]["ServiceManifestImport"][ 182 | result["ApplicationManifest"]["ServiceManifestImport"].length 183 | ] = { 184 | ServiceManifestRef: [ 185 | { 186 | $: { 187 | ServiceManifestName: servicePackage, 188 | ServiceManifestVersion: "1.0.0" 189 | } 190 | } 191 | ] 192 | }; 193 | result["ApplicationManifest"]["DefaultServices"][0]["Service"][ 194 | result["ApplicationManifest"]["DefaultServices"][0][ 195 | "Service" 196 | ].length 197 | ] = { 198 | $: { Name: serviceName }, 199 | StatefulService: [ 200 | { 201 | $: { 202 | ServiceTypeName: serviceTypeName, 203 | TargetReplicaSetSize: "3", 204 | MinReplicaSetSize: "2" 205 | }, 206 | UniformInt64Partition: [ 207 | { 208 | $: { 209 | PartitionCount: "1", 210 | LowKey: "-9223372036854775808", 211 | HighKey: "9223372036854775807" 212 | } 213 | } 214 | ] 215 | } 216 | ] 217 | }; 218 | var builder = new xml2js.Builder(); 219 | var xml = builder.buildObject(result); 220 | fs.writeFile( 221 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml"), 222 | xml, 223 | function(err) { 224 | if (err) { 225 | return console.log(err); 226 | } 227 | } 228 | ); 229 | }); 230 | } 231 | ); 232 | } 233 | 234 | if (is_Linux) { 235 | this.fs.copyTpl( 236 | this.templatePath( 237 | "service/app/appPackage/servicePackage/Code/entryPoint.sh" 238 | ), 239 | this.destinationPath( 240 | path.join( 241 | appPackage, 242 | appPackagePath, 243 | servicePackage, 244 | "Code", 245 | "entryPoint.sh" 246 | ) 247 | ), 248 | { 249 | serviceProjName: serviceProjName 250 | } 251 | ); 252 | this.fs.copyTpl( 253 | this.templatePath("main/common/dotnet-include.sh"), 254 | this.destinationPath( 255 | path.join( 256 | appPackage, 257 | appPackagePath, 258 | servicePackage, 259 | "Code", 260 | "dotnet-include.sh" 261 | ) 262 | ), 263 | {} 264 | ); 265 | } 266 | this.fs.copyTpl( 267 | this.templatePath( 268 | "service/app/appPackage/servicePackage/Config/Settings.xml" 269 | ), 270 | this.destinationPath( 271 | path.join( 272 | appPackage, 273 | appPackagePath, 274 | servicePackage, 275 | "Config", 276 | "Settings.xml" 277 | ) 278 | ), 279 | { 280 | serviceName: serviceName 281 | } 282 | ); 283 | this.fs.copyTpl( 284 | this.templatePath("interface/ActorInterface.cs"), 285 | this.destinationPath( 286 | path.join( 287 | appPackage, 288 | "src", 289 | interfaceSrcPath, 290 | "I" + this.actorName + ".cs" 291 | ) 292 | ), 293 | { 294 | actorInterfaceNamespace: this.actorName + ".Interfaces", 295 | actorInterfaceName: "I" + this.actorName, 296 | authorName: this.props.authorName 297 | } 298 | ); 299 | this.fs.copyTpl( 300 | this.templatePath("interface/project.csproj"), 301 | this.destinationPath( 302 | path.join( 303 | appPackage, 304 | "src", 305 | interfaceSrcPath, 306 | interfaceProjName + ".csproj" 307 | ) 308 | ), 309 | { 310 | actorName: this.actorName, 311 | authorName: this.props.authorName 312 | } 313 | ); 314 | this.fs.copyTpl( 315 | this.templatePath("service/class/ActorImpl.cs"), 316 | this.destinationPath( 317 | path.join(appPackage, "src", serviceSrcPath, this.actorName + ".cs") 318 | ), 319 | { 320 | actorInterfaceNamespace: this.actorName + ".Interfaces", 321 | actorInterfaceName: "I" + this.actorName, 322 | serviceName: serviceName, 323 | actorName: this.actorName, 324 | appName: appName 325 | } 326 | ); 327 | this.fs.copyTpl( 328 | this.templatePath("service/class/project.csproj"), 329 | this.destinationPath( 330 | path.join( 331 | appPackage, 332 | "src", 333 | serviceSrcPath, 334 | serviceProjName + ".csproj" 335 | ) 336 | ), 337 | { 338 | actorInterfaceNamespace: this.actorName + ".Interfaces", 339 | actorInterfaceName: "I" + this.actorName, 340 | actorName: this.actorName, 341 | authorName: this.props.authorName 342 | } 343 | ); 344 | this.fs.copyTpl( 345 | this.templatePath("service/class/Program.cs"), 346 | this.destinationPath( 347 | path.join(appPackage, "src", serviceSrcPath, "Program.cs") 348 | ), 349 | { 350 | actorName: this.actorName, 351 | authorName: this.props.authorName, 352 | appName: appName 353 | } 354 | ); 355 | this.fs.copyTpl( 356 | this.templatePath("service/class/ActorImpl.cs"), 357 | this.destinationPath( 358 | path.join(appPackage, "src", serviceSrcPath, this.actorName + ".cs") 359 | ), 360 | { 361 | actorInterfaceNamespace: this.actorName + ".Interfaces", 362 | actorInterfaceName: "I" + this.actorName, 363 | serviceName: serviceName, 364 | actorName: this.actorName, 365 | appName: appName 366 | } 367 | ); 368 | this.fs.copyTpl( 369 | this.templatePath("service/class/Program.cs"), 370 | this.destinationPath( 371 | path.join(appPackage, "src", serviceSrcPath, "Program.cs") 372 | ), 373 | { 374 | actorName: this.actorName, 375 | authorName: this.props.authorName, 376 | appName: appName 377 | } 378 | ); 379 | this.fs.copyTpl( 380 | this.templatePath("testclient/class/project.csproj"), 381 | this.destinationPath( 382 | path.join( 383 | appPackage, 384 | "src", 385 | testClientSrcPath, 386 | testClientProjName + ".csproj" 387 | ) 388 | ), 389 | { 390 | actorInterfaceNamespace: this.actorName + ".Interfaces", 391 | actorInterfaceName: "I" + this.actorName, 392 | authorName: this.props.authorName, 393 | actorName: this.actorName 394 | } 395 | ); 396 | this.fs.copyTpl( 397 | this.templatePath("testclient/class/Program.cs"), 398 | this.destinationPath( 399 | path.join(appPackage, "src", testClientSrcPath, "Program.cs") 400 | ), 401 | { 402 | actorInterfaceNamespace: this.actorName + ".Interfaces", 403 | actorInterfaceName: "I" + this.actorName, 404 | actorName: this.actorName, 405 | serviceName: serviceName, 406 | appName: appName 407 | } 408 | ); 409 | this.fs.copyTpl( 410 | this.templatePath("service/class/ActorEventListener.cs"), 411 | this.destinationPath( 412 | path.join(appPackage, "src", serviceSrcPath, "ActorEventListener.cs") 413 | ), 414 | { 415 | actorName: this.actorName, 416 | authorName: this.props.authorName, 417 | appName: appName 418 | } 419 | ); 420 | this.fs.copyTpl( 421 | this.templatePath("service/class/ActorEventSource.cs"), 422 | this.destinationPath( 423 | path.join(appPackage, "src", serviceSrcPath, "ActorEventSource.cs") 424 | ), 425 | { 426 | actorName: this.actorName, 427 | authorName: this.props.authorName, 428 | appName: appName 429 | } 430 | ); 431 | this.fs.copyTpl( 432 | this.templatePath( 433 | "testclient/testscripts/testclient" + buildScriptExtension 434 | ), 435 | this.destinationPath( 436 | path.join( 437 | appPackage, 438 | serviceProjName + "TestClient", 439 | "testclient" + buildScriptExtension 440 | ) 441 | ), 442 | { 443 | testClientProjName: testClientProjName, 444 | fabricCodePath: is_mac 445 | ? "/home/FabricDrop/bin/Fabric/Fabric.Code" 446 | : "/opt/microsoft/servicefabric/bin/Fabric/Fabric.Code/" 447 | } 448 | ); 449 | if (is_Linux) { 450 | this.fs.copyTpl( 451 | this.templatePath("main/common/dotnet-include.sh"), 452 | this.destinationPath( 453 | path.join( 454 | appPackage, 455 | serviceProjName + "TestClient", 456 | "dotnet-include.sh" 457 | ) 458 | ), 459 | {} 460 | ); 461 | } 462 | if (this.isAddNewService == false) { 463 | this.fs.copyTpl( 464 | this.templatePath("main/deploy/deploy" + sdkScriptExtension), 465 | this.destinationPath( 466 | path.join(appPackage, "install" + sdkScriptExtension) 467 | ), 468 | { 469 | appPackage: appPackage, 470 | appName: appName, 471 | appTypeName: appTypeName 472 | } 473 | ); 474 | } 475 | if (this.isAddNewService == false) { 476 | this.fs.copyTpl( 477 | this.templatePath("main/deploy/un-deploy" + sdkScriptExtension), 478 | this.destinationPath( 479 | path.join(appPackage, "uninstall" + sdkScriptExtension) 480 | ), 481 | { 482 | appPackage: appPackage, 483 | appName: appName, 484 | appTypeName: appTypeName 485 | } 486 | ); 487 | } 488 | if (this.isAddNewService == false) { 489 | this.fs.copyTpl( 490 | this.templatePath("main/deploy/upgrade" + sdkScriptExtension), 491 | this.destinationPath( 492 | path.join(appPackage, "upgrade" + sdkScriptExtension) 493 | ), 494 | { 495 | appPackage: appPackage, 496 | appName: appName, 497 | appTypeName: appTypeName 498 | } 499 | ); 500 | } 501 | if (this.isAddNewService == false) { 502 | this.fs.copyTpl( 503 | this.templatePath("main/build/build" + buildScriptExtension), 504 | this.destinationPath( 505 | path.join(appPackage, "build" + buildScriptExtension) 506 | ), 507 | { 508 | testProject: testProject, 509 | interfaceProject: interfaceProject, 510 | serviceProject: serviceProject, 511 | codePath: codePath, 512 | testCodePath: testCodePath 513 | } 514 | ); 515 | } else { 516 | var nodeFs = require("fs"); 517 | var appendToSettings = null; 518 | if (is_Linux || is_mac) { 519 | var appendToSettings = 520 | "\n\ 521 | \ndotnet restore $DIR/../" + 522 | interfaceProject + 523 | " -s https://api.nuget.org/v3/index.json \ 524 | \ndotnet build $DIR/../" + 525 | interfaceProject + 526 | " -v normal\n \n \ 527 | \ndotnet restore $DIR/../" + 528 | serviceProject + 529 | " -s https://api.nuget.org/v3/index.json \ 530 | \ndotnet build $DIR/../" + 531 | serviceProject + 532 | " -v normal\ 533 | \ndotnet publish $DIR/../" + 534 | serviceProject + 535 | " -o ../../../../" + 536 | codePath + 537 | "\n\n\ 538 | \ndotnet restore $DIR/../" + 539 | testProject + 540 | " -s https://api.nuget.org/v3/index.json \ 541 | \ndotnet build $DIR/../" + 542 | testProject + 543 | " -v normal\ 544 | \ncd " + 545 | "`" + 546 | "dirname $DIR/../" + 547 | testProject + 548 | "`" + 549 | "\ndotnet publish -o ../../../../" + 550 | appName + 551 | "/" + 552 | serviceProjName + 553 | "TestClient\ 554 | \ncd -"; 555 | } else if (is_Windows) { 556 | var appendToSettings = 557 | "\n\ 558 | \ndotnet restore %~dp0\\..\\" + 559 | interfaceProject + 560 | " -s https://api.nuget.org/v3/index.json \ 561 | \ndotnet build %~dp0\\..\\" + 562 | interfaceProject + 563 | " -v normal\n \n \ 564 | \ndotnet restore %~dp0\\..\\" + 565 | serviceProject + 566 | " -s https://api.nuget.org/v3/index.json \ 567 | \ndotnet build %~dp0\\..\\" + 568 | serviceProject + 569 | " -v normal\ 570 | \ndotnet publish %~dp0\\..\\" + 571 | serviceProject + 572 | " -o %dp0\\..\\" + 573 | codePath + 574 | "\n\n\ 575 | \ndotnet restore %~dp0\\..\\" + 576 | testProject + 577 | " -s https://api.nuget.org/v3/index.json \ 578 | \ndotnet build %~dp0\\..\\" + 579 | testProject + 580 | ' -v normal\ 581 | \nfor %%F in ("' + 582 | testProject + 583 | '") do cd %%~dpF\ 584 | \ndotnet publish -o %~dp0\\..\\' + 585 | appName + 586 | "\\" + 587 | serviceProjName + 588 | "TestClient\ 589 | \ncd %~dp0.."; 590 | } 591 | nodeFs.appendFile( 592 | path.join(appPackage, "build" + buildScriptExtension), 593 | appendToSettings, 594 | function(err) { 595 | if (err) { 596 | return console.log(err); 597 | } 598 | } 599 | ); 600 | } 601 | if (is_Linux) { 602 | if (this.isAddNewService == false) { 603 | this.fs.copyTpl( 604 | this.templatePath("main/common/dotnet-include.sh"), 605 | this.destinationPath(path.join(appPackage, "dotnet-include.sh")), 606 | {} 607 | ); 608 | } 609 | } 610 | 611 | this.fs.copyTpl( 612 | this.templatePath( 613 | "service/app/appPackage/servicePackage/Config/_readme.txt" 614 | ), 615 | this.destinationPath( 616 | path.join( 617 | appPackage, 618 | appPackagePath, 619 | servicePackage, 620 | "Config", 621 | "_readme.txt" 622 | ) 623 | ) 624 | ); 625 | 626 | this.fs.copyTpl( 627 | this.templatePath( 628 | "service/app/appPackage/servicePackage/Data/_readme.txt" 629 | ), 630 | this.destinationPath( 631 | path.join( 632 | appPackage, 633 | appPackagePath, 634 | servicePackage, 635 | "Data", 636 | "_readme.txt" 637 | ) 638 | ) 639 | ); 640 | } 641 | }; 642 | 643 | module.exports = ClassGenerator; 644 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/interface/ActorInterface.cs: -------------------------------------------------------------------------------- 1 | namespace <%= actorInterfaceNamespace %> 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading; 7 | using System.Threading.Tasks; 8 | using Microsoft.ServiceFabric.Actors; 9 | 10 | /// 11 | /// This interface defines the methods exposed by an actor. 12 | /// Clients use this interface to interact with the actor that implements it. 13 | /// 14 | public interface <%= actorInterfaceName %> : IActor 15 | { 16 | /// 17 | /// TODO: Replace with your own actor method. 18 | /// 19 | /// 20 | Task GetCountAsync(); 21 | 22 | /// 23 | /// TODO: Replace with your own actor method. 24 | /// 25 | /// 26 | /// 27 | Task SetCountAsync(int count); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/interface/project.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <%= actorName %>.Interfaces Class Library 5 | <%= authorName %> 6 | netstandard2.0 7 | <%= actorName %>.Interfaces 8 | <%= actorName %>.Interfaces 9 | 2.0.0 10 | $(PackageTargetFallback) 11 | x64 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/build/build.cmd: -------------------------------------------------------------------------------- 1 | dotnet restore %~dp0\..\<%= interfaceProject %> -s https://api.nuget.org/v3/index.json 2 | dotnet build %~dp0\..\<%= interfaceProject %> -v normal 3 | 4 | dotnet restore %~dp0\..\<%= serviceProject %> -s https://api.nuget.org/v3/index.json 5 | dotnet build %~dp0\..\<%= serviceProject %> -v normal 6 | for %%F in ("%~dp0\..\<%= serviceProject %>") do cd %%~dpF 7 | dotnet publish -o %~dp0\..\<%= codePath %> 8 | cd %~dp0\.. 9 | 10 | dotnet restore %~dp0\..\<%= testProject %> -s https://api.nuget.org/v3/index.json 11 | dotnet build %~dp0\..\<%= testProject %> -v normal 12 | for %%F in ("%~dp0\..\<%= testProject %>") do cd %%~dpF 13 | dotnet publish -o %~dp0\..\<%= testCodePath %> 14 | cd %~dp0\.. -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/build/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | DIR=`dirname $0` 3 | source $DIR/dotnet-include.sh 4 | 5 | dotnet restore $DIR/../<%= interfaceProject %> -s https://api.nuget.org/v3/index.json 6 | dotnet build $DIR/../<%= interfaceProject %> -v normal 7 | 8 | dotnet restore $DIR/../<%= serviceProject %> -s https://api.nuget.org/v3/index.json 9 | dotnet build $DIR/../<%= serviceProject %> -v normal 10 | cd `dirname $DIR/../<%= serviceProject %>` 11 | dotnet publish -o ../../../../<%= codePath %> 12 | cd - 13 | 14 | 15 | dotnet restore $DIR/../<%= testProject %> -s https://api.nuget.org/v3/index.json 16 | dotnet build $DIR/../<%= testProject %> -v normal 17 | cd `dirname $DIR/../<%= testProject %>` 18 | dotnet publish -o ../../../../<%= testCodePath %> 19 | cd - 20 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/common/dotnet-include.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ -f /etc/os-release ]]; then 4 | . /etc/os-release 5 | linuxDistrib=$ID 6 | if [ $linuxDistrib = "rhel" ]; then 7 | source scl_source enable rh-dotnet20 8 | exitCode=$? 9 | if [ $exitCode != 0 ]; then 10 | echo "Failed: source scl_source enable rh-dotnet20 : ExitCode: $exitCode" 11 | exit $exitCode 12 | fi 13 | fi 14 | fi -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/deploy/deploy.ps1: -------------------------------------------------------------------------------- 1 | function Uninstall() { 2 | Write-Host "Uninstalling App as installation failed... Please try installation again." 3 | Invoke-Expression "& $PSScriptRoot\uninstall.ps1" 4 | Exit 5 | } 6 | 7 | $AppPath = "$PSScriptRoot\<%= appPackage %>" 8 | Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $AppPath -ApplicationPackagePathInImageStore <%= appPackage %> -ShowProgress 9 | if (!$?) { 10 | Uninstall 11 | } 12 | 13 | Register-ServiceFabricApplicationType <%= appPackage %> 14 | if (!$?) { 15 | Uninstall 16 | } 17 | 18 | New-ServiceFabricApplication fabric:/<%= appName %> <%= appTypeName %> 1.0.0 19 | if (!$?) { 20 | Uninstall 21 | } -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/deploy/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | uninstall () { 3 | echo "Uninstalling App as installation failed... Please try installation again." 4 | ./uninstall.sh 5 | exit 6 | } 7 | 8 | cd `dirname $0` 9 | sfctl application upload --path <%= appPackage %> --show-progress 10 | if [ $? -ne 0 ]; then 11 | uninstall 12 | fi 13 | 14 | sfctl application provision --application-type-build-path <%= appPackage %> 15 | if [ $? -ne 0 ]; then 16 | uninstall 17 | fi 18 | 19 | sfctl application create --app-name fabric:/<%= appName %> --app-type <%= appTypeName %> --app-version 1.0.0 20 | if [ $? -ne 0 ]; then 21 | uninstall 22 | fi 23 | 24 | cd - -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/deploy/un-deploy.ps1: -------------------------------------------------------------------------------- 1 | 2 | Remove-ServiceFabricApplication fabric:/<%= appName %> 3 | Unregister-ServiceFabricApplicationType <%= appTypeName %> 1.0.0 4 | Remove-ServiceFabricApplicationPackage <%= appPackage %> -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/deploy/un-deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sfctl application delete --application-id <%= appName %> 4 | sfctl application unprovision --application-type-name <%= appTypeName %> --application-type-version 1.0.0 5 | sfctl store delete --content-path <%= appPackage %> -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/deploy/upgrade.ps1: -------------------------------------------------------------------------------- 1 | Param( 2 | [Parameter(Mandatory=$true)] 3 | [string]$version 4 | ) 5 | 6 | $AppPath = "$PSScriptRoot\<%= appPackage %>" 7 | Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $AppPath -ApplicationPackagePathInImageStore "<%= appPackage %>\$version" -ShowProgress 8 | Register-ServiceFabricApplicationType -ApplicationPathInImageStore "<%= appPackage %>\$version" 9 | Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/<%= appPackage %> -ApplicationTypeVersion $version -FailureAction Rollback -Monitored -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/main/deploy/upgrade.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd `dirname $0` 3 | sfctl application upload --path <%= appPackage %> --show-progress 4 | sfctl application provision --application-type-build-path <%= appPackage %> 5 | sfctl application upgrade --app-id fabric:/<%= appName %> --app-version $1 --parameters "{}" --mode Monitored 6 | cd - -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/app/appPackage/ApplicationManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/app/appPackage/servicePackage/Code/entryPoint.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | check_errs() 3 | { 4 | # Function. Parameter 1 is the return code 5 | if [ "${1}" -ne "0" ]; then 6 | # make our script exit with the right error code. 7 | exit ${1} 8 | fi 9 | } 10 | 11 | DIR=`dirname $0` 12 | echo 0x3f > /proc/self/coredump_filter 13 | source $DIR/dotnet-include.sh 14 | dotnet $DIR/<%= serviceProjName %>.dll $@ 15 | check_errs $? 16 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/app/appPackage/servicePackage/Config/Settings.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 |
4 | 5 | 6 |
7 |
8 | 9 |
10 |
-------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/app/appPackage/servicePackage/Config/_readme.txt: -------------------------------------------------------------------------------- 1 | contains a Settings.xml file, that can specify parameters for the service 2 | 3 | Configuration packages describe user-defined, application-overridable configuration settings (sections of key-value pairs) 4 | required for running service replicas/instances of service types specified in the ser-vice manifest. The configuration settings 5 | must be stored in Settings.xml in the config package folder. 6 | 7 | The service developer uses Service Fabric APIs to locate the package folders and read application-overridable configuration settings. 8 | The service developer can also register callbacks that are in-voked when any of the configuration packages specified in the 9 | service manifest are upgraded and re-reads new configuration settings inside that callback. 10 | 11 | This means that Service Fabric will not recycle EXEs and DLLHOSTs specified in the host and support packages when 12 | configuration packages are up-graded. -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/app/appPackage/servicePackage/Data/_readme.txt: -------------------------------------------------------------------------------- 1 | Data packages contain data files like custom dictionaries, 2 | non-overridable configuration files, custom initialized data files, etc. 3 | 4 | Service Fabric will recycle all EXEs and DLLHOSTs specified in the host and support packages when any of the data packages 5 | specified inside service manifest are upgraded. -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/app/appPackage/servicePackage/ServiceManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | dotnet 11 | <%= serviceProjName %>.dll 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/app/appPackage/servicePackage/ServiceManifest_Linux.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | entryPoint.sh 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/class/ActorEventListener.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------ 2 | // Copyright (c) Microsoft Corporation. All rights reserved. 3 | // Licensed under the MIT License (MIT). See License.txt in the repo root for license information. 4 | // ------------------------------------------------------------ 5 | 6 | namespace <%= actorName %> 7 | { 8 | using System; 9 | using System.IO; 10 | using System.Linq; 11 | using System.Diagnostics.Tracing; 12 | using System.Globalization; 13 | using System.Text; 14 | 15 | /// 16 | /// ActorEventListener is a class which listens to the eventsources registered and redirects the traces to a file 17 | /// Note that this class serves as a template to EventListener class and redirects the logs to /tmp/{appname_actorName_yyyyMMddHHmmssffff}. 18 | /// You can extend the functionality by writing your code to implement rolling logs for the logs written through this class. 19 | /// You can also write your custom listener class and handle the registered evestsources accordingly. 20 | /// 21 | internal class ActorEventListener : EventListener 22 | { 23 | private string directoryPath = Path.Combine(Path.GetTempPath(), "logs"); 24 | private string fileName = "<%= appName %>" + "_" + "<%= actorName %>" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".log"; 25 | 26 | /// 27 | /// We override this method to get a callback on every event we subscribed to with EnableEvents 28 | /// 29 | /// The event arguments that describe the event. 30 | protected override void OnEventWritten(EventWrittenEventArgs eventData) 31 | { 32 | string message = ""; 33 | 34 | if (eventData.Message != null) 35 | { 36 | message = string.Format(CultureInfo.InvariantCulture, eventData.Message, eventData.Payload.ToArray()); 37 | } 38 | 39 | Directory.CreateDirectory(directoryPath); 40 | using (StreamWriter writer = new StreamWriter( new FileStream(Path.Combine(directoryPath, fileName), FileMode.Append))) 41 | { 42 | // report all event information 43 | writer.WriteLine(Write(eventData.Task.ToString(), 44 | eventData.EventName, 45 | eventData.EventId.ToString(), 46 | eventData.Level, message)); 47 | } 48 | } 49 | 50 | private static string Write(string taskName, string eventName, string id, EventLevel level, string message) 51 | { 52 | StringBuilder output = new StringBuilder(); 53 | 54 | DateTime now = DateTime.UtcNow; 55 | output.Append(now.ToString("yyyy/MM/dd-HH:mm:ss.fff", CultureInfo.InvariantCulture)); 56 | output.Append(','); 57 | output.Append(level); 58 | output.Append(','); 59 | output.Append(taskName); 60 | 61 | if (!string.IsNullOrEmpty(eventName)) 62 | { 63 | output.Append('.'); 64 | output.Append(eventName); 65 | } 66 | 67 | if (!string.IsNullOrEmpty(id)) 68 | { 69 | output.Append('@'); 70 | output.Append(id); 71 | } 72 | 73 | if (!string.IsNullOrEmpty(message)) 74 | { 75 | output.Append(','); 76 | output.Append(message); 77 | } 78 | 79 | return output.ToString(); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/class/ActorEventSource.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------ 2 | // Copyright (c) Microsoft Corporation. All rights reserved. 3 | // Licensed under the MIT License (MIT). See License.txt in the repo root for license information. 4 | // ------------------------------------------------------------ 5 | 6 | namespace <%= actorName %> 7 | { 8 | using System.Diagnostics.Tracing; 9 | using System.Fabric; 10 | using Microsoft.ServiceFabric.Services.Runtime; 11 | 12 | /// 13 | /// Implements methods for logging service related events. 14 | /// 15 | public class ActorEventSource : EventSource 16 | { 17 | private const int MessageEventId = 1; 18 | private const int ActorHostInitializationFailedEventId = 3; 19 | 20 | public static ActorEventSource Current = new ActorEventSource(); 21 | 22 | // Define an instance method for each event you want to record and apply an [Event] attribute to it. 23 | // The method name is the name of the event. 24 | // Pass any parameters you want to record with the event (only primitive integer types, DateTime, Guid & string are allowed). 25 | // Each event method implementation should check whether the event source is enabled, and if it is, call WriteEvent() method to raise the event. 26 | // The number and types of arguments passed to every event method must exactly match what is passed to WriteEvent(). 27 | // Put [NonEvent] attribute on all methods that do not define an event. 28 | // For more information see https://msdn.microsoft.com/en-us/library/system.diagnostics.tracing.eventsource.aspx 29 | 30 | [NonEvent] 31 | public void Message(string message, params object[] args) 32 | { 33 | if (this.IsEnabled()) 34 | { 35 | string finalMessage = string.Format(message, args); 36 | this.Message(finalMessage); 37 | } 38 | } 39 | 40 | [Event(MessageEventId, Level = EventLevel.Informational, Message = "{0}")] 41 | public void Message(string message) 42 | { 43 | if (this.IsEnabled()) 44 | { 45 | this.WriteEvent(MessageEventId, message); 46 | } 47 | } 48 | 49 | [Event(ActorHostInitializationFailedEventId, Level = EventLevel.Error, Message = "Actor host initialization failed : {0}")] 50 | public void ActorHostInitializationFailed(string exception) 51 | { 52 | this.WriteEvent(ActorHostInitializationFailedEventId, exception); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/class/ActorImpl.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace <%= actorName %> 3 | { 4 | 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading; 9 | using System.Threading.Tasks; 10 | using Microsoft.ServiceFabric.Actors; 11 | using Microsoft.ServiceFabric.Actors.Runtime; 12 | using Microsoft.ServiceFabric.Actors.Client; 13 | using Interfaces; 14 | 15 | /// 16 | /// This class represents an actor. 17 | /// Every ActorID maps to an instance of this class. 18 | /// The StatePersistence attribute determines persistence and replication of actor state: 19 | /// - Persisted: State is written to disk and replicated. 20 | /// - Volatile: State is kept in memory only and replicated. 21 | /// - None: State is kept in memory only and not replicated. 22 | /// 23 | [ActorServiceAttribute(Name="<%= serviceName %>")] 24 | [StatePersistence(StatePersistence.Persisted)] 25 | internal class <%= actorName %> : Actor, <%= actorInterfaceName %> 26 | { 27 | /// 28 | /// This method is called whenever an actor is activated. 29 | /// An actor is activated the first time any of its methods are invoked. 30 | /// 31 | public <%= actorName %>(ActorService actorService, ActorId actorId) : base(actorService, actorId) 32 | { 33 | } 34 | 35 | protected override Task OnActivateAsync() 36 | { 37 | // The StateManager is this actor's private state store. 38 | // Data stored in the StateManager will be replicated for high-availability for actors that use volatile or persisted state storage. 39 | // Any serializable object can be saved in the StateManager. 40 | // For more information, see http://aka.ms/servicefabricactorsstateserialization 41 | return this.StateManager.TryAddStateAsync("count", 0); 42 | } 43 | 44 | /// 45 | /// TODO: Replace with your own actor method. 46 | /// 47 | /// 48 | Task <%= actorInterfaceName %>.GetCountAsync() 49 | { 50 | return this.StateManager.GetStateAsync("count"); 51 | } 52 | 53 | /// 54 | /// TODO: Replace with your own actor method. 55 | /// 56 | /// 57 | /// 58 | Task <%= actorInterfaceName %>.SetCountAsync(int count) 59 | { 60 | // Requests are not guaranteed to be processed in order nor at most once. 61 | // The update function here verifies that the incoming count is greater than the current count to preserve order. 62 | return this.StateManager.AddOrUpdateStateAsync("count", count, (key, value) => count > value ? count : value); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/class/Program.cs: -------------------------------------------------------------------------------- 1 | namespace <%= actorName %> 2 | { 3 | using System; 4 | using System.Diagnostics; 5 | using System.Diagnostics.Tracing; 6 | using System.Fabric; 7 | using System.Threading; 8 | using System.Threading.Tasks; 9 | using Microsoft.ServiceFabric.Actors.Runtime; 10 | 11 | internal static class Program 12 | { 13 | /// 14 | /// This is the entry point of the service host process. 15 | /// 16 | private static void Main() 17 | { 18 | try 19 | { 20 | //Creating a new event listener to redirect the traces to a file 21 | ActorEventListener listener = new ActorEventListener(); 22 | listener.EnableEvents(ActorEventSource.Current, EventLevel.LogAlways, EventKeywords.All); 23 | 24 | ActorEventSource.Current.Message("Registering Actor : {0}", "<%= actorName %>"); 25 | 26 | ActorRuntime.RegisterActorAsync<<%= actorName %>>( 27 | (context, actorType) => new ActorService(context, actorType)).GetAwaiter().GetResult(); 28 | 29 | ActorEventSource.Current.Message("Registered Actor : {0}", "<%= actorName %>"); 30 | 31 | Thread.Sleep(Timeout.Infinite); 32 | } 33 | catch (Exception ex) 34 | { 35 | ActorEventSource.Current.ActorHostInitializationFailed(ex.ToString()); 36 | throw; 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/service/class/project.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <%= actorName %> Application 5 | <%= authorName %> 6 | netcoreapp2.0 7 | <%= actorName %>Service 8 | Exe 9 | <%= actorName %>Service 10 | $(PackageTargetFallback) 11 | x64 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/testclient/class/Program.cs: -------------------------------------------------------------------------------- 1 | namespace <%= actorName %>Client 2 | { 3 | using System; 4 | using Microsoft.ServiceFabric.Actors; 5 | using Microsoft.ServiceFabric.Actors.Client; 6 | using <%= actorInterfaceNamespace %>; 7 | 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | var <%= actorName %>TestClient = ActorProxy.Create<<%= actorInterfaceName %>>(new ActorId(0x100), "fabric:/<%= appName %>" , "<%= serviceName %>"); 13 | int result = <%= actorName %>TestClient.GetCountAsync().Result; 14 | <%= actorName %>TestClient.SetCountAsync(result + 1).Wait(); 15 | Console.WriteLine("Value = {0}", result); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/testclient/class/project.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <%= actorName %> TestClient Application 5 | <%= authorName %> 6 | netcoreapp2.0 7 | <%= actorName %>TestClient 8 | Exe 9 | <%= actorName %>TestClient 10 | $(PackageTargetFallback) 11 | x64 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/testclient/testscripts/testclient.cmd: -------------------------------------------------------------------------------- 1 | dotnet %~dp0\<%= testClientProjName %>.dll 2 | exit /b %errorlevel% -------------------------------------------------------------------------------- /generators/CoreCLRStatefulActor/templates/testclient/testscripts/testclient.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | check_errs() 3 | { 4 | # Function. Parameter 1 is the return code 5 | if [ "${1}" -ne "0" ]; then 6 | # make our script exit with the right error code. 7 | exit ${1} 8 | fi 9 | } 10 | 11 | DIR=`dirname $0` 12 | export LD_LIBRARY_PATH=<%= fabricCodePath %> 13 | source $DIR/dotnet-include.sh 14 | dotnet $DIR/<%= testClientProjName %>.dll $@ 15 | check_errs $? 16 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var path = require("path"), 4 | Generator = require("yeoman-generator"); 5 | 6 | var ClassGenerator = class extends Generator { 7 | constructor(args, opts) { 8 | super(args, opts); 9 | 10 | this.desc("Generate Stateful Service application template"); 11 | this.option("libPath", { 12 | type: String, 13 | required: true 14 | }); 15 | this.option("isAddNewService", { 16 | type: Boolean, 17 | required: true 18 | }); 19 | this.libPath = this.options.libPath; 20 | this.isAddNewService = this.options.isAddNewService; 21 | } 22 | 23 | async prompting() { 24 | var utility = require("../utility"); 25 | var prompts = [ 26 | { 27 | type: "input", 28 | name: "serviceFQN", 29 | message: "Enter the name of service : ", 30 | validate: function(input) { 31 | return input ? utility.validateFQN(input) : false; 32 | } 33 | } 34 | ]; 35 | 36 | await this.prompt(prompts).then(answers => { 37 | this.serviceFQN = answers.serviceFQN; 38 | var parts = this.serviceFQN.split("."), 39 | name = parts.pop(); 40 | this.packageName = parts.join("."); 41 | this.dir = parts.join("/"); 42 | this.serviceName = utility.capitalizeFirstLetter(name.trim()); 43 | if (!this.packageName) { 44 | this.packageName = "statefulservice"; 45 | this.serviceFQN = "statefulservice." + this.serviceFQN; 46 | this.dir = this.dir + "/statefulservice"; 47 | } 48 | }); 49 | } 50 | 51 | initializing() { 52 | this.props = this.config.getAll(); 53 | this.config.defaults({ 54 | author: "" 55 | }); 56 | } 57 | 58 | writing() { 59 | var serviceProjName = this.serviceName; 60 | var appPackage = this.props.projName; 61 | var servicePackage = this.serviceName + "Pkg"; 62 | var serviceName = this.serviceName; 63 | var serviceTypeName = this.serviceName + "Type"; 64 | var appTypeName = this.props.projName + "Type"; 65 | var appName = this.props.projName; 66 | 67 | var serviceJarName = this.serviceName.toLowerCase(); 68 | 69 | var serviceMainClass = this.serviceName + "Service"; 70 | var endpoint = serviceName + "Endpoint"; 71 | var replicatorEndpoint = serviceName + "ReplicatorEndpoint"; 72 | var replicatorConfig = serviceName + "ReplicatorConfig"; 73 | var replicatorSecurityConfig = serviceName + "ReplicatorSecurityConfig"; 74 | var localStoreConfig = serviceName + "LocalStoreConfig"; 75 | 76 | var appPackagePath = 77 | this.isAddNewService == false 78 | ? path.join(this.props.projName, appPackage) 79 | : appPackage; 80 | var serviceSrcPath = path.join(this.props.projName, serviceProjName); 81 | appPackagePath = appName; 82 | 83 | var serviceProject = path.join( 84 | appPackage, 85 | "src", 86 | serviceSrcPath, 87 | serviceProjName + ".csproj" 88 | ); 89 | var codePath = path.join( 90 | appPackage, 91 | appPackagePath, 92 | servicePackage, 93 | "Code" 94 | ); 95 | 96 | var is_Windows = process.platform == "win32"; 97 | var is_Linux = process.platform == "linux"; 98 | var is_mac = process.platform == "darwin"; 99 | 100 | var sdkScriptExtension; 101 | var buildScriptExtension; 102 | var serviceManifestFile; 103 | if (is_Windows) { 104 | sdkScriptExtension = ".ps1"; 105 | buildScriptExtension = ".cmd"; 106 | serviceManifestFile = "ServiceManifest.xml"; 107 | } else { 108 | sdkScriptExtension = ".sh"; 109 | buildScriptExtension = ".sh"; 110 | } 111 | if (is_Linux) serviceManifestFile = "ServiceManifest_Linux.xml"; 112 | if (is_mac) serviceManifestFile = "ServiceManifest.xml"; 113 | 114 | this.fs.copyTpl( 115 | this.templatePath( 116 | "service/app/appPackage/servicePackage/" + serviceManifestFile 117 | ), 118 | this.destinationPath( 119 | path.join( 120 | appPackage, 121 | appPackagePath, 122 | servicePackage, 123 | "ServiceManifest.xml" 124 | ) 125 | ), 126 | { 127 | servicePackage: servicePackage, 128 | serviceTypeName: serviceTypeName, 129 | serviceName: serviceName, 130 | serviceProjName: serviceProjName 131 | } 132 | ); 133 | if (this.isAddNewService == false) { 134 | this.fs.copyTpl( 135 | this.templatePath("service/app/appPackage/ApplicationManifest.xml"), 136 | this.destinationPath( 137 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml") 138 | ), 139 | { 140 | appTypeName: appTypeName, 141 | servicePackage: servicePackage, 142 | serviceName: serviceName, 143 | serviceTypeName: serviceTypeName 144 | } 145 | ); 146 | } else { 147 | var fs = require("fs"); 148 | var xml2js = require("xml2js"); 149 | var parser = new xml2js.Parser(); 150 | fs.readFile( 151 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml"), 152 | function(err, data) { 153 | parser.parseString(data, function(err, result) { 154 | if (err) { 155 | return console.log(err); 156 | } 157 | result["ApplicationManifest"]["ServiceManifestImport"][ 158 | result["ApplicationManifest"]["ServiceManifestImport"].length 159 | ] = { 160 | ServiceManifestRef: [ 161 | { 162 | $: { 163 | ServiceManifestName: servicePackage, 164 | ServiceManifestVersion: "1.0.0" 165 | } 166 | } 167 | ] 168 | }; 169 | result["ApplicationManifest"]["DefaultServices"][0]["Service"][ 170 | result["ApplicationManifest"]["DefaultServices"][0][ 171 | "Service" 172 | ].length 173 | ] = { 174 | $: { Name: serviceName }, 175 | StatefulService: [ 176 | { 177 | $: { ServiceTypeName: serviceTypeName, InstanceCount: "1" }, 178 | SingletonPartition: [""] 179 | } 180 | ] 181 | }; 182 | 183 | var builder = new xml2js.Builder(); 184 | var xml = builder.buildObject(result); 185 | fs.writeFile( 186 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml"), 187 | xml, 188 | function(err) { 189 | if (err) { 190 | return console.log(err); 191 | } 192 | } 193 | ); 194 | }); 195 | } 196 | ); 197 | } 198 | if (is_Linux) { 199 | this.fs.copyTpl( 200 | this.templatePath( 201 | "service/app/appPackage/servicePackage/Code/entryPoint.sh" 202 | ), 203 | this.destinationPath( 204 | path.join( 205 | appPackage, 206 | appPackagePath, 207 | servicePackage, 208 | "Code", 209 | "entryPoint.sh" 210 | ) 211 | ), 212 | { 213 | serviceProjName: serviceProjName 214 | } 215 | ); 216 | this.fs.copyTpl( 217 | this.templatePath("main/common/dotnet-include.sh"), 218 | this.destinationPath( 219 | path.join( 220 | appPackage, 221 | appPackagePath, 222 | servicePackage, 223 | "Code", 224 | "dotnet-include.sh" 225 | ) 226 | ), 227 | {} 228 | ); 229 | } 230 | this.fs.copyTpl( 231 | this.templatePath( 232 | "service/app/appPackage/servicePackage/Config/Settings.xml" 233 | ), 234 | this.destinationPath( 235 | path.join( 236 | appPackage, 237 | appPackagePath, 238 | servicePackage, 239 | "Config", 240 | "Settings.xml" 241 | ) 242 | ), 243 | { 244 | serviceName: serviceName 245 | } 246 | ); 247 | 248 | this.fs.copyTpl( 249 | this.templatePath("service/class/ServiceImpl.cs"), 250 | this.destinationPath( 251 | path.join(appPackage, "src", serviceSrcPath, this.serviceName + ".cs") 252 | ), 253 | { 254 | serviceName: serviceName, 255 | serviceName: this.serviceName, 256 | appName: appName 257 | } 258 | ); 259 | this.fs.copyTpl( 260 | this.templatePath("service/class/project.csproj"), 261 | this.destinationPath( 262 | path.join( 263 | appPackage, 264 | "src", 265 | serviceSrcPath, 266 | this.serviceName + ".csproj" 267 | ) 268 | ), 269 | { 270 | serviceName: this.serviceName, 271 | authorName: this.props.authorName 272 | } 273 | ); 274 | this.fs.copyTpl( 275 | this.templatePath("service/class/Program.cs"), 276 | this.destinationPath( 277 | path.join(appPackage, "src", serviceSrcPath, "Program.cs") 278 | ), 279 | { 280 | serviceName: this.serviceName, 281 | authorName: this.props.authorName, 282 | appName: appName, 283 | serviceTypeName: serviceTypeName 284 | } 285 | ); 286 | this.fs.copyTpl( 287 | this.templatePath("service/class/ServiceEventSource.cs"), 288 | this.destinationPath( 289 | path.join(appPackage, "src", serviceSrcPath, "ServiceEventSource.cs") 290 | ), 291 | { 292 | serviceName: this.serviceName, 293 | authorName: this.props.authorName, 294 | appName: appName, 295 | serviceTypeName: serviceTypeName 296 | } 297 | ); 298 | 299 | if (this.isAddNewService == false) { 300 | this.fs.copyTpl( 301 | this.templatePath("main/deploy/deploy" + sdkScriptExtension), 302 | this.destinationPath( 303 | path.join(appPackage, "install" + sdkScriptExtension) 304 | ), 305 | { 306 | appPackage: appPackage, 307 | appName: appName, 308 | appTypeName: appTypeName 309 | } 310 | ); 311 | } 312 | 313 | if (this.isAddNewService == false) { 314 | this.fs.copyTpl( 315 | this.templatePath("main/deploy/un-deploy" + sdkScriptExtension), 316 | this.destinationPath( 317 | path.join(appPackage, "uninstall" + sdkScriptExtension) 318 | ), 319 | { 320 | appPackage: appPackage, 321 | appName: appName, 322 | appTypeName: appTypeName 323 | } 324 | ); 325 | } 326 | if (this.isAddNewService == false) { 327 | this.fs.copyTpl( 328 | this.templatePath("main/deploy/upgrade" + sdkScriptExtension), 329 | this.destinationPath( 330 | path.join(appPackage, "upgrade" + sdkScriptExtension) 331 | ), 332 | { 333 | appPackage: appPackage, 334 | appName: appName, 335 | appTypeName: appTypeName 336 | } 337 | ); 338 | } 339 | if (this.isAddNewService == false) { 340 | this.fs.copyTpl( 341 | this.templatePath("main/build/build" + buildScriptExtension), 342 | this.destinationPath( 343 | path.join(appPackage, "build" + buildScriptExtension) 344 | ), 345 | { 346 | serviceProject: serviceProject, 347 | codePath: codePath 348 | } 349 | ); 350 | } else { 351 | var nodeFs = require("fs"); 352 | var appendToSettings = null; 353 | if (is_Linux || is_mac) { 354 | var appendToSettings = 355 | "\n\ 356 | \ndotnet restore $DIR/../" + 357 | serviceProject + 358 | " -s https://api.nuget.org/v3/index.json \ 359 | \ndotnet build $DIR/../" + 360 | serviceProject + 361 | " -v normal\ 362 | \ncd " + 363 | "`" + 364 | "dirname $DIR/../" + 365 | serviceProject + 366 | "`" + 367 | "\ndotnet publish -o ../../../../" + 368 | appName + 369 | "/" + 370 | appName + 371 | "/" + 372 | servicePackage + 373 | "/Code\ 374 | \ncd -"; 375 | } else if (is_Windows) { 376 | var appendToSettings = 377 | "\n\ 378 | \ndotnet restore %~dp0\\..\\" + 379 | serviceProject + 380 | " -s https://api.nuget.org/v3/index.json \ 381 | \ndotnet build %~dp0\\..\\" + 382 | serviceProject + 383 | ' -v normal\ 384 | \nfor %%F in ("' + 385 | serviceProject + 386 | '") do cd %%~dpF\ 387 | \ndotnet publish -o %~dp0\\..\\' + 388 | appName + 389 | "\\" + 390 | appName + 391 | "\\" + 392 | servicePackage + 393 | "\\Code"; 394 | } 395 | nodeFs.appendFile( 396 | path.join(appPackage, "build" + buildScriptExtension), 397 | appendToSettings, 398 | function(err) { 399 | if (err) { 400 | return console.log(err); 401 | } 402 | } 403 | ); 404 | } 405 | if (is_Linux) { 406 | if (this.isAddNewService == false) { 407 | this.fs.copyTpl( 408 | this.templatePath("main/common/dotnet-include.sh"), 409 | this.destinationPath(path.join(appPackage, "dotnet-include.sh")), 410 | {} 411 | ); 412 | } 413 | } 414 | 415 | this.fs.copyTpl( 416 | this.templatePath("service/app/appPackage/servicePackage/Config/_readme.txt"), 417 | this.destinationPath( 418 | path.join( 419 | appPackage, 420 | appPackagePath, 421 | servicePackage, 422 | "Config", 423 | "_readme.txt" 424 | ) 425 | ) 426 | ); 427 | 428 | this.fs.copyTpl( 429 | this.templatePath("service/app/appPackage/servicePackage/Data/_readme.txt"), 430 | this.destinationPath( 431 | path.join( 432 | appPackage, 433 | appPackagePath, 434 | servicePackage, 435 | "Data", 436 | "_readme.txt" 437 | ) 438 | ) 439 | ); 440 | } 441 | }; 442 | 443 | module.exports = ClassGenerator; 444 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/build/build.cmd: -------------------------------------------------------------------------------- 1 | dotnet restore %~dp0\..\<%= serviceProject %> -s https://api.nuget.org/v3/index.json 2 | dotnet build %~dp0\..\<%= serviceProject %> -v normal 3 | 4 | for %%F in ("%~dp0\..\<%= serviceProject %>") do cd %%~dpF 5 | dotnet publish -o %~dp0\..\<%= codePath %> 6 | cd %~dp0\.. -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/build/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | DIR=`dirname $0` 3 | source $DIR/dotnet-include.sh 4 | 5 | dotnet restore $DIR/../<%= serviceProject %> -s https://api.nuget.org/v3/index.json 6 | dotnet build $DIR/../<%= serviceProject %> -v normal 7 | 8 | cd `dirname $DIR/../<%= serviceProject %>` 9 | dotnet publish -o ../../../../<%= codePath %> 10 | cd - 11 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/common/dotnet-include.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ -f /etc/os-release ]]; then 4 | . /etc/os-release 5 | linuxDistrib=$ID 6 | if [ $linuxDistrib = "rhel" ]; then 7 | source scl_source enable rh-dotnet20 8 | exitCode=$? 9 | if [ $exitCode != 0 ]; then 10 | echo "Failed: source scl_source enable rh-dotnet20 : ExitCode: $exitCode" 11 | exit $exitCode 12 | fi 13 | fi 14 | fi -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/deploy/deploy.ps1: -------------------------------------------------------------------------------- 1 | function Uninstall() { 2 | Write-Host "Uninstalling App as installation failed... Please try installation again." 3 | Invoke-Expression "& $PSScriptRoot\uninstall.ps1" 4 | Exit 5 | } 6 | 7 | $AppPath = "$PSScriptRoot\<%= appPackage %>" 8 | Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $AppPath -ApplicationPackagePathInImageStore <%= appPackage %> -ShowProgress 9 | if (!$?) { 10 | Uninstall 11 | } 12 | 13 | Register-ServiceFabricApplicationType <%= appPackage %> 14 | if (!$?) { 15 | Uninstall 16 | } 17 | 18 | New-ServiceFabricApplication fabric:/<%= appName %> <%= appTypeName %> 1.0.0 19 | if (!$?) { 20 | Uninstall 21 | } -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/deploy/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | uninstall () { 3 | echo "Uninstalling App as installation failed... Please try installation again." 4 | ./uninstall.sh 5 | exit 6 | } 7 | 8 | cd `dirname $0` 9 | sfctl application upload --path <%= appPackage %> --show-progress 10 | if [ $? -ne 0 ]; then 11 | uninstall 12 | fi 13 | 14 | sfctl application provision --application-type-build-path <%= appPackage %> 15 | if [ $? -ne 0 ]; then 16 | uninstall 17 | fi 18 | 19 | sfctl application create --app-name fabric:/<%= appName %> --app-type <%= appTypeName %> --app-version 1.0.0 20 | if [ $? -ne 0 ]; then 21 | uninstall 22 | fi 23 | 24 | cd - -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/deploy/un-deploy.ps1: -------------------------------------------------------------------------------- 1 | 2 | Remove-ServiceFabricApplication fabric:/<%= appName %> 3 | Unregister-ServiceFabricApplicationType <%= appTypeName %> 1.0.0 4 | Remove-ServiceFabricApplicationPackage <%= appPackage %> -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/deploy/un-deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -x 2 | 3 | sfctl application delete --application-id <%= appName %> 4 | sfctl application unprovision --application-type-name <%= appTypeName %> --application-type-version 1.0.0 5 | sfctl store delete --content-path <%= appPackage %> -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/deploy/upgrade.ps1: -------------------------------------------------------------------------------- 1 | Param( 2 | [Parameter(Mandatory=$true)] 3 | [string]$version 4 | ) 5 | 6 | $AppPath = "$PSScriptRoot\<%= appPackage %>" 7 | Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $AppPath -ApplicationPackagePathInImageStore "<%= appPackage %>\$version" -ShowProgress 8 | Register-ServiceFabricApplicationType -ApplicationPathInImageStore "<%= appPackage %>\$version" 9 | Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/<%= appPackage %> -ApplicationTypeVersion $version -FailureAction Rollback -Monitored -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/main/deploy/upgrade.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd `dirname $0` 3 | sfctl application upload --path <%= appPackage %> --show-progress 4 | sfctl application provision --application-type-build-path <%= appPackage %> 5 | sfctl application upgrade --app-id fabric:/<%= appName %> --app-version $1 --parameters "{}" --mode Monitored 6 | cd - -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/app/appPackage/ApplicationManifest.xml: -------------------------------------------------------------------------------- 1 |  2 | 7 | 8 | 9 | 10 | 11 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/app/appPackage/servicePackage/Code/entryPoint.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | check_errs() 3 | { 4 | # Function. Parameter 1 is the return code 5 | if [ "${1}" -ne "0" ]; then 6 | # make our script exit with the right error code. 7 | exit ${1} 8 | fi 9 | } 10 | 11 | DIR=`dirname $0` 12 | echo 0x3f > /proc/self/coredump_filter 13 | source $DIR/dotnet-include.sh 14 | dotnet $DIR/<%= serviceProjName %>.dll $@ 15 | check_errs $? 16 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/app/appPackage/servicePackage/Config/Settings.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 |
5 | 6 |
7 | 8 |
9 | 10 | 11 | 16 | 17 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/app/appPackage/servicePackage/Config/_readme.txt: -------------------------------------------------------------------------------- 1 | contains a Settings.xml file, that can specify parameters for the service 2 | 3 | Configuration packages describe user-defined, application-overridable configuration settings (sections of key-value pairs) 4 | required for running service replicas/instances of service types specified in the ser-vice manifest. The configuration settings 5 | must be stored in Settings.xml in the config package folder. 6 | 7 | The service developer uses Service Fabric APIs to locate the package folders and read application-overridable configuration settings. 8 | The service developer can also register callbacks that are in-voked when any of the configuration packages specified in the 9 | service manifest are upgraded and re-reads new configuration settings inside that callback. 10 | 11 | This means that Service Fabric will not recycle EXEs and DLLHOSTs specified in the host and support packages when 12 | configuration packages are up-graded. -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/app/appPackage/servicePackage/Data/_readme.txt: -------------------------------------------------------------------------------- 1 | Data packages contain data files like custom dictionaries, 2 | non-overridable configuration files, custom initialized data files, etc. 3 | 4 | Service Fabric will recycle all EXEs and DLLHOSTs specified in the host and support packages when any of the data packages 5 | specified inside service manifest are upgraded. -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/app/appPackage/servicePackage/ServiceManifest.xml: -------------------------------------------------------------------------------- 1 |  2 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | dotnet 18 | <%= serviceProjName %>.dll 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 32 | 33 | 34 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/app/appPackage/servicePackage/ServiceManifest_Linux.xml: -------------------------------------------------------------------------------- 1 |  2 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | entryPoint.sh 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | 31 | 32 | 33 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/class/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Microsoft.ServiceFabric.Services.Runtime; 6 | 7 | namespace <%= serviceName %> 8 | { 9 | internal static class Program 10 | { 11 | /// 12 | /// This is the entry point of the service host process. 13 | /// 14 | private static void Main() 15 | { 16 | try 17 | { 18 | // The ServiceManifest.XML file defines one or more service type names. 19 | // Registering a service maps a service type name to a .NET type. 20 | // When Service Fabric creates an instance of this service type, 21 | // an instance of the class is created in this host process. 22 | 23 | ServiceRuntime.RegisterServiceAsync("<%= serviceTypeName %>", 24 | context => new <%= serviceName %>(context)).GetAwaiter().GetResult(); 25 | 26 | // Prevents this host process from terminating so services keep running. 27 | Thread.Sleep(Timeout.Infinite); 28 | } 29 | catch (Exception) 30 | { 31 | throw; 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/class/ServiceEventSource.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Tracing; 4 | using System.Fabric; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using Microsoft.ServiceFabric.Services.Runtime; 9 | 10 | namespace <%= serviceName %> 11 | { 12 | [EventSource(Name = "MyCompany-ServiceCounterApplication-<%= serviceName %>")] 13 | internal sealed class ServiceEventSource : EventSource 14 | { 15 | public static readonly ServiceEventSource Current = new ServiceEventSource(); 16 | 17 | static ServiceEventSource() 18 | { 19 | // A workaround for the problem where ETW activities do not get tracked until Tasks infrastructure is initialized. 20 | // This problem will be fixed in .NET Framework 4.6.2. 21 | Task.Run(() => { }); 22 | } 23 | 24 | // Instance constructor is private to enforce singleton semantics 25 | private ServiceEventSource() : base() { } 26 | 27 | #region Keywords 28 | // Event keywords can be used to categorize events. 29 | // Each keyword is a bit flag. A single event can be associated with multiple keywords (via EventAttribute.Keywords property). 30 | // Keywords must be defined as a public class named 'Keywords' inside EventSource that uses them. 31 | public static class Keywords 32 | { 33 | public const EventKeywords Requests = (EventKeywords)0x1L; 34 | public const EventKeywords ServiceInitialization = (EventKeywords)0x2L; 35 | } 36 | #endregion 37 | 38 | #region Events 39 | // Define an instance method for each event you want to record and apply an [Event] attribute to it. 40 | // The method name is the name of the event. 41 | // Pass any parameters you want to record with the event (only primitive integer types, DateTime, Guid & string are allowed). 42 | // Each event method implementation should check whether the event source is enabled, and if it is, call WriteEvent() method to raise the event. 43 | // The number and types of arguments passed to every event method must exactly match what is passed to WriteEvent(). 44 | // Put [NonEvent] attribute on all methods that do not define an event. 45 | // For more information see https://msdn.microsoft.com/en-us/library/system.diagnostics.tracing.eventsource.aspx 46 | 47 | [NonEvent] 48 | public void Message(string message, params object[] args) 49 | { 50 | if (this.IsEnabled()) 51 | { 52 | string finalMessage = string.Format(message, args); 53 | Message(finalMessage); 54 | } 55 | } 56 | 57 | private const int MessageEventId = 1; 58 | [Event(MessageEventId, Level = EventLevel.Informational, Message = "{0}")] 59 | public void Message(string message) 60 | { 61 | if (this.IsEnabled()) 62 | { 63 | WriteEvent(MessageEventId, message); 64 | } 65 | } 66 | 67 | [NonEvent] 68 | public void ServiceMessage(StatefulServiceContext serviceContext, string message, params object[] args) 69 | { 70 | if (this.IsEnabled()) 71 | { 72 | string finalMessage = string.Format(message, args); 73 | ServiceMessage( 74 | serviceContext.ServiceName.ToString(), 75 | serviceContext.ServiceTypeName, 76 | serviceContext.ReplicaId, 77 | serviceContext.PartitionId, 78 | serviceContext.CodePackageActivationContext.ApplicationName, 79 | serviceContext.CodePackageActivationContext.ApplicationTypeName, 80 | serviceContext.NodeContext.NodeName, 81 | finalMessage); 82 | } 83 | } 84 | 85 | // For very high-frequency events it might be advantageous to raise events using WriteEventCore API. 86 | // This results in more efficient parameter handling, but requires explicit allocation of EventData structure and unsafe code. 87 | // To enable this code path, define UNSAFE conditional compilation symbol and turn on unsafe code support in project properties. 88 | private const int ServiceMessageEventId = 2; 89 | [Event(ServiceMessageEventId, Level = EventLevel.Informational, Message = "{7}")] 90 | private 91 | #if UNSAFE 92 | unsafe 93 | #endif 94 | void ServiceMessage( 95 | string serviceName, 96 | string serviceTypeName, 97 | long replicaOrInstanceId, 98 | Guid partitionId, 99 | string applicationName, 100 | string applicationTypeName, 101 | string nodeName, 102 | string message) 103 | { 104 | #if !UNSAFE 105 | WriteEvent(ServiceMessageEventId, serviceName, serviceTypeName, replicaOrInstanceId, partitionId, applicationName, applicationTypeName, nodeName, message); 106 | #else 107 | const int numArgs = 8; 108 | fixed (char* pServiceName = serviceName, pServiceTypeName = serviceTypeName, pApplicationName = applicationName, pApplicationTypeName = applicationTypeName, pNodeName = nodeName, pMessage = message) 109 | { 110 | EventData* eventData = stackalloc EventData[numArgs]; 111 | eventData[0] = new EventData { DataPointer = (IntPtr) pServiceName, Size = SizeInBytes(serviceName) }; 112 | eventData[1] = new EventData { DataPointer = (IntPtr) pServiceTypeName, Size = SizeInBytes(serviceTypeName) }; 113 | eventData[2] = new EventData { DataPointer = (IntPtr) (&replicaOrInstanceId), Size = sizeof(long) }; 114 | eventData[3] = new EventData { DataPointer = (IntPtr) (&partitionId), Size = sizeof(Guid) }; 115 | eventData[4] = new EventData { DataPointer = (IntPtr) pApplicationName, Size = SizeInBytes(applicationName) }; 116 | eventData[5] = new EventData { DataPointer = (IntPtr) pApplicationTypeName, Size = SizeInBytes(applicationTypeName) }; 117 | eventData[6] = new EventData { DataPointer = (IntPtr) pNodeName, Size = SizeInBytes(nodeName) }; 118 | eventData[7] = new EventData { DataPointer = (IntPtr) pMessage, Size = SizeInBytes(message) }; 119 | 120 | WriteEventCore(ServiceMessageEventId, numArgs, eventData); 121 | } 122 | #endif 123 | } 124 | 125 | private const int ServiceTypeRegisteredEventId = 3; 126 | [Event(ServiceTypeRegisteredEventId, Level = EventLevel.Informational, Message = "Service host process {0} registered service type {1}", Keywords = Keywords.ServiceInitialization)] 127 | public void ServiceTypeRegistered(int hostProcessId, string serviceType) 128 | { 129 | WriteEvent(ServiceTypeRegisteredEventId, hostProcessId, serviceType); 130 | } 131 | 132 | private const int ServiceHostInitializationFailedEventId = 4; 133 | [Event(ServiceHostInitializationFailedEventId, Level = EventLevel.Error, Message = "Service host initialization failed", Keywords = Keywords.ServiceInitialization)] 134 | public void ServiceHostInitializationFailed(string exception) 135 | { 136 | WriteEvent(ServiceHostInitializationFailedEventId, exception); 137 | } 138 | 139 | // A pair of events sharing the same name prefix with a "Start"/"Stop" suffix implicitly marks boundaries of an event tracing activity. 140 | // These activities can be automatically picked up by debugging and profiling tools, which can compute their execution time, child activities, 141 | // and other statistics. 142 | private const int ServiceRequestStartEventId = 5; 143 | [Event(ServiceRequestStartEventId, Level = EventLevel.Informational, Message = "Service request '{0}' started", Keywords = Keywords.Requests)] 144 | public void ServiceRequestStart(string requestTypeName) 145 | { 146 | WriteEvent(ServiceRequestStartEventId, requestTypeName); 147 | } 148 | 149 | private const int ServiceRequestStopEventId = 6; 150 | [Event(ServiceRequestStopEventId, Level = EventLevel.Informational, Message = "Service request '{0}' finished", Keywords = Keywords.Requests)] 151 | public void ServiceRequestStop(string requestTypeName, string exception = "") 152 | { 153 | WriteEvent(ServiceRequestStopEventId, requestTypeName, exception); 154 | } 155 | #endregion 156 | 157 | #region Private methods 158 | #if UNSAFE 159 | private int SizeInBytes(string s) 160 | { 161 | if (s == null) 162 | { 163 | return 0; 164 | } 165 | else 166 | { 167 | return (s.Length + 1) * sizeof(char); 168 | } 169 | } 170 | #endif 171 | #endregion 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/class/ServiceImpl.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Fabric; 4 | using System.Linq; 5 | using System.Threading; 6 | using System.Threading.Tasks; 7 | using Microsoft.ServiceFabric.Data.Collections; 8 | using Microsoft.ServiceFabric.Services.Communication.Runtime; 9 | using Microsoft.ServiceFabric.Services.Runtime; 10 | 11 | namespace <%= serviceName %> 12 | { 13 | /// 14 | /// An instance of this class is created for each service replica by the Service Fabric runtime. 15 | /// 16 | internal sealed class <%= serviceName %> : StatefulService 17 | { 18 | private string myName; 19 | private long myId; 20 | 21 | public <%= serviceName %>(StatefulServiceContext context) 22 | : base(context) 23 | { 24 | this.myName = context.CodePackageActivationContext.ApplicationName; 25 | this.myId = context.ReplicaOrInstanceId; 26 | } 27 | 28 | /// 29 | /// Optional override to create listeners (e.g., HTTP, Service Remoting, WCF, etc.) for this service replica to handle client or user requests. 30 | /// 31 | /// 32 | /// For more information on service communication, see https://aka.ms/servicefabricservicecommunication 33 | /// 34 | /// A collection of listeners. 35 | protected override IEnumerable CreateServiceReplicaListeners() 36 | { 37 | return new ServiceReplicaListener[0]; 38 | } 39 | 40 | /// 41 | /// This is the main entry point for your service replica. 42 | /// This method executes when this replica of your service becomes primary and has write status. 43 | /// 44 | /// Canceled when Service Fabric needs to shut down this service replica. 45 | protected override async Task RunAsync(CancellationToken cancellationToken) 46 | { 47 | // TODO: Replace the following sample code with your own logic 48 | // or remove this RunAsync override if it's not needed in your service. 49 | 50 | var myDictionary = await this.StateManager.GetOrAddAsync>("myDictionary"); 51 | 52 | while (true) 53 | { 54 | cancellationToken.ThrowIfCancellationRequested(); 55 | 56 | using (var tx = this.StateManager.CreateTransaction()) 57 | { 58 | var result = await myDictionary.TryGetValueAsync(tx, "Counter"); 59 | 60 | var counterValue = await myDictionary.GetOrAddAsync(tx, "Counter", 0); 61 | 62 | Console.WriteLine("Incrementing counter... {0} {1} : {2}", this.myName, this.myId, counterValue); 63 | ServiceEventSource.Current.Message("Incrementing counter from replica : " + this.myId + " : current value : " + counterValue); 64 | 65 | await myDictionary.AddOrUpdateAsync(tx, "Counter", 0, (key, value) => ++value); 66 | 67 | // If an exception is thrown before calling CommitAsync, the transaction aborts, all changes are 68 | // discarded, and nothing is saved to the secondary replicas. 69 | await tx.CommitAsync(); 70 | } 71 | 72 | await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); 73 | } 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /generators/CoreCLRStatefulService/templates/service/class/project.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Stateless Service Application 5 | <%= authorName %> 6 | Exe 7 | netcoreapp2.0 8 | <%= serviceName %> 9 | <%= serviceName %> 10 | $(PackageTargetFallback) 11 | x64 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/.yo-rc.json: -------------------------------------------------------------------------------- 1 | { 2 | "generator-azuresf": { 3 | "projName": "appname", 4 | "frameworkType": "Reliable Stateless Service - C#", 5 | "author": "" 6 | } 7 | } -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var path = require("path"), 4 | Generator = require("yeoman-generator"); 5 | 6 | var ClassGenerator = class extends Generator { 7 | constructor(args, opts) { 8 | super(args, opts); 9 | 10 | this.desc("Generate Stateless Service application template"); 11 | this.option("libPath", { 12 | type: String, 13 | required: true 14 | }); 15 | this.option("isAddNewService", { 16 | type: Boolean, 17 | required: true 18 | }); 19 | this.libPath = this.options.libPath; 20 | this.isAddNewService = this.options.isAddNewService; 21 | } 22 | 23 | async prompting() { 24 | var utility = require("../utility"); 25 | var prompts = [ 26 | { 27 | type: "input", 28 | name: "serviceFQN", 29 | message: "Enter the name of service : ", 30 | validate: function(input) { 31 | return input ? utility.validateFQN(input) : false; 32 | } 33 | } 34 | ]; 35 | 36 | await this.prompt(prompts).then(answers => { 37 | this.serviceFQN = answers.serviceFQN; 38 | var parts = this.serviceFQN.split("."), 39 | name = parts.pop(); 40 | this.packageName = parts.join("."); 41 | this.dir = parts.join("/"); 42 | this.serviceName = utility.capitalizeFirstLetter(name.trim()); 43 | if (!this.packageName) { 44 | this.packageName = "statelessservice"; 45 | this.serviceFQN = "statelessservice." + this.serviceFQN; 46 | this.dir = this.dir + "/statelessservice"; 47 | } 48 | }); 49 | } 50 | 51 | initializing() { 52 | this.props = this.config.getAll(); 53 | this.config.defaults({ 54 | author: "" 55 | }); 56 | } 57 | 58 | writing() { 59 | var serviceProjName = this.serviceName; 60 | var appPackage = this.props.projName; 61 | var servicePackage = this.serviceName + "Pkg"; 62 | var serviceName = this.serviceName; 63 | var serviceTypeName = this.serviceName + "Type"; 64 | var appTypeName = this.props.projName + "Type"; 65 | var appName = this.props.projName; 66 | 67 | var serviceJarName = this.serviceName.toLowerCase(); 68 | 69 | var serviceMainClass = this.serviceName + "Service"; 70 | var endpoint = serviceName + "Endpoint"; 71 | var replicatorEndpoint = serviceName + "ReplicatorEndpoint"; 72 | var replicatorConfig = serviceName + "ReplicatorConfig"; 73 | var replicatorSecurityConfig = serviceName + "ReplicatorSecurityConfig"; 74 | var localStoreConfig = serviceName + "LocalStoreConfig"; 75 | 76 | var appPackagePath = 77 | this.isAddNewService == false 78 | ? path.join(this.props.projName, appPackage) 79 | : appPackage; 80 | var serviceSrcPath = path.join(this.props.projName, serviceProjName); 81 | appPackagePath = appName; 82 | 83 | var serviceProject = path.join( 84 | appPackage, 85 | "src", 86 | serviceSrcPath, 87 | serviceProjName + ".csproj" 88 | ); 89 | var codePath = path.join( 90 | appPackage, 91 | appPackagePath, 92 | servicePackage, 93 | "Code" 94 | ); 95 | 96 | var is_Windows = process.platform == "win32"; 97 | var is_Linux = process.platform == "linux"; 98 | var is_mac = process.platform == "darwin"; 99 | 100 | var sdkScriptExtension; 101 | var buildScriptExtension; 102 | var serviceManifestFile; 103 | if (is_Windows) { 104 | sdkScriptExtension = ".ps1"; 105 | buildScriptExtension = ".cmd"; 106 | serviceManifestFile = "ServiceManifest.xml"; 107 | } else { 108 | sdkScriptExtension = ".sh"; 109 | buildScriptExtension = ".sh"; 110 | } 111 | if (is_Linux) serviceManifestFile = "ServiceManifest_Linux.xml"; 112 | if (is_mac) serviceManifestFile = "ServiceManifest.xml"; 113 | 114 | this.fs.copyTpl( 115 | this.templatePath( 116 | "service/app/appPackage/servicePackage/" + serviceManifestFile 117 | ), 118 | this.destinationPath( 119 | path.join( 120 | appPackage, 121 | appPackagePath, 122 | servicePackage, 123 | "ServiceManifest.xml" 124 | ) 125 | ), 126 | { 127 | servicePackage: servicePackage, 128 | serviceTypeName: serviceTypeName, 129 | serviceName: serviceName, 130 | serviceProjName: serviceProjName 131 | } 132 | ); 133 | if (this.isAddNewService == false) { 134 | this.fs.copyTpl( 135 | this.templatePath("service/app/appPackage/ApplicationManifest.xml"), 136 | this.destinationPath( 137 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml") 138 | ), 139 | { 140 | appTypeName: appTypeName, 141 | servicePackage: servicePackage, 142 | serviceName: serviceName, 143 | serviceTypeName: serviceTypeName 144 | } 145 | ); 146 | } else { 147 | var fs = require("fs"); 148 | var xml2js = require("xml2js"); 149 | var parser = new xml2js.Parser(); 150 | fs.readFile( 151 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml"), 152 | function(err, data) { 153 | parser.parseString(data, function(err, result) { 154 | if (err) { 155 | return console.log(err); 156 | } 157 | result["ApplicationManifest"]["ServiceManifestImport"][ 158 | result["ApplicationManifest"]["ServiceManifestImport"].length 159 | ] = { 160 | ServiceManifestRef: [ 161 | { 162 | $: { 163 | ServiceManifestName: servicePackage, 164 | ServiceManifestVersion: "1.0.0" 165 | } 166 | } 167 | ] 168 | }; 169 | result["ApplicationManifest"]["DefaultServices"][0]["Service"][ 170 | result["ApplicationManifest"]["DefaultServices"][0][ 171 | "Service" 172 | ].length 173 | ] = { 174 | $: { Name: serviceName }, 175 | StatelessService: [ 176 | { 177 | $: { ServiceTypeName: serviceTypeName, InstanceCount: "1" }, 178 | SingletonPartition: [""] 179 | } 180 | ] 181 | }; 182 | 183 | var builder = new xml2js.Builder(); 184 | var xml = builder.buildObject(result); 185 | fs.writeFile( 186 | path.join(appPackage, appPackagePath, "ApplicationManifest.xml"), 187 | xml, 188 | function(err) { 189 | if (err) { 190 | return console.log(err); 191 | } 192 | } 193 | ); 194 | }); 195 | } 196 | ); 197 | } 198 | 199 | if (this.isAddNewService == false) { 200 | this.fs.copyTpl( 201 | this.templatePath("main/deploy/deploy" + sdkScriptExtension), 202 | this.destinationPath( 203 | path.join(appPackage, "install" + sdkScriptExtension) 204 | ), 205 | { 206 | appPackage: appPackage, 207 | appName: appName, 208 | appTypeName: appTypeName 209 | } 210 | ); 211 | } 212 | if (this.isAddNewService == false) { 213 | this.fs.copyTpl( 214 | this.templatePath("main/deploy/un-deploy" + sdkScriptExtension), 215 | this.destinationPath( 216 | path.join(appPackage, "uninstall" + sdkScriptExtension) 217 | ), 218 | { 219 | appPackage: appPackage, 220 | appName: appName, 221 | appTypeName: appTypeName 222 | } 223 | ); 224 | } 225 | if (this.isAddNewService == false) { 226 | this.fs.copyTpl( 227 | this.templatePath("main/deploy/upgrade" + sdkScriptExtension), 228 | this.destinationPath( 229 | path.join(appPackage, "upgrade" + sdkScriptExtension) 230 | ), 231 | { 232 | appPackage: appPackage, 233 | appName: appName, 234 | appTypeName: appTypeName 235 | } 236 | ); 237 | } 238 | 239 | if (this.isAddNewService == false) { 240 | this.fs.copyTpl( 241 | this.templatePath("main/build/build" + buildScriptExtension), 242 | this.destinationPath( 243 | path.join(appPackage, "build" + buildScriptExtension) 244 | ), 245 | { 246 | serviceProject: serviceProject, 247 | codePath: codePath 248 | } 249 | ); 250 | } else { 251 | var nodeFs = require("fs"); 252 | var appendToSettings = null; 253 | if (is_Linux || is_mac) { 254 | var appendToSettings = 255 | "\n\ 256 | \ndotnet restore $DIR/../" + 257 | serviceProject + 258 | " -s https://api.nuget.org/v3/index.json \ 259 | \ndotnet build $DIR/../" + 260 | serviceProject + 261 | " -v normal\ 262 | \ncd " + 263 | "`" + 264 | "dirname $DIR/../" + 265 | serviceProject + 266 | "`" + 267 | "\ndotnet publish -o ../../../../" + 268 | appName + 269 | "/" + 270 | appName + 271 | "/" + 272 | servicePackage + 273 | "/Code\ 274 | \ncd -"; 275 | } else if (is_Windows) { 276 | var appendToSettings = 277 | "\n\ 278 | \ndotnet restore %~dp0\\..\\" + 279 | serviceProject + 280 | " -s https://api.nuget.org/v3/index.json \ 281 | \ndotnet build %~dp0\\..\\" + 282 | serviceProject + 283 | ' -v normal\ 284 | \nfor %%F in ("' + 285 | serviceProject + 286 | '") do cd %%~dpF\ 287 | \ndotnet publish -o %~dp0\\..\\' + 288 | appName + 289 | "\\" + 290 | appName + 291 | "\\" + 292 | servicePackage + 293 | "\\Code"; 294 | } 295 | nodeFs.appendFile( 296 | path.join(appPackage, "build" + buildScriptExtension), 297 | appendToSettings, 298 | function(err) { 299 | if (err) { 300 | return console.log(err); 301 | } 302 | } 303 | ); 304 | } 305 | if (is_Linux) { 306 | this.fs.copyTpl( 307 | this.templatePath("main/common/dotnet-include.sh"), 308 | this.destinationPath( 309 | path.join( 310 | appPackage, 311 | appPackagePath, 312 | servicePackage, 313 | "Code", 314 | "dotnet-include.sh" 315 | ) 316 | ), 317 | {} 318 | ); 319 | if (this.isAddNewService == false) { 320 | this.fs.copyTpl( 321 | this.templatePath("main/common/dotnet-include.sh"), 322 | this.destinationPath(path.join(appPackage, "dotnet-include.sh")), 323 | {} 324 | ); 325 | } 326 | this.fs.copyTpl( 327 | this.templatePath( 328 | "service/app/appPackage/servicePackage/Code/entryPoint.sh" 329 | ), 330 | this.destinationPath( 331 | path.join( 332 | appPackage, 333 | appPackagePath, 334 | servicePackage, 335 | "Code", 336 | "entryPoint.sh" 337 | ) 338 | ), 339 | { 340 | serviceProjName: serviceProjName 341 | } 342 | ); 343 | } 344 | 345 | this.fs.copyTpl( 346 | this.templatePath( 347 | "service/app/appPackage/servicePackage/Config/Settings.xml" 348 | ), 349 | this.destinationPath( 350 | path.join( 351 | appPackage, 352 | appPackagePath, 353 | servicePackage, 354 | "Config", 355 | "Settings.xml" 356 | ) 357 | ), 358 | { 359 | serviceName: serviceName 360 | } 361 | ); 362 | 363 | this.fs.copyTpl( 364 | this.templatePath("service/class/ServiceImpl.cs"), 365 | this.destinationPath( 366 | path.join(appPackage, "src", serviceSrcPath, this.serviceName + ".cs") 367 | ), 368 | { 369 | serviceName: serviceName, 370 | serviceName: this.serviceName, 371 | appName: appName 372 | } 373 | ); 374 | this.fs.copyTpl( 375 | this.templatePath("service/class/project.csproj"), 376 | this.destinationPath( 377 | path.join( 378 | appPackage, 379 | "src", 380 | serviceSrcPath, 381 | this.serviceName + ".csproj" 382 | ) 383 | ), 384 | { 385 | serviceName: this.serviceName, 386 | authorName: this.props.authorName 387 | } 388 | ); 389 | this.fs.copyTpl( 390 | this.templatePath("service/class/Program.cs"), 391 | this.destinationPath( 392 | path.join(appPackage, "src", serviceSrcPath, "Program.cs") 393 | ), 394 | { 395 | serviceName: this.serviceName, 396 | authorName: this.props.authorName, 397 | appName: appName, 398 | serviceTypeName: serviceTypeName 399 | } 400 | ); 401 | this.fs.copyTpl( 402 | this.templatePath("service/class/ServiceEventListener.cs"), 403 | this.destinationPath( 404 | path.join(appPackage, "src", serviceSrcPath, "ServiceEventListener.cs") 405 | ), 406 | { 407 | serviceName: this.serviceName, 408 | authorName: this.props.authorName, 409 | appName: appName, 410 | serviceTypeName: serviceTypeName 411 | } 412 | ); 413 | this.fs.copyTpl( 414 | this.templatePath("service/class/ServiceEventSource.cs"), 415 | this.destinationPath( 416 | path.join(appPackage, "src", serviceSrcPath, "ServiceEventSource.cs") 417 | ), 418 | { 419 | serviceName: this.serviceName, 420 | authorName: this.props.authorName, 421 | appName: appName, 422 | serviceTypeName: serviceTypeName 423 | } 424 | ); 425 | 426 | this.fs.copyTpl( 427 | this.templatePath("service/app/appPackage/servicePackage/Config/_readme.txt"), 428 | this.destinationPath( 429 | path.join( 430 | appPackage, 431 | appPackagePath, 432 | servicePackage, 433 | "Config", 434 | "_readme.txt" 435 | ) 436 | ) 437 | ); 438 | 439 | this.fs.copyTpl( 440 | this.templatePath("service/app/appPackage/servicePackage/Data/_readme.txt"), 441 | this.destinationPath( 442 | path.join( 443 | appPackage, 444 | appPackagePath, 445 | servicePackage, 446 | "Data", 447 | "_readme.txt" 448 | ) 449 | ) 450 | ); 451 | } 452 | }; 453 | 454 | module.exports = ClassGenerator; 455 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/build/build.cmd: -------------------------------------------------------------------------------- 1 | dotnet restore %~dp0\..\<%= serviceProject %> -s https://api.nuget.org/v3/index.json 2 | dotnet build %~dp0\..\<%= serviceProject %> -v normal 3 | 4 | for %%F in ("%~dp0\..\<%= serviceProject %>") do cd %%~dpF 5 | dotnet publish -o %~dp0\..\<%= codePath %> 6 | cd %~dp0\.. 7 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/build/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | DIR=`dirname $0` 3 | source $DIR/dotnet-include.sh 4 | 5 | dotnet restore $DIR/../<%= serviceProject %> -s https://api.nuget.org/v3/index.json 6 | dotnet build $DIR/../<%= serviceProject %> -v normal 7 | 8 | cd `dirname $DIR/../<%= serviceProject %>` 9 | dotnet publish -o ../../../../<%= codePath %> 10 | cd - 11 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/common/dotnet-include.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [[ -f /etc/os-release ]]; then 4 | . /etc/os-release 5 | linuxDistrib=$ID 6 | if [ $linuxDistrib = "rhel" ]; then 7 | source scl_source enable rh-dotnet20 8 | exitCode=$? 9 | if [ $exitCode != 0 ]; then 10 | echo "Failed: source scl_source enable rh-dotnet20 : ExitCode: $exitCode" 11 | exit $exitCode 12 | fi 13 | fi 14 | fi -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/deploy/deploy.ps1: -------------------------------------------------------------------------------- 1 | function Uninstall() { 2 | Write-Host "Uninstalling App as installation failed... Please try installation again." 3 | Invoke-Expression "& $PSScriptRoot\uninstall.ps1" 4 | Exit 5 | } 6 | 7 | $AppPath = "$PSScriptRoot\<%= appPackage %>" 8 | Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $AppPath -ApplicationPackagePathInImageStore <%= appPackage %> -ShowProgress 9 | if (!$?) { 10 | Uninstall 11 | } 12 | 13 | Register-ServiceFabricApplicationType <%= appPackage %> 14 | if (!$?) { 15 | Uninstall 16 | } 17 | 18 | New-ServiceFabricApplication fabric:/<%= appName %> <%= appTypeName %> 1.0.0 19 | if (!$?) { 20 | Uninstall 21 | } -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/deploy/deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | uninstall () { 3 | echo "Uninstalling App as installation failed... Please try installation again." 4 | ./uninstall.sh 5 | exit 6 | } 7 | 8 | cd `dirname $0` 9 | sfctl application upload --path <%= appPackage %> --show-progress 10 | if [ $? -ne 0 ]; then 11 | uninstall 12 | fi 13 | 14 | sfctl application provision --application-type-build-path <%= appPackage %> 15 | if [ $? -ne 0 ]; then 16 | uninstall 17 | fi 18 | 19 | sfctl application create --app-name fabric:/<%= appName %> --app-type <%= appTypeName %> --app-version 1.0.0 20 | if [ $? -ne 0 ]; then 21 | uninstall 22 | fi 23 | 24 | cd - -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/deploy/un-deploy.ps1: -------------------------------------------------------------------------------- 1 | 2 | Remove-ServiceFabricApplication fabric:/<%= appName %> 3 | Unregister-ServiceFabricApplicationType <%= appTypeName %> 1.0.0 4 | Remove-ServiceFabricApplicationPackage <%= appPackage %> -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/deploy/un-deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | 4 | sfctl application delete --application-id <%= appName %> 5 | sfctl application unprovision --application-type-name <%= appTypeName %> --application-type-version 1.0.0 6 | sfctl store delete --content-path <%= appPackage %> -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/deploy/upgrade.ps1: -------------------------------------------------------------------------------- 1 | Param( 2 | [Parameter(Mandatory=$true)] 3 | [string]$version 4 | ) 5 | 6 | $AppPath = "$PSScriptRoot\<%= appPackage %>" 7 | Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $AppPath -ApplicationPackagePathInImageStore "<%= appPackage %>\$version" -ShowProgress 8 | Register-ServiceFabricApplicationType -ApplicationPathInImageStore "<%= appPackage %>\$version" 9 | Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/<%= appPackage %> -ApplicationTypeVersion $version -FailureAction Rollback -Monitored -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/main/deploy/upgrade.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd `dirname $0` 3 | sfctl application upload --path <%= appPackage %> --show-progress 4 | sfctl application provision --application-type-build-path <%= appPackage %> 5 | sfctl application upgrade --app-id fabric:/<%= appName %> --app-version $1 --parameters "{}" --mode Monitored 6 | cd - -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/app/appPackage/ApplicationManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/app/appPackage/servicePackage/Code/_readme.txt: -------------------------------------------------------------------------------- 1 | ======================== 2 | BUILD OUTPUT DESCRIPTION 3 | ======================== 4 | 5 | To run the project from the command line, go to the dist folder and 6 | type the following: 7 | 8 | dotnet "StatelessHelloWorldService.dll" 9 | 10 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/app/appPackage/servicePackage/Code/entryPoint.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | check_errs() 3 | { 4 | # Function. Parameter 1 is the return code 5 | if [ "${1}" -ne "0" ]; then 6 | # make our script exit with the right error code. 7 | exit ${1} 8 | fi 9 | } 10 | 11 | DIR=`dirname $0` 12 | echo 0x3f > /proc/self/coredump_filter 13 | source $DIR/dotnet-include.sh 14 | dotnet $DIR/<%= serviceProjName %>.dll $@ 15 | check_errs $? 16 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/app/appPackage/servicePackage/Config/Settings.xml: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/app/appPackage/servicePackage/Config/_readme.txt: -------------------------------------------------------------------------------- 1 | contains a Settings.xml file, that can specify parameters for the service 2 | 3 | Configuration packages describe user-defined, application-overridable configuration settings (sections of key-value pairs) 4 | required for running service replicas/instances of service types specified in the ser-vice manifest. The configuration settings 5 | must be stored in Settings.xml in the config package folder. 6 | 7 | The service developer uses Service Fabric APIs to locate the package folders and read application-overridable configuration settings. 8 | The service developer can also register callbacks that are in-voked when any of the configuration packages specified in the 9 | service manifest are upgraded and re-reads new configuration settings inside that callback. 10 | 11 | This means that Service Fabric will not recycle EXEs and DLLHOSTs specified in the host and support packages when 12 | configuration packages are up-graded. -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/app/appPackage/servicePackage/Data/_readme.txt: -------------------------------------------------------------------------------- 1 | Data packages contain data files like custom dictionaries, 2 | non-overridable configuration files, custom initialized data files, etc. 3 | 4 | Service Fabric will recycle all EXEs and DLLHOSTs specified in the host and support packages when any of the data packages 5 | specified inside service manifest are upgraded. -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/app/appPackage/servicePackage/ServiceManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | dotnet 18 | <%= serviceProjName %>.dll 19 | 20 | 21 | 22 | 23 | 25 | 26 | 27 | 28 | 29 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/app/appPackage/servicePackage/ServiceManifest_Linux.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | entryPoint.sh 18 | 19 | 20 | 21 | 22 | 24 | 25 | 26 | 27 | 28 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/class/Program.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------ 2 | // Copyright (c) Microsoft Corporation. All rights reserved. 3 | // Licensed under the MIT License (MIT). See License.txt in the repo root for license information. 4 | // ------------------------------------------------------------ 5 | 6 | namespace <%= serviceName %> 7 | { 8 | using System; 9 | using System.Diagnostics.Tracing; 10 | using System.Fabric; 11 | using System.Threading; 12 | using System.Threading.Tasks; 13 | using Microsoft.ServiceFabric.Services.Runtime; 14 | 15 | /// 16 | /// This is the entrypoint for the host process that hosts the service. 17 | /// 18 | public static class Program 19 | { 20 | public static void Main(string[] args) 21 | { 22 | try 23 | { 24 | //Creating a new event listener to redirect the traces to a file 25 | ServiceEventListener listener = new ServiceEventListener(); 26 | listener.EnableEvents(ServiceEventSource.Current, EventLevel.LogAlways, EventKeywords.All); 27 | 28 | // The ServiceManifest.XML file defines one or more service type names. 29 | // Registering a service maps a service type name to a .NET type. 30 | // When Service Fabric creates an instance of this service type, 31 | // an instance of the class is created in this host process. 32 | ServiceEventSource.Current.Message("Registering Service : {0}", "<%= serviceName %>"); 33 | 34 | ServiceRuntime.RegisterServiceAsync("<%= serviceTypeName %>", 35 | context => new <%= serviceName %> (context)).GetAwaiter().GetResult(); 36 | 37 | ServiceEventSource.Current.Message("Registered Service : {0}", "<%= serviceName %>"); 38 | 39 | // Prevents this host process from terminating so services keep running. 40 | Thread.Sleep(Timeout.Infinite); 41 | } 42 | catch (Exception ex) 43 | { 44 | ServiceEventSource.Current.ServiceHostInitializationFailed(ex); 45 | throw ex; 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/class/ServiceEventListener.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------ 2 | // Copyright (c) Microsoft Corporation. All rights reserved. 3 | // Licensed under the MIT License (MIT). See License.txt in the repo root for license information. 4 | // ------------------------------------------------------------ 5 | 6 | namespace <%= serviceName %> 7 | { 8 | using System; 9 | using System.IO; 10 | using System.Linq; 11 | using System.Diagnostics.Tracing; 12 | using System.Globalization; 13 | using System.Text; 14 | 15 | /// 16 | /// ServiceEventListener is a class which listens to the eventsources registered and redirects the traces to a file 17 | /// Note that this class serves as a template to EventListener class and redirects the logs to /tmp/{appname_serviceName_yyyyMMddHHmmssffff}. 18 | /// You can extend the functionality by writing your code to implement rolling logs for the logs written through this class. 19 | /// You can also write your custom listener class and handle the registered evestsources accordingly. 20 | /// 21 | internal class ServiceEventListener : EventListener 22 | { 23 | private string directoryPath = Path.Combine(Path.GetTempPath(), "logs"); 24 | private string fileName = "<%= appName %>" + "_" + "<%= serviceName %>" + "_" + DateTime.Now.ToString("yyyyMMddHHmmssffff") + ".log"; 25 | 26 | /// 27 | /// We override this method to get a callback on every event we subscribed to with EnableEvents 28 | /// 29 | /// The event arguments that describe the event. 30 | protected override void OnEventWritten(EventWrittenEventArgs eventData) 31 | { 32 | string message = ""; 33 | 34 | if (eventData.Message != null) 35 | { 36 | message = string.Format(CultureInfo.InvariantCulture, eventData.Message, eventData.Payload.ToArray()); 37 | } 38 | 39 | Directory.CreateDirectory(directoryPath); 40 | using (StreamWriter writer = new StreamWriter( new FileStream(Path.Combine(directoryPath, fileName), FileMode.Append))) 41 | { 42 | // report all event information 43 | writer.WriteLine(Write(eventData.Task.ToString(), 44 | eventData.EventName, 45 | eventData.EventId.ToString(), 46 | eventData.Level, message)); 47 | } 48 | } 49 | 50 | private static string Write(string taskName, string eventName, string id, EventLevel level, string message) 51 | { 52 | StringBuilder output = new StringBuilder(); 53 | 54 | DateTime now = DateTime.UtcNow; 55 | output.Append(now.ToString("yyyy/MM/dd-HH:mm:ss.fff", CultureInfo.InvariantCulture)); 56 | output.Append(','); 57 | output.Append(level); 58 | output.Append(','); 59 | output.Append(taskName); 60 | 61 | if (!string.IsNullOrEmpty(eventName)) 62 | { 63 | output.Append('.'); 64 | output.Append(eventName); 65 | } 66 | 67 | if (!string.IsNullOrEmpty(id)) 68 | { 69 | output.Append('@'); 70 | output.Append(id); 71 | } 72 | 73 | if (!string.IsNullOrEmpty(message)) 74 | { 75 | output.Append(','); 76 | output.Append(message); 77 | } 78 | 79 | return output.ToString(); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/class/ServiceEventSource.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------ 2 | // Copyright (c) Microsoft Corporation. All rights reserved. 3 | // Licensed under the MIT License (MIT). See License.txt in the repo root for license information. 4 | // ------------------------------------------------------------ 5 | 6 | namespace <%= serviceName %> 7 | { 8 | using System; 9 | using System.Diagnostics.Tracing; 10 | using System.Fabric; 11 | using Microsoft.ServiceFabric.Services.Runtime; 12 | 13 | /// 14 | /// Implements methods for logging service related events. 15 | /// 16 | public class ServiceEventSource : EventSource 17 | { 18 | private const int MessageEventId = 1; 19 | private const int ServiceTypeRegisteredEventId = 3; 20 | private const int ServiceHostInitializationFailedEventId = 4; 21 | private const int ServiceWebHostBuilderFailedEventId = 5; 22 | 23 | public static ServiceEventSource Current = new ServiceEventSource(); 24 | 25 | // Define an instance method for each event you want to record and apply an [Event] attribute to it. 26 | // The method name is the name of the event. 27 | // Pass any parameters you want to record with the event (only primitive integer types, DateTime, Guid & string are allowed). 28 | // Each event method implementation should check whether the event source is enabled, and if it is, call WriteEvent() method to raise the event. 29 | // The number and types of arguments passed to every event method must exactly match what is passed to WriteEvent(). 30 | // Put [NonEvent] attribute on all methods that do not define an event. 31 | // For more information see https://msdn.microsoft.com/en-us/library/system.diagnostics.tracing.eventsource.aspx 32 | 33 | [NonEvent] 34 | public void Message(string message, params object[] args) 35 | { 36 | if (this.IsEnabled()) 37 | { 38 | string finalMessage = string.Format(message, args); 39 | this.Message(finalMessage); 40 | } 41 | } 42 | 43 | [Event(MessageEventId, Level = EventLevel.Informational, Message = "{0}")] 44 | public void Message(string message) 45 | { 46 | if (this.IsEnabled()) 47 | { 48 | this.WriteEvent(MessageEventId, message); 49 | } 50 | } 51 | 52 | [Event(ServiceTypeRegisteredEventId, Level = EventLevel.Informational, Message = "Service host process {0} registered service type {1}")] 53 | public void ServiceTypeRegistered(int hostProcessId, string serviceType) 54 | { 55 | this.WriteEvent(ServiceTypeRegisteredEventId, hostProcessId, serviceType); 56 | } 57 | 58 | [NonEvent] 59 | public void ServiceHostInitializationFailed(Exception e) 60 | { 61 | this.ServiceHostInitializationFailed(e.ToString()); 62 | } 63 | 64 | [Event(ServiceHostInitializationFailedEventId, Level = EventLevel.Error, Message = "Service host initialization failed: {0}")] 65 | private void ServiceHostInitializationFailed(string exception) 66 | { 67 | this.WriteEvent(ServiceHostInitializationFailedEventId, exception); 68 | } 69 | 70 | [NonEvent] 71 | public void ServiceWebHostBuilderFailed(Exception e) 72 | { 73 | this.ServiceWebHostBuilderFailed(e.ToString()); 74 | } 75 | 76 | [Event(ServiceWebHostBuilderFailedEventId, Level = EventLevel.Error, Message = "Service Owin Web Host Builder Failed: {0}")] 77 | private void ServiceWebHostBuilderFailed(string exception) 78 | { 79 | this.WriteEvent(ServiceWebHostBuilderFailedEventId, exception); 80 | } 81 | 82 | } 83 | } -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/class/ServiceImpl.cs: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------ 2 | // Copyright (c) Microsoft Corporation. All rights reserved. 3 | // Licensed under the MIT License (MIT). See License.txt in the repo root for license information. 4 | // ------------------------------------------------------------ 5 | 6 | namespace <%= serviceName %> 7 | { 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Fabric; 11 | using System.Linq; 12 | using System.Threading; 13 | using System.Threading.Tasks; 14 | using Microsoft.ServiceFabric.Services.Communication.Runtime; 15 | using Microsoft.ServiceFabric.Services.Runtime; 16 | 17 | /// 18 | /// An instance of this class is created for each service instance by the Service Fabric runtime. 19 | /// 20 | internal sealed class <%= serviceName %> : StatelessService 21 | { 22 | public <%= serviceName %>(StatelessServiceContext context) 23 | : base(context) 24 | { } 25 | 26 | /// 27 | /// Optional override to create listeners (e.g., TCP, HTTP) for this service replica to handle client or user requests. 28 | /// 29 | /// A collection of listeners. 30 | protected override IEnumerable CreateServiceInstanceListeners() 31 | { 32 | return new ServiceInstanceListener[0]; 33 | } 34 | 35 | /// 36 | /// This is the main entry point for your service instance. 37 | /// 38 | /// Canceled when Service Fabric needs to shut down this service instance. 39 | protected override async Task RunAsync(CancellationToken cancellationToken) 40 | { 41 | // TODO: Replace the following sample code with your own logic 42 | // or remove this RunAsync override if it's not needed in your service. 43 | 44 | while (true) 45 | { 46 | cancellationToken.ThrowIfCancellationRequested(); 47 | 48 | await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /generators/CoreCLRStatelessService/templates/service/class/project.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Stateless Service Application 5 | <%= authorName %> 6 | netcoreapp2.0 7 | <%= serviceName %> 8 | Exe 9 | <%= serviceName %> 10 | $(PackageTargetFallback) 11 | x64 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /generators/app/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | var path = require("path"), 4 | yosay = require("yosay"), 5 | Generator = require("yeoman-generator"); 6 | 7 | var isGuestUseCase = false; 8 | var JavaGenerator = class extends Generator { 9 | constructor(args, opts) { 10 | super(args, opts); 11 | 12 | this.desc("Generate Service Fabric CSharp app template"); 13 | } 14 | 15 | async prompting() { 16 | this.log(yosay("Welcome to Service Fabric generator for CSharp")); 17 | 18 | var prompts = [ 19 | { 20 | type: "input", 21 | name: "projName", 22 | message: "Name your application", 23 | default: this.config.get("projName"), 24 | validate: function(input) { 25 | return input ? true : false; 26 | } 27 | }, 28 | { 29 | type: "list", 30 | name: "frameworkType", 31 | message: "Choose a framework for your service", 32 | default: this.config.get("frameworkType"), 33 | choices: [ 34 | "Reliable Actor Service", 35 | "Reliable Stateless Service", 36 | "Reliable Stateful Service" 37 | ] 38 | } 39 | ]; 40 | 41 | await this.prompt(prompts).then(answers => { 42 | answers.projName = answers.projName.trim(); 43 | 44 | this.props = answers; 45 | this.config.set(answers); 46 | }); 47 | } 48 | 49 | writing() { 50 | var libPath = "REPLACE_SFLIBSPATH"; 51 | var isAddNewService = false; 52 | if (this.props.frameworkType == "Reliable Actor Service") { 53 | this.composeWith("azuresfcsharp:CoreCLRStatefulActor", { 54 | options: { libPath: libPath, isAddNewService: isAddNewService } 55 | }); 56 | } else if (this.props.frameworkType == "Reliable Stateless Service") { 57 | this.composeWith("azuresfcsharp:CoreCLRStatelessService", { 58 | options: { libPath: libPath, isAddNewService: isAddNewService } 59 | }); 60 | } else if (this.props.frameworkType == "Reliable Stateful Service") { 61 | this.composeWith("azuresfcsharp:CoreCLRStatefulService", { 62 | options: { libPath: libPath, isAddNewService: isAddNewService } 63 | }); 64 | } 65 | } 66 | 67 | end() { 68 | this.config.save(); 69 | if (this.isGuestUseCase == false) { 70 | //this is for Add Service 71 | var nodeFs = require("fs"); 72 | if ( 73 | nodeFs 74 | .statSync(path.join(this.destinationRoot(), ".yo-rc.json")) 75 | .isFile() 76 | ) { 77 | nodeFs 78 | .createReadStream(path.join(this.destinationRoot(), ".yo-rc.json")) 79 | .pipe( 80 | nodeFs.createWriteStream( 81 | path.join( 82 | this.destinationRoot(), 83 | this.props.projName, 84 | ".yo-rc.json" 85 | ) 86 | ) 87 | ); 88 | } 89 | } 90 | } 91 | }; 92 | 93 | module.exports = JavaGenerator; 94 | -------------------------------------------------------------------------------- /generators/app/templates/dummyfile.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/microsoft/generator-azuresfcsharp/e05ea10ad9c36a1f89de47d185f0627351f61dc1/generators/app/templates/dummyfile.txt -------------------------------------------------------------------------------- /generators/utility.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | validateFQN: function (input) { 3 | var chalk = require('chalk'); 4 | if(/^(([a-z]|_[a-z0-9_])[a-z0-9_]*(\.([a-z]|_[a-z0-9_])[a-z0-9_]*)*)$/i.test(input)) { 5 | return true; 6 | } 7 | var err = chalk.red("Invalid FQN! Please enter a valid class FQN"); 8 | throw new Error(err); 9 | }, 10 | capitalizeFirstLetter: function (input) { 11 | return input.charAt(0).toUpperCase() + input.slice(1); 12 | }, 13 | uncapitalizeFirstLetter: function (input) { 14 | return input.charAt(0).toLowerCase() + input.slice(1); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "generator-azuresfcsharp", 3 | "version": "1.1.0", 4 | "description": "Azure Service Fabric CSharp app template generator", 5 | "homepage": "https://azure.microsoft.com/en-in/services/service-fabric/", 6 | "authors": { 7 | "name": "Microsoft , Service Fabric", 8 | "url": "https://github.com/Azure/generator-azuresfcsharp" 9 | }, 10 | "files": [ 11 | "generators" 12 | ], 13 | "main": "generators/index.js", 14 | "keywords": [ 15 | "Azure", 16 | "Service Fabric", 17 | "yeoman-generator" 18 | ], 19 | "dependencies": { 20 | "chalk": "^3.0.0", 21 | "xml2js": "^0.4.23", 22 | "yeoman-generator": "^4.5.0", 23 | "yosay": "^2.0.2" 24 | }, 25 | "repository": "https://github.com/Azure/generator-azuresfcsharp", 26 | "license": "MIT" 27 | } 28 | --------------------------------------------------------------------------------