Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with Azure staging crazy URL

Tags:

wcf

azure

i'm deploying a webrole in azure that contains a web-site and a wcf service...
The site consumes services from the wcf.
The problem here is that the staging deploy creates a crazy url for the endpoints and i have to keep changing the endpoints in the web.config...

I'm wondering if theres a way to either "predict" what the url will be or to force one or even point to a generic host such as "localhost"???

like image 261
Leonardo Avatar asked Nov 13 '22 10:11

Leonardo


1 Answers

You should be able to use role discovery to find the WCF endpoint. See this SO answer here and the blog post it links to.

My own abstract base class for connecting to azure services was based on that article. It uses role discovery to crate a channel like this:

    #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

And in a derived class I pass it the following variables (for example):

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

I never need to refer to the staging (or production) URLs at all.

See my answer here for more details: Add WCF reference within the same solution without adding a service reference

like image 72
Jude Fisher Avatar answered Nov 15 '22 07:11

Jude Fisher