Salesforce Apex and Windows Communication Foundation (WCF)

Trying to work with Windows Communication Foundation ( WCF ), which can build web services, from Microsoft can be difficult at times. Here are three links which describe some of those difficulties.

1)  The first one is pulled from the DeveloperForce boards…

Error Parsing Net 3.0 WCF WSDL From Apex Code

Answer 1… So, WCF splits the wsdl in to different parts and uses imports and such. We (Salesforce) currently don’t support import or include.  We (Salesforce) also don’t support any WS-* specs. Because of these two reasons, it’s best just to use the regular .NET asmx approach.

Answer 2… What in particular in WCF are you looking to use that ASMX doesn’t give you?

So we will struggle trying to import a WSDL from WCF since Salesforce APEX does not support import or include items in the WSDL when doing WSDL2Apex.

2) Another technical reason for the struggles with WCF is explained in this article…

Salesforce Net API Integration

Here is the main answer… WCF does not support WSDL defined SOAP headers which (the) Salesforce API uses for carrying session identifier.

3) Here is a third article where somebody ran into a problem integrating with BizTalk which was using WCF…

Experience Of Biztalk and Salesforce.com Integration

Here is the specific point from this article…  (Here Biztalk exposed as WS and Apex API consumes it, initially we had exposed it as a WCF Svc and later on found out that somehow the APEX Api does not like WCF so had to step down on Web Service.)

So if you want to use a web service created from a Microsoft based product and you want to use the WSDL2Apex feature then it seems you must generate a traditional .asmx web service.

Here is a nice article that gives some pointers when you see errors when importing your WSDL using WSDL2Apex…

Consuming ASP.NET Web Service

Of course, if you cannot get your WSDL to import, or if you really only need to make one call out of a large WSDL file, then you can always create your XML dynamically in APEX and then use the Http, HttpRequest and HttpResponse Apex objects to make the needed web service call.
I have been able to make a successful call to a WFC web service using the Http, HttpRequest and HttpResponse objects in APEX.  I constructed the XML dynamically and then made the following calls on the HttpRequest object.

Http hL = new Http();
HttpRequest reqL = new HttpRequest();
reqL.setTimeout(60000);  //This may be too long in some scenarios
reqL.setEndpoint(endPoint);  //String Value
reqL.setMethod(‘POST’);
reqL.setHeader(‘SOAPAction’, ‘https://my.service.com/connect/IConnect/ActionToTake’);
reqL.setHeader(‘User-Agent’, ‘SFDC-Callout/22.0’);
reqL.setHeader(‘Accept’,‘text/xml’); 
reqL.setHeader(‘Content-type’,‘text/xml’); 
reqL.setHeader(‘charset’,‘UTF-8’);
reqL.setBody(bodyToSend);  //Fully constructed XML body
HttpResponse resL = hL.send(reqL);

If all else fails with SOAP, then look to see if there is a REST option.

Here are a couple of the main links for integrating with Salesforce…

Apex Web Services and Callouts
Integrating with the Force.com Platform


Here is another bit of information related to a common error message when doing integrations. I have received this error message before when trying to connect to a web service…

There was an error processing this request. Details: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

This error means that the certificate on web service is not supported by Salesforce APEX.  Salesforce supports a limited number of certificate vendors including VeriSign.

I hope this article helps you to move around these roadblocks quickly when you run into them on your integration projects.  Happy integrating!

Comments

Returning visitor? Please login or register.

Hi Terry,

Thanks for such an informative article. Actually, in our next project, we have a requirement where our web service has to exchange information between salesforce and a .net application. So it must enable two way communication but as you have mentioned above, salesforce is not WCF friendly, how can we achieve the same using an asmx web service?

Thanks & Regards,
Sandeep

Sandeep Posted on: Jul 17, 2013 at 07:42 PM

Sandeep,

Thanks for the comment!  I was able to get the WCF web service to work in the end by constructing my own XML and then using the APEX built-in HTTP objects instead of relying on the WSDL2APEX feature.  I would have you try that first so you can try to stay with WCF on the .NET side. 

In trying to figure out the structure of the XML I would first import the WSDL into .NET and use .NET to make some calls to the WCF web service while having a tool like Fiddler run which will capture the HTTP call and show the correct structure of the XML.

I hope that helps!

Terry Luschen Posted on: Jul 17, 2013 at 09:09 PM

Hi Terry,

As I have just started working on the thing, I have also got an idea by googling some communities that we will have to modify the WSDL generated by the WCF service to consume the service in Salesforce. As you also suggested in your previous reply, we will have to reconstruct the WSDL (XML). Can you please guide which tags should we remove or modify in the generated WSDL?

Sandeep Parashar Posted on: Aug 06, 2013 at 03:01 AM

Sandeep,

I think you have two options…

1) Take the WSDL generated by the WCF service and try to use the WSDL2APEX feature in Salesforce.  If it does not work, then you could try modifying the WSDL to get it to import.  Sometimes it can be easy like changing a reserved word.  If you can get it to import, then hopefully you are set.

2) Try to construct the XML that your WCF web service would like to consume in APEX and use the HTTP objects in APEX to make the call.  This option would not use the WSDL2APEX feature and it would not involve trying to change the WSDL in any way.  Here is a link to a blog post I did about calling the Partner API in Salesforce in this manner.
http://www.sundoginteractive.com/sunblog/posts/getting-a-session-id-using-the-login-partner-api-call

I hope that helps!  Good luck!

Terry Luschen Posted on: Aug 06, 2013 at 06:29 AM

how can we communicate asp.net web service(.asmx) with salesforce for login purpose (i.e singlw signe on system)

Amjad Raza Khan Posted on: Sep 30, 2013 at 07:05 PM

Amjad,

You can use the Partner or Enterprise WSDL to login into Salesforce.  That will return you a string which you can use to make any queries or updates you would like to make.

I hope that helps.

Terry Luschen

Terry Luschen Posted on: Sep 30, 2013 at 08:40 PM

Leave A Comment

Please help us stop spam by answering the question below: