2016-09-05 4 views
3

サービスファブリックアプリケーションとして実行されるステートレスサービスでWebJob SDKを使用したいと考えています。残念ながら、私はそれを正しく実行することができません。以下は、問題を再現するテストコードの一部です。 "ProcessMethod"は呼び出されません。トリガされた関数 "ProcessNotificationsInQueue"も実行されません(はい、キューに項目があります)。アプリケーションがまだ実行中であるにもかかわらず、アプリケーションの「正常性状態」は、サービスファブリックエクスプローラで「エラー」に設定されています。サービスファブリックアプリケーションで実行中にWebJob SDKが動作しない

DashboardConnectionStringとStorageConnectionStringの両方に正しい値が設定されています。

コンソールアプリケーションまたはWorkerRoleで動作しているときに、非常に似たようなコードでは問題はありません。

何か不足していますか?誰も既にWebアプリケーションをService Fabricアプリケーションで正常に使用していますか?

public sealed class TestStatelessService : StatelessService 
{ 
    public TestStatelessService(StatelessServiceContext context) 
     : base(context) 
    { } 

    /// <summary> 
    /// Optional override to create listeners (e.g., TCP, HTTP) for this service replica to handle client or user requests. 
    /// </summary> 
    /// <returns>A collection of listeners.</returns> 
    protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() 
    { 
     return new ServiceInstanceListener[0]; 
    } 

    /// <summary> 
    /// This is the main entry point for your service instance. 
    /// </summary> 
    /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param> 
    protected override async Task RunAsync(CancellationToken cancellationToken) 
    { 
     ConfigurationPackage configPackage = this.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config"); 
     KeyedCollection<string, ConfigurationProperty> parameters = configPackage.Settings.Sections["MyConfigSection"].Parameters; 

     JobHostConfiguration config = new JobHostConfiguration(); 
     config.DashboardConnectionString = parameters["AzureWebJobsDashboard"].Value; 
     config.StorageConnectionString = parameters["AzureWebJobsStorage"].Value; 
     config.Queues.BatchSize = 10; 
     config.Queues.MaxDequeueCount = 8; 
     config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(30); 
     var host = new JobHost(config); 
     host.CallAsync(typeof(TestStatelessService).GetMethod("ProcessMethod"), cancellationToken); 
     host.RunAndBlock(); 
    } 

    [NoAutomaticTrigger] 
    public async Task ProcessMethod(CancellationToken cancellationToken) 
    { 
     long iterations = 0; 
     while (true) 
     { 
      cancellationToken.ThrowIfCancellationRequested(); 

      ServiceEventSource.Current.ServiceMessage(this, "Working-{0}", ++iterations); 

      await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken); 
     } 
    } 

    [Timeout("00:03:00")] 
    public static void ProcessNotificationsInQueue([QueueTrigger("newnotificationqueue")] Notification notification) 
    { 
     //Do something 
    } 
} 

答えて

3

host.CallAsync(typeof演算(TestStatelessService).GetMethod( "ProcessMethod")、cancellationToken)

あなたがマークできるよう、TestStatelessServiceクラスが定義されたパラメータなしのコンストラクタを持っていないことを注意してください。 ProcessMethodは静的として機能します。

あなたの説明によると、このtutorialに続いてAzure Service Fabricアプリケーションを作成しました。コードに基づいて、私はService FabricアプリケーションでWebJob SDKを正常にテストしました。ここに私のコードサンプルがありますか、あなたの側で動作しているかどうかを調べてみてください。

TestStatelessService.cs

/// <summary> 
/// This is the main entry point for your service instance. 
/// </summary> 
/// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param> 
protected override async Task RunAsync(CancellationToken cancellationToken) 
{ 
    ConfigurationPackage configPackage = this.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config"); 
    KeyedCollection<string, ConfigurationProperty> parameters = configPackage.Settings.Sections["MyConfigSection"].Parameters; 

    JobHostConfiguration config = new JobHostConfiguration(); 
    config.DashboardConnectionString = parameters["AzureWebJobsDashboard"].Value; 
    config.StorageConnectionString = parameters["AzureWebJobsStorage"].Value; 
    config.Queues.BatchSize = 10; 
    config.Queues.MaxDequeueCount = 8; 
    config.Queues.MaxPollingInterval = TimeSpan.FromSeconds(30); 
    var host = new JobHost(config); 
    host.CallAsync(typeof(TestStatelessService).GetMethod("ProcessMethod"),cancellationToken); 
    host.RunAndBlock(); 
} 

[NoAutomaticTrigger] 
public static async Task ProcessMethod(CancellationToken cancellationToken) 
{ 
    long iterations = 0; 
    while (true) 
    { 
     cancellationToken.ThrowIfCancellationRequested(); 
     //log 
     Trace.TraceInformation(">>[{0}]ProcessMethod Working-{1}",DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss"),++iterations); 
     //sleep for 5s 
     await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken); 
    } 
} 

[Timeout("00:03:00")] 
public static void ProcessNotificationsInQueue([QueueTrigger("newnotificationqueue")] CloudQueueMessage notification) 
{ 
    Trace.TraceInformation(">ProcessNotificationsInQueue invoked with notification:{0}", notification.AsString); 
} 

結果

アプリケーションの「健康状態は」にもかかわらずサービスファブリックExplorerで「エラー」に設定されていますアプリケーションはまだ実行中です。

あなたの側のコードをデバッグし、詳細なエラーを見つけてください。

+0

ありがとう@Bruce。不足している静的なものがクラッシュを引き起こしていました。 ProcessMethodを静的としてマークした後、両方の関数が問題なく呼び出されます。 – TAM

+0

@ Bruce私は正確な解決策を試しましたが、私のコントロールはProcessMethod関数には行きません。 – Rakesh

+0

解決策が見つかりました。 publicとして宣言されたTestStatelessServiceクラスを持つ必要があります。デフォルトでは、作成されたサービスは内部クラスとしてクラスを提供します。 – Rakesh

関連する問題