wcf - 处理 Azure 暂存疯狂 URL

标签 wcf azure

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

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

最佳答案

您应该能够使用角色发现来查找 WCF 端点。请参阅此答案 hereblog post它链接到。

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

    #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

关于wcf - 处理 Azure 暂存疯狂 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15091178/

相关文章:

c# - 接收 eBay SOAP 通知消息

asp.net - XMLHttpRequest 无法加载 [url] 预检响应具有无效的 HTTP 状态代码 400

javascript - 保留通过 JavaScript 客户端的身份验证?

Azure 应用程序网关 - 重写 url 路径(去除前缀)

azure - 部署逻辑应用 API 连接,提供不记名 token

c# - jQuery Datatables 使用 WCF 进行服务器端处理

c# - 关于将 WCF 托管为 Windows 服务的问题

azure - 将应用服务证书添加到 KeyVault 时出现错误 "The parameter keyVaultCsmId has an invalid value"

javascript - Azure Functions Http 触发器 - DocumentDB 输出集成 Microsoft.Azure.Documents.Client : Value cannot be null

azure - Bot Framework Emulator - 连接时间比平时更长