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

bif*_*fen 4 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(可能与hack)

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

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

有人知道怎么做,或者真的不可能吗?以工作者角色托管它会非常好,所以我不必使用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();
    }
Run Code Online (Sandbox Code Playgroud)

bif*_*fen 10

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

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

<Runtime executionContext="elevated" />
Run Code Online (Sandbox Code Playgroud)