locked
Issue when calling an external web service - CRM 2013 RRS feed

  • Question

  • Hello everyone.

    This is my scenario:

    I'm doing a real time data integration from CRM to another software. I deployed several plugins, mostly in create/update messages, for the entities i want to sync. The developers of the other software gave me the endpoint of the web service and I created the client using svcutil. I manually created the binding and gave it to the generated client, along with the endpoint. At the first tests I could sync some registries, but I had some mapping errors. I debugged the plugin, corrected the errors, and when I want to tests again, the plugin is throwing me an exception when calls any method of the web service. Let me explain the issue:

    The exception:

    I instantiated the client for the web service (passing to it the binding and the endpoint). And when I'm trying to call any method (ex. Add), it throws the exceptions "InvalidOperationException". I started to debug the code and I notice that just before to call the web service method, the state of the client stills in "Opening". Of course that is generating the exception. 

    The complete exception is: "The communication object, System.ServiceModel.ChannelFactory`1[IAxxisWcfServices], cannot be modified while it is in the Opening state."} System.SystemException {System.InvalidOperationException}

    I did a test, outside the CRM environment, on a console app with the same conditions of the web services client (generating with svcutil, manually passing the same binding and endpoint), and the same order of the plugin code, and everything works well; The state of the client never stills in 'Opening', so I discarded any problem with the web service.

    Anyone knows if I'm missing something in the code? or some configuration?

    The CRM is 2013 On-premise with last updates.

    Thanks in advance.

    Monday, March 2, 2015 3:45 PM

All replies

  • HI Hugo,

    Create some proxy/ServiceObjectHandler to use the Service object.

    Proxy would managed the usages of service object and would make sure that the current object is not in use somewhere, if it is then it would create the new object for use.

    Monday, March 2, 2015 4:53 PM
  • Thanks for the answer.

    I tried a handler where I check the state of the client. If not ready for messaging, dispose the client and create new one. But just before the calls of any methods in the web service, I check the state and stills 'Opening'.

    Monday, March 2, 2015 5:53 PM
  • How are you specifying the configurable settings for the web service instance ? Are you relying on a .config file, or are they all set in code ? If you rely on a .config file, then that is one likely difference between running the code in a plugin compared to the console app, as the .config file depends on the process in which the plugin runs, which in turn depends on whether the plugin assembly is registered in the sandbox or not, and whether the step is asynchronous or not

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Monday, March 2, 2015 8:20 PM
    Moderator
  • How are you specifying the configurable settings for the web service instance ? Are you relying on a .config file, or are they all set in code ? If you rely on a .config file, then that is one likely difference between running the code in a plugin compared to the console app, as the .config file depends on the process in which the plugin runs, which in turn depends on whether the plugin assembly is registered in the sandbox or not, and whether the step is asynchronous or not

    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    The config settings are set in code, for the reasons you gave.

    I generated the client using svcutil and set the binding and endpoint manually in code.

    The plugins are registered with none isolation mode and synchronous

    When I use the same svcutil-generated client and the same binding and endpoint (both in code), in a console app, everything works fine. Weird.


    Pd: I followed the rules about web access through plugins
    Monday, March 2, 2015 8:51 PM
  • I tried to consume the web service in other MS CRM platform, independent from the current project, and it throws me the same error. Maybe, can i start to think that is and error of the web service?

    The weird thing is the service works when consume it within a console app.

    Any ideas?


    Tuesday, March 3, 2015 3:05 AM
  • I tried adding the web reference in a console app and works fine. In the plugins this can't be done, because the plugins are deploy in database.

    I tried another basic test using ChannelFactory. Only want to retrieve some entity through the external web services:

    IPluginExecutionContext context = localContext.PluginExecutionContext;
    IOrganizationService service = localContext.OrganizationService;
    ITracingService tracer = localContext.TracingService;
    try
    {
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Name = "BasicHttpBinding_IAxxisWcfServices";
    	binding.Security.Mode = BasicHttpSecurityMode.None;
            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
    	binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
    	
    	EndpointAddress endpoint = new EndpointAddress(@"http://*****.svc");
    	ChannelFactory<IAxxisWcfServices> factory = new ChannelFactory<IAxxisWcfServices>(binding, endpoint);
    	IAxxisWcfServices channel = factory.CreateChannel();
            var entity = channel.FindByEntities(new CRM.Application.Context.Dtos.Asegurado( { CrmGuid = "7C3E96C6-C3BC-E411-80DF-0A736AE14667" }, null, new CRM.Framework.Session() { User = "***", Password = "***" });
    }
    catch (Exception ex)
    {
            throw new InvalidPluginExecutionException(ex.Message);
    }

    That test threw the following exception on the GenericParameterAttributes of channelType of factory object:

    http://s27.postimg.org/4wf3er8up/Error.png


    Tuesday, March 3, 2015 5:19 AM
  • I made another test with the console app consuming the web service but this time running it in the server of CRM (for discarding possibles platforms problems). That test went fine.

    Another test I made was deploy (at the same platform) a plugin that consumed other non-WCF plugin. This also went fine

    The problem goes only when consuming the above-mentioned web service within a CRM plugin.

    This service is developed using WCF.

    Any ideas of what might be happening?

    Thanks in advance.
    Wednesday, March 4, 2015 8:28 AM