This issue occurs when your service's executable .exe
path is wrong in ServiceManifest.xml.
<CodePackage>
contains .exe
path in <Program>
just check it out first:
<CodePackage Name="Code" Version="1.0.0">
<EntryPoint>
<ExeHost>
<Program>MyServicePkgHost.exe</Program> // Important
<WorkingFolder>CodePackage</WorkingFolder>
</ExeHost>
</EntryPoint>
</CodePackage>
Important References:
For confirming you are not missing anything look at below two tutorials:
Edit 1:-
Your package layout of application directory structure should be:
PS D:\temp> tree /f .\MyServicePkg
D:\TEMP\MYSERVICEPKG
│ ApplicationManifest.xml
│
└───MyServicePkgManifest
│ ServiceManifest.xml
│
├───MyServicePkg
│ MyServicePkgHost.exe
│
├───MyServicePkgConfig
│ Settings.xml
│
└───MyServicePkgData
init.dat
Error:
Why the "Code is missing for service package" occurs?
Reason:
In above directory there is missing a .bat
code file from you application directory:
│
├───MyServicePkg
│ MyServicePkgHost.exe
│ MyServicePkgSetup.bat
|
So you have to add that missing file MyServicePkgSetup.bat
. And following is a simple service manifest ServiceManifest.xml example that shows the SetupEntryPoint
and the main EntryPoint
for the service.
<?xml version="1.0" encoding="utf-8" ?>
<ServiceManifest Name="MyServiceManifest" Version="SvcManifestVersion1" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Description>An example service manifest</Description>
<ServiceTypes>
<StatelessServiceType ServiceTypeName="MyServiceType" />
</ServiceTypes>
<CodePackage Name="Code" Version="1.0.0">
<SetupEntryPoint>
<ExeHost>
<Program>MyServicePkgSetup.bat</Program> // important
<WorkingFolder>CodePackage</WorkingFolder>
</ExeHost>
</SetupEntryPoint>
<EntryPoint>
<ExeHost>
<Program>MyServicePkgHost.exe</Program> // important
</ExeHost>
</EntryPoint>
</CodePackage>
<ConfigPackage Name="Config" Version="1.0.0" />
</ServiceManifest>
After you configure the service to have a setup entry point, you can change the security permissions that it runs under in the application manifest:
Under the <ServiceManifestImport>
section, configure a policy to apply this principal to <SetupEntryPoint>
. This tells Service Fabric that when the MyServicePkgSetup.bat
file is run, it should be RunAs with administrator privileges. Given that you have not applied a policy to the main entry point, the code in MyServicePkgHost.exe
runs under the system NetworkService account. This is the default account that all service entry points are run as.
<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="MyApplicationType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="MyServiceTypePkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<Policies>
<RunAsPolicy CodePackageRef="Code" UserRef="SetupAdminUser" EntryPointType="Setup" />
</Policies>
</ServiceManifestImport>
<Principals>
<Users>
<User Name="SetupAdminUser">
<MemberOf>
<SystemGroup Name="Administrators" />
</MemberOf>
</User>
</Users>
</Principals>
</ApplicationManifest>
PS: You can verify the package structure locally through PowerShell by using the Test-ServiceFabricApplicationPackage command.
Workaround to add missing file MyServicePkgSetup.bat
:
Let's now add the file MyServicePkgSetup.bat
to the Visual Studio project to test the administrator privileges. In Visual Studio, right-click the service project and add a new file called MyServicePkgSetup.bat
.
Next, ensure that the MyServicePkgSetup.bat
file is included in the service package. By default, it is not. Select the file, right-click to get the context menu, and choose Properties. In the Properties dialog box, ensure that Copy to Output Directory is set to Copy if newer. See the following screenshot:

**File Path: ** C:\..\YourApplication\
Now open the MyServicePkgSetup.bat
file and add the following commands:
REM Set a system environment variable. This requires administrator privilege
setx -m TestVariable "MyValue"
echo System TestVariable set to > out.txt
echo %TestVariable% >> out.txt
REM To delete this system variable us
REM REG delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v TestVariable /f
Next, build and deploy the solution to a local development cluster. After the service has started, as shown in Service Fabric Explorer, you can see that the MySetup.bat file was successful in a two ways. Open a PowerShell command prompt and type:
PS C:\ [Environment]::GetEnvironmentVariable("TestVariable","Machine")
MyValue
Then, note the name of the node where the service was deployed and started in Service Fabric Explorer--for example, Node 2. Next, navigate to the application instance work folder to find the out.txt file that shows the value of TestVariable. For example, if this service was deployed to Node 2, then you can go to this path for the MyApplicationType:
C:\SfDevCluster\Data\_App\Node.2\MyApplicationType_App\work\out.txt