title: Deploy Service Fabric Application through VSTS release pipeline using Hosted Agent
body: <p>I have set up continuous integration using Hosted Agent for service fabric by following this document <a href="https://azure.microsoft.com/en-us/documentation/articles/service-fabric-set-up-continuous-integration/" rel="nofollow noreferrer">https://azure.microsoft.com/en-us/documentation/articles/service-fabric-set-up-continuous-integration/</a></p>
  
  <p>In Release pipeline after importing certificate I am getting the following error and deployment failing. I am not able to identify where the issue is
  <a href="https://i.stack.imgur.com/Afc7G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Afc7G.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <h2>[error]An error occurred during this operation.  Please check the trace logs for more details.</h2>
    
    <p>Finishing task: ServiceFabricDeploy </p>
    
    <h2>[error]System.Exception: Task ServiceFabricDeploy failed.</h2>
    
    <p>This caused the job to fail. Look at the logs for the task for more details. </p>
    
    <p>[error]   at Microsoft.TeamFoundation.DistributedTask.Worker.JobRunner.Run(IJobContext jobContext, IJobRequest job, IJobExtension jobExtension, CancellationTokenSource tokenSource)</p>
  </blockquote>
  
  <p>Under Deploy service fabric task it is showing the below error
  <a href="https://i.stack.imgur.com/bSCBZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bSCBZ.png" alt="enter image description here"></a></p>
  
  <blockquote>
    <p>Imported cluster client certificate with thumbprint 'A6B32E70CFE715F608A247C1ED94AB3D0164A58E'.</p>
    
    <p>Thumbprint Subject                                            </p>
    
    <p>A6B32E70CFE715F608A247C1ED94AB3D0164A58E  >CN=clusternamedns.eastus.cloudapp.azure.com                                     </p>
    
    <h2>[error]An error occurred during this operation.  Please check the trace logs for more details.</h2>
  </blockquote>
  
  <h2><strong>Update</strong></h2>
  
  <p>After setting system.debug to true in variables, I got the following log</p>
  
  <pre><code>    2016-08-03T05:44:31.6556865Z ##[debug]System.Fabric.FabricException: An error occurred during this operation.  Please check the trace logs for more details. ---&gt; System.Runtime.InteropServices.COMException: No credentials are available in the security package (Exception from HRESULT: 0x8009030E)
  
  2016-08-03T05:44:31.6566887Z ##[debug]   at System.Fabric.Interop.NativeClient.IFabricClientSettings2.SetSecurityCredentials(FABRIC_SECURITY_CREDENTIALS credentials)
  
  2016-08-03T05:44:31.6577063Z ##[debug]   at System.Fabric.FabricClient.SetSecurityCredentialsInternal(SecurityCredentials credentials)
  
  2016-08-03T05:44:31.6587072Z ##[debug]   at System.Fabric.Interop.Utility.WrapNativeSyncInvoke[TResult](Func`1 func, String functionTag, String functionArgs)
  
  2016-08-03T05:44:31.6597111Z ##[debug]   --- End of inner exception stack trace ---
  
  2016-08-03T05:44:31.6606871Z ##[debug]   at System.Fabric.Interop.Utility.RunInMTA[TResult](Func`1 func)
  
  2016-08-03T05:44:31.6647953Z ##[debug]   at System.Fabric.FabricClient.InitializeFabricClient(SecurityCredentials credentialArg, FabricClientSettings newSettings, String[] hostEndpointsArg)
  
  2016-08-03T05:44:31.6656886Z ##[debug]   at Microsoft.ServiceFabric.Powershell.ClusterConnection.FabricClientBuilder.Build()
  
  2016-08-03T05:44:31.6666879Z ##[debug]   at Microsoft.ServiceFabric.Powershell.ClusterConnection..ctor(FabricClientBuilder fabricClientBuilder, Boolean getMetadata)
  
  2016-08-03T05:44:31.6676869Z ##[debug]   at Microsoft.ServiceFabric.Powershell.ConnectCluster.ProcessRecord()
  
  2016-08-03T05:44:31.6770225Z ##[debug]Leaving C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\1.103.1\tasks\ServiceFabricDeploy\1.0.1\deploy.ps1.
  
  2016-08-03T05:44:31.6850322Z ##[debug]Caught exception from task script.
  
  2016-08-03T05:44:31.6890370Z ##[debug]Error record:
  
  2016-08-03T05:44:31.7380329Z ##[debug]Connect-ServiceFabricCluster : An error occurred during this operation.  Please check the trace logs for more details.
  
  2016-08-03T05:44:31.7390333Z ##[debug]At C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\1.103.1\tasks\ServiceFabricDeploy\1.0.1\deploy.ps1:73 char:12
  
  2016-08-03T05:44:31.7410325Z ##[debug]+     [void](Connect-ServiceFabricCluster @clusterConnectionParameters)
  
  2016-08-03T05:44:31.7420325Z ##[debug]+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  2016-08-03T05:44:31.7430323Z ##[debug]    + CategoryInfo          : InvalidOperation: (:) [Connect-ServiceFabricCluster], FabricException
  
  2016-08-03T05:44:31.7440363Z ##[debug]    + FullyQualifiedErrorId : CreateClusterConnectionErrorId,Microsoft.ServiceFabric.Powershell.ConnectCluster
  
  2016-08-03T05:44:31.7450426Z ##[debug] 
  
  2016-08-03T05:44:31.7470318Z ##[debug]Script stack trace:
  
  2016-08-03T05:44:31.7500512Z ##[debug]at &lt;ScriptBlock&gt;, C:\LR\MMS\Services\Mms\TaskAgentProvisioner\Tools\agents\1.103.1\tasks\ServiceFabricDeploy\1.0.1\deploy.ps1: line 73
  
  2016-08-03T05:44:31.7910331Z ##[debug]at &lt;ScriptBlock&gt;, &lt;No file&gt;: line 1
  
  2016-08-03T05:44:31.7920318Z ##[debug]at &lt;ScriptBlock&gt;, &lt;No file&gt;: line 22
  
  2016-08-03T05:44:31.7930364Z ##[debug]at &lt;ScriptBlock&gt;, &lt;No file&gt;: line 18
  
  2016-08-03T05:44:31.7940315Z ##[debug]at &lt;ScriptBlock&gt;, &lt;No file&gt;: line 1
  
  2016-08-03T05:44:31.7960349Z ##[debug]Exception:
  
  2016-08-03T05:44:31.8000522Z ##[debug]System.Fabric.FabricException: An error occurred during this operation.  Please check the trace logs for more details. ---&gt; System.Runtime.InteropServices.COMException: No credentials are available in the security package (Exception from HRESULT: 0x8009030E)
  
  2016-08-03T05:44:31.8010571Z ##[debug]   at System.Fabric.Interop.NativeClient.IFabricClientSettings2.SetSecurityCredentials(FABRIC_SECURITY_CREDENTIALS credentials)
  
  2016-08-03T05:44:31.8020684Z ##[debug]   at System.Fabric.FabricClient.SetSecurityCredentialsInternal(SecurityCredentials credentials)
  
  2016-08-03T05:44:31.8030335Z ##[debug]   at System.Fabric.Interop.Utility.WrapNativeSyncInvoke[TResult](Func`1 func, String functionTag, String functionArgs)
  
  2016-08-03T05:44:31.8040334Z ##[debug]   --- End of inner exception stack trace ---
  
  2016-08-03T05:44:31.8060326Z ##[debug]   at System.Fabric.Interop.Utility.RunInMTA[TResult](Func`1 func)
  
  2016-08-03T05:44:31.8070343Z ##[debug]   at System.Fabric.FabricClient.InitializeFabricClient(SecurityCredentials credentialArg, FabricClientSettings newSettings, String[] hostEndpointsArg)
  
  2016-08-03T05:44:31.8080330Z ##[debug]   at Microsoft.ServiceFabric.Powershell.ClusterConnection.FabricClientBuilder.Build()
  
  2016-08-03T05:44:31.8090325Z ##[debug]   at Microsoft.ServiceFabric.Powershell.ClusterConnection..ctor(FabricClientBuilder fabricClientBuilder, Boolean getMetadata)
  
  2016-08-03T05:44:31.8100358Z ##[debug]   at Microsoft.ServiceFabric.Powershell.ConnectCluster.ProcessRecord()
  
  2016-08-03T05:44:31.8340330Z ##[error]An error occurred during this operation.  Please check the trace logs for more details.
  </code></pre>