处理Azure登台疯狂的URL

时间:2013-02-26 14:08:55

标签: wcf azure

我正在azure中部署一个包含网站和wcf服务的webrole ...
该网站使用wcf提供的服务 这里的问题是,登台部署为端点创建了一个疯狂的URL,我必须不断更改web.config中的端点...

我想知道是否有办法“预测”网址是什么,或强制一个甚至指向通用主机,例如“localhost”???

2 个答案:

答案 0 :(得分:1)

您应该能够使用角色发现来查找WCF端点。请参阅此回答here以及它链接到的blog post

我自己的用于连接azure服务的抽象基类是基于该文章的。它使用角色发现来创建这样的频道:

    #region Channel
    protected String roleName;
    protected String serviceName;
    protected String endpointName;
    protected String protocol = @"http";

    protected EndpointAddress _endpointAddress;
    protected BasicHttpBinding httpBinding;
    protected NetTcpBinding tcpBinding;

    protected IChannelFactory channelFactory;
    protected T client;

    protected virtual AddressHeader[] addressHeaders
    {
        get
        {
            return null;
        }
    }

    protected virtual EndpointAddress endpointAddress
    {
        get
        {
            if (_endpointAddress == null)
            {
                var endpoints = RoleEnvironment.Roles[roleName].Instances.Select(i => i.InstanceEndpoints[endpointName]).ToArray();
                var endpointIP = endpoints.FirstOrDefault().IPEndpoint;
                if(addressHeaders != null)
                {
                    _endpointAddress = new EndpointAddress(new Uri(String.Format("{1}://{0}/{2}", endpointIP, protocol, serviceName)), addressHeaders);
                }
                else
                {
                    _endpointAddress = new EndpointAddress(String.Format("{1}://{0}/{2}", endpointIP, protocol, serviceName));
                }

            }
            return _endpointAddress;
        }
    }

    protected virtual Binding binding
    {
        get
        {
            switch (protocol)
            {
                case "tcp.ip":
                    if (tcpBinding == null) tcpBinding = new NetTcpBinding();
                    return tcpBinding;
                default:
                    //http
                    if (httpBinding == null) httpBinding = new BasicHttpBinding();
                    return httpBinding;
            }
        }
    }

    public virtual T Client
    {
        get
        {
            if (this.client == null)
            {
                this.channelFactory = new ChannelFactory<T>(binding, endpointAddress);
                this.client = ((ChannelFactory<T>)channelFactory).CreateChannel();
                ((IContextChannel)client).OperationTimeout = TimeSpan.FromMinutes(2);
                var scope = new OperationContextScope(((IContextChannel)client));
                addCustomMessageHeaders(scope);
            }
            return this.client; 
        }
    }
    #endregion

在派生类中,我传递了以下变量(例如):

this.roleName = "WebServiceRole";
this.endpointName = "HttpInternal";
this.serviceName = "services/Accounts.svc";

我根本不需要引用暂存(或生产)网址。

有关详情,请参阅我的回答:Add WCF reference within the same solution without adding a service reference

答案 1 :(得分:0)

无法预测GUID,控制它或使用某个常量名称。

为了简化操作,您可以做的是将URL移动到.CSCFG并​​从Azure管理门户更新WCF服务的URL