Azure工作者角色中的外部HTTP端点可能吗?

时间:2011-02-28 11:13:46

标签: wcf azure azure-worker-roles

我正在尝试在辅助角色中的Azure上托管面向外部的WCF服务。

我有一个解决方案在本地运行非常好,但是当我尝试将其发布到Azure时,它会进入初始化/忙碌/停止循环。

我在互联网上发现的信息说不同:

http://www.theworkflowelement.com/2011/01/worker-role-service-hosting-faq.html(不可能)

http://code.msdn.microsoft.com/WCF-Azure-Worker-Role-on-b394df49(可能与黑客有关)

其他消息来源说这是可能的,但我没有代表发布两个以上的链接。

当我尝试发布它时,最后一个挂起。

任何人都知道如何做到这一点,或者是否真的不可能?以工作者角色托管它会非常好,所以我不必使用Web角色所需的svc和web.config混乱。

这是我正在使用的代码:

    [ServiceContract(Namespace = "")]
    public interface IMyService
    {
        [OperationContract]
        [WebGet]
        string Echo(string s);
    }

    public class MyService : IMyService
    {
        public string Echo(string s)
        {
            return "hey " + s;
        }
    }

    public class TestPasswordValidator : UserNamePasswordValidator
    {
        public override void Validate(string userName, string password)
        {
        }
    }

    private static void StartService()
    {
        var endpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["HttpsEndpoint"];
        var uri = new Uri(endpoint.Protocol + "://" + endpoint.IPEndpoint + "/myservice");
        var host = new ServiceHost(typeof(MyService), uri);

        host.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = UserNamePasswordValidationMode.Custom;
        host.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new TestPasswordValidator();

        var mexBehavior = new ServiceMetadataBehavior();
        mexBehavior.HttpsGetEnabled = true;
        mexBehavior.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
        host.Description.Behaviors.Add(mexBehavior);

        var soapBinding = new WSHttpBinding(SecurityMode.TransportWithMessageCredential);
        soapBinding.Security.Message.ClientCredentialType = MessageCredentialType.UserName;

        host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpsBinding(), "mex");
        host.AddServiceEndpoint(typeof(IMyService), soapBinding, "Soap");

        var restBinding = new WebHttpBinding(WebHttpSecurityMode.Transport);
        restBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

        var restEndpoint = host.AddServiceEndpoint(typeof(IMyService), restBinding, "");
        restEndpoint.Behaviors.Add(new WebHttpBehavior { HelpEnabled = true, DefaultOutgoingResponseFormat = WebMessageFormat.Json, AutomaticFormatSelectionEnabled = true, DefaultBodyStyle = WebMessageBodyStyle.WrappedRequest });

        host.Open();
    }

    public override void Run()
    {
        StartService();

        while (true)
        {
            Thread.Sleep(10000);
        }
    }

    public override bool OnStart()
    {
        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        // For information on handling configuration changes
        // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

        return base.OnStart();
    }

1 个答案:

答案 0 :(得分:10)

我弄清楚为什么会这样。工作者角色需要以提升的权限运行才能打开HTTP端口。但是,此设置在角色设置gui中不可用。我认为控制权限的gui show设置是完全信任/部分信任。我想我不知道那是做什么的。

正确的设置位于ServiceRole下的ServiceDefinition.csdef文件中。

<Runtime executionContext="elevated" />
相关问题