c# - Reusing connections in Azure Relay Bus -
we're looking connecting azure websites on-premises wcf services. want use azure relay bus this.
there cost involved in setting tcp channel first time, 3 seconds in our case. understood. cache channel, next call goes faster. ( 250ms ) channel cached in threadlocal storage.
when multiple users using website, every new thread used, new instance of channel created, , have pay price again setting tcp channel.
my question are:
how prevent real user confronted delay of setting channel? normal practice have tcp channel each client thread?
note: on-premises use iis auto-start or nt services 'warm up' our wcf services, way go website running on azure? fact each thread has own channel doesn't make easier.
code shown below.
public static class nettcprelayclient<tchannel> { /// <summary> /// threadlocal instance of channel, each thread can have open tcp channel /// </summary> private static threadlocal<tchannel> staticchannel = new threadlocal<tchannel>(); /// <summary> /// shared instance of channelfactory /// </summary> private static channelfactory<tchannel> cf = null; /// <summary> /// creates channel. /// </summary> /// <returns>the created channel</returns> private static tchannel createchannel() { // url , access parameters configuration service var address = servicesecurityinspector.getnettcprelaybindingaddress(typeof(tcontract)); string issuername = configurationmanager.appsettings["shared.appsettings.azureservicebus.issuername"]; string issuersecret = configurationmanager.appsettings["shared.appsettings.azureservicebus.issuersecret"]; // create nettcprelaybinding, if (cf == null) { cf = new channelfactory<tchannel>(new nettcprelaybinding(), new endpointaddress(address)); cf.endpoint.behaviors.add(new transportclientendpointbehavior { tokenprovider = tokenprovider.createsharedsecrettokenprovider(issuername, issuersecret) }); } tchannel channel = cf.createchannel(); // open channel iclientchannel clientchannel = channel iclientchannel; if (clientchannel != null) { clientchannel.open(); } return channel; } /// <summary> /// gets channel making call on relay bus. /// note channel cached. /// , each thread has it's own channnel. /// </summary> /// <returns>the channel</returns> public static tchannel getchannel() { // check if have channel instance if (!staticchannel.isvaluecreated) { // no, create 1 staticchannel.value = createchannel(); } else { // if channel exists iclientchannel clientchannel = staticchannel iclientchannel; // check if open, if not, make new 1 if (clientchannel != null) { communicationstate state = clientchannel.state; // check state if (state == communicationstate.faulted) { // channel in faulted state, close , recreate closechannel(); staticchannel.value = createchannel(); } else if ((state == communicationstate.closed) || (state == communicationstate.closing)) { // channel closed or closing, recreate staticchannel.value = createchannel(); } } } return staticchannel.value; } /// <summary> /// closes channel in proper way /// </summary> private static void closechannel() { // check if still have valid channel if (staticchannel != null) { iclientchannel clientchannel = staticchannel iclientchannel; if (clientchannel != null) { // if channel open, close if (clientchannel.state != communicationstate.closed) { clientchannel.abort(); } } // , set static variable it's default ( = null ) staticchannel = null; } } }
Comments
Post a Comment