CRM 2011 On-Premise: Plugin calling a web service - How can I configure the web reference for different environments?


  • I have a set of plugins that call out to a web service. So in the plugin project I have a web reference. The reference is dynamic, so in my app.config I have the setting for the URL. So when I create and deploy this to my development environment it works fine.  But how do I deploy my CRM solution to my testing and staging environments, as I need to adjust the app.config?  Since this is somehow stored in SQL server, I don't have access to the app.config once it is deployed.

    I have tried a couple of things.  I have added code to reset the web references URL in the constructor of a service class that all of my plugins use.  I store the environment name in an admin entity in CRM. So in the constructor I hit CRM to pull the environment name and construct the web-service's URL. My architecture is [plugin -> service class -> web service]. However, it seems that code is executed but CRM is ignoring that code and continues to use the app.config settings and ignores my updated URL value.  This would work fine in a web-app or win-app, but apparently in CRM it will not work.  Maybe there is a different way to do this in CRM?  I'm curious how others might be approaching something like this.

    The only way I have been able to get this to work is to deploy the plugins manually to each environment after I deploy the solution.  That is an annoyance and kind of breaks the whole methodology of testing in DEV and promoting to TST, then to STG, then to PRD, as I am actually deploying from my local machine to DEV, my local machine to TST, my local machine to STG, my local machine to PRD.  It works, but I'd rather wrap it all into the CRM solution and simply deploy to the solution from DEV to TST, from TST to STG, from STG to PRD.  Here's my constructor and how I am using it.  But this doesn't work.  It seems that the plugins just ignore it and do not use the updated URL.   

    So my question is, how do you configure web references from within a plugin for different environments, if I cannot reset the URL programmatically nor can I access the app.config after the plugins have been deployed?  Is manually deploying it to each environment the only option?  I hope not.

    Updated:  I should have mentioned that this is calling a traditional web-service (asmx) and not a WCF web service.  So I cannot access the ending point they way you might with a WCF service reference.  The .URL is the only option that I see, but that doesn't work.  Maybe I will test it out as a service-reference and see if I can do that for a traditional web-servive, but I don't think that will work. I don't think something like the following will work for me, but it might. 

    _dhiIntegrationWebService = new IntegrationSoapClient(_dhiIntegrationWebService.Endpoint.Binding, new EndpointAddress(wsPath + "Integration.asmx"));

    Here's my current code.  I might try to change it to use something like the above to see if that will work, but I suspect not because it is an .ASMX and not a .SVC.

    public CrmServices(IOrganizationService service)
                _service = service;
                // Pull the Web Service URL from CRM so we do not have to maintain it for each deployment.
                var wsPath = "";
                var querybyattribute = new QueryByAttribute("dhi_adminconfiguration")
                    ColumnSet = new ColumnSet("dhi_key", "dhi_value")
                var resp = _service.RetrieveMultiple(querybyattribute);
                foreach (var entity in resp.Entities)
                    if (entity.Attributes.Contains("dhi_value"))
                        wsPath = entity.Attributes["dhi_value"].ToString();
                _dhiCustomizationWebService.Url = wsPath + "Customization.asmx";
                _dhiIntegrationWebService.Url = wsPath + "Integration.asmx";

    Jon Gregory Rothlander

    Wednesday, May 27, 2015 6:18 PM

All replies