accessing wcf-web service from PlugIns or Custom Workflows under Sandbox Isolation Mode at Dynamics CRM

If you want to make an external web service call inside PlugIn or Workflow in dynamics CRM (even under sandbox) , Use following code at your PlugIn or Custom Workflows;





edwed wedwed 



                        try
                        {
                            BasicHttpBinding httpbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                            httpbinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
                            httpbinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;

                            httpbinding.CloseTimeout = new TimeSpan(0, 5, 0);

                            System.ServiceModel.Channels.Binding binding = httpbinding;

                            System.ServiceModel.EndpointAddress remoteAddress = new EndpointAddress("http://abc.com.tr/xyzService.svc");

                            xyzServiceClient client = new xyzServiceClient(binding, remoteAddress);
                            var methodResult = Convert.ToBase64String(client.GetData(xxxPathinString));
                            ....
                         

                        }
                        catch (System.Security.SecurityException exp)
                        {
                            //Log exception to the Plugin Trace Log:
                            tracingService.Trace("Exception occured in xyzMethod call :" + exp.Message);
                        }
                        catch (ArgumentException exp)
                        {
                            //Log exception to the Plugin Trace Log:
                            tracingService.Trace("Exception occured in xyzMethod call :" + exp.Message);
                        }
                        catch (WebException exception)
                        {
                            string str = string.Empty;
                            if (exception.Response != null)
                            {
                                using (StreamReader reader =
                                    new StreamReader(exception.Response.GetResponseStream()))
                                {
                                    str = reader.ReadToEnd();
                                }
                                exception.Response.Close();
                            }
                            if (exception.Status == WebExceptionStatus.Timeout)
                            {
                                throw new InvalidPluginExecutionException(
                                    "The timeout elapsed while attempting to issue the request.", exception);
                            }
                            throw new InvalidPluginExecutionException(String.Format(CultureInfo.InvariantCulture,
                                "A Web exception occurred while attempting to issue the request. {0}: {1}",
                                exception.Message, str), exception);
                        }
                        catch (Exception exp)
                        {
                            //Log exception to the Plugin Trace Log:
                            tracingService.Trace("Exception occured in xyzMethod call :" + exp.Message);
                        }


enjoy...

Comments

Popular posts from this blog

Assembly Microsoft.Dynamics.Service.Plugins.dll can not be loaded. Dynamics CRM 365 Engine version 9 - CRM User creation error

Exception caught instantiating TERADATA report server extension SQL Reporting Services

Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified at Configuration class initiation in CrmServiceHelper.cv