Consume a WCF from inside a Dynamics 365 plugin

1 min read

Summary

A code snippet demonstrating how to consume a WCF web service from inside a Dynamics 365 plugin.

The code

private static HelloService.HelloServiceClient ServiceBinding(IOrganizationService service, ref Entity entity) {

    BasicHttpBinding serviceBinding = new BasicHttpBinding();
    serviceBinding.Name = "BasicHttpBinding_IHelloService";
    serviceBinding.Security.Mode = BasicHttpSecurityMode.None;
    serviceBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
    serviceBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
    serviceBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
    EndpointAddress endPointAddress = new EndpointAddress("http://localhost:8080/HelloService");
    HelloService.HelloServiceClient xclient = new HelloService.HelloServiceClient(serviceBinding, endPointAddress);

    return xclient;

    //CALL METHOD USING:
    //HelloService.HelloServiceClient xclient = ServiceBinding(service, ref entity);
}

Full code example:

//PLUG-IN CODE---------------------------------------------------------------------------------------------------- 
 try {
     ColumnSet attributes = new ColumnSet(new string[] {
         "address1_addresstypecode", "new_globaloptionset"
     });
     entity = service.Retrieve(entity.LogicalName, entity.Id, attributes);

     HelloService.HelloServiceClient xclient = ServiceBinding(service, ref entity);
     string message = "Adam";

     //CALL THE WCF SERVICE, PASS THE MESSAGE AS AN ARGUMENT AND UPDATE THE NAME FIELD IN THE ACCOUNT FORM
     entity["name"] = xclient.GetMessage(message);
     
     if (context.Depth <= 1) {
         service.Update(entity);
     }
 } catch (FaultException ex) {
     throw new InvalidPluginExecutionException("An error occurred in the plug-in", ex);
 }
 private static HelloService.HelloServiceClient ServiceBinding(IOrganizationService service, ref Entity entity) {

     BasicHttpBinding serviceBinding = new BasicHttpBinding();
     serviceBinding.Name = "BasicHttpBinding_IHelloService";
     serviceBinding.Security.Mode = BasicHttpSecurityMode.None;
     serviceBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
     serviceBinding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
     serviceBinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
     EndpointAddress endPointAddress = new EndpointAddress("http://localhost:8080/HelloService");
     HelloService.HelloServiceClient xclient = new HelloService.HelloServiceClient(serviceBinding, endPointAddress);
     return xclient;

     //CALL METHOD USING:
     //HelloService.HelloServiceClient xclient = ServiceBinding(service, ref entity);
 }

 //WCF CODE----------------------------------------------------------------------------------------------------
  public class HelloService : IHelloService
    {
        public string GetMessage(string name)
        {
            return "Hello" + name;
        }
    }

 //RESULT ----------------------------------------------------------------------------------------------------

 Hello Adam

You must be logged in to post a comment.