locked
Error in Plugin which call WCF service. localContext.ServiceProvider is null. RRS feed

  • Question

  • Hello,

    I am writing Plugin on Sales Order Save event which calls WCF service.

    I got reference in

    http://nishantrana.wordpress.com/2010/11/20/calling-wcf-service-in-plugin-in-crm/

    and modified my code and web config.

    But now i got error "Object reference not set to an instance of an object"

    I debugged and found that i got "Null" in localContext.ServiceProvider.

    My code is as below.

    protected void ExecuteCRMOrderToAX(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                BasicHttpBinding myBinding = new BasicHttpBinding();
    
                myBinding.Name = "BasicHttpBinding_SalesOrderService";
    
                myBinding.Security.Mode = BasicHttpSecurityMode.None;
    
                myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
    
                myBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
    
                myBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
    
                EndpointAddress endPointAddress = new EndpointAddress("http://<<myaxurl>>/MicrosoftDynamicsAXAif50/salesorderservice.svc");
                
                // TODO: Implement your custom Plug-in business logic.
                var serviceProvider = localContext.ServiceProvider;  // Here i m getting serviceProvider as null
    
                #region "Passing CRM Credential"
    
                ax.SalesOrderServiceClient sc = new ax.SalesOrderServiceClient(myBinding, endPointAddress);
                sc.ClientCredentials.Windows.ClientCredential.UserName = "crmusername";
                sc.ClientCredentials.Windows.ClientCredential.Password = "crmpassword";
    
                sc.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
    
                #endregion
    
                #region "Create Sales Order"
    
                // Obtain the execution context from the service provider.
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); //getting error because serviceProvider is null


    My .config file is as below.

    <configuration>
        <configSections>
        </configSections>
        <system.serviceModel>
            <bindings>
                <basicHttpBinding>
                    <binding name="BasicHttpBinding_SalesOrderService" closeTimeout="00:01:00"
                        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                        allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                        messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                        useDefaultWebProxy="true">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                        <security mode="TransportCredentialOnly">
                            <transport clientCredentialType="Ntlm" proxyCredentialType="None"
                                realm="" />
                            <message clientCredentialType="UserName" algorithmSuite="Default" />
                        </security>
                    </binding>
                </basicHttpBinding>
            </bindings>
            <client>
                <endpoint address="http://<<myaxurl>>/MicrosoftDynamicsAXAif50/salesorderservice.svc"
                    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_SalesOrderService"
                    contract="AXServiceReference.SalesOrderService" name="BasicHttpBinding_SalesOrderService" />
            </client>
        </system.serviceModel>
    </configuration>

    Tuesday, December 18, 2012 10:38 AM

Answers

  • Hi RK.CRM,

    You are right. So i marked your post as helpful.

    Its my mistake. Actually i am working on Plugin through Visual Studio Template of CRM. So it creates auttomatically with

    LocalPluginContext localContext

    parameter.

    But i then realize how to work with this. My correct code is as below.

    IPluginExecutionContext context = localContext.PluginExecutionContext;

    IOrganizationService _service = localContext.OrganizationService;




    • Edited by DynamicsCRM31 Wednesday, December 19, 2012 5:04 AM
    • Marked as answer by DynamicsCRM31 Wednesday, December 19, 2012 5:04 AM
    Wednesday, December 19, 2012 5:03 AM

All replies

  • Please help me someone who have faced this things.
    Tuesday, December 18, 2012 11:31 AM
  • Hi

    You can try  "IServiceProvider serviceProvider" instead of "LocalPluginContext localContext"  and no need to write "var serviceProvider = localContext.ServiceProvider;".

    Try this it may work.


    Thanks

    Tuesday, December 18, 2012 2:49 PM
  • Hi RK.CRM,

    You are right. So i marked your post as helpful.

    Its my mistake. Actually i am working on Plugin through Visual Studio Template of CRM. So it creates auttomatically with

    LocalPluginContext localContext

    parameter.

    But i then realize how to work with this. My correct code is as below.

    IPluginExecutionContext context = localContext.PluginExecutionContext;

    IOrganizationService _service = localContext.OrganizationService;




    • Edited by DynamicsCRM31 Wednesday, December 19, 2012 5:04 AM
    • Marked as answer by DynamicsCRM31 Wednesday, December 19, 2012 5:04 AM
    Wednesday, December 19, 2012 5:03 AM