سؤال

I am not sure if I am approaching this correctly. I have a MVC controller action that I need to call periodically. I am trying to create a scheduler with Quartz.net and then I need to run that scheduler in my worker. How can I access my MVC controller action inside my worker role?

هل كانت مفيدة؟

المحلول

You could make an HTTP request to the action's route URL from the worker role.

using (var httpClinet = new HttpClient())
{
    var result = httpClient.DownloadString("http://www.site.com/path/to/action");
}

Update

...how I can get the current url inside worker role? Because the url would change depending on where it is being deployed...

One way would be to store the variable URL segments in your worker role's ConfigurationSettings.

<Role name="My.WorkerRole">
    ...
    <ConfigurationSettings>
        <Setting name="UrlScheme" value="https" />
        <Setting name="UrlHost" value="www.site.com" />
        <Setting name="UrlPath" value="path/to/action?param1=value1" />
    </ConfigurationSettings>
    ...
</Role>

using (var httpClinet = new HttpClient())
{
    var url = string.Format("{0}://{1}/{2}",
        RoleEnvironment.GetConfigurationSettingValue("UrlScheme"),
        RoleEnvironment.GetConfigurationSettingValue("UrlHost"),
        RoleEnvironment.GetConfigurationSettingValue("UrlPath"));
    var result = httpClient.DownloadString(url);
}

You can then change the values in your ServiceConfiguration.Cloud.cscfg file before deploying.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top