【问题标题】:Dealing with Azure staging crazy URL处理 Azure 暂存疯狂 URL
【发布时间】:2013-02-12 00:36:56
【问题描述】:

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

我想知道是否有一种方法可以“预测” url 将是什么,或者强制一个甚至指向一个通用主机,例如“localhost”???

【问题讨论】:

标签: wcf azure


【解决方案1】:

您应该能够使用角色发现来查找 WCF 端点。请参阅此 SO 答案 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";

我根本不需要参考暂存(或生产)URL。

请在此处查看我的答案以获取更多详细信息:Add WCF reference within the same solution without adding a service reference

【讨论】:

    【解决方案2】:

    无法预测、控制 GUID 或使用某些常量名称。

    为了让事情更简单,您可以做的是将 URL 移动到 .CSCFG 并从 Azure 管理门户更新 WCF 服务的 URL

    【讨论】:

      猜你喜欢
      • 2013-12-18
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多