I have successfully been able to call my Web Service (WCF Service using Basic HTTP Binding) programatically from Infopath 2010 using the C# code below. Note: as far as I know, the code would be the same in Infopath 2007:
public void FormEvents_Submit(object sender, SubmitEventArgs e)
{
//XPathNavigator mainDataNavigator = MainDataSource.CreateNavigator();
WebServiceConnection webConnection = (WebServiceConnection)DataConnections["Main submit"];
XmlDocument outputDoc = new XmlDocument();
XmlDocument errorDoc = new XmlDocument();
XPathNavigator outputNavigator = outputDoc.CreateNavigator();
XPathNavigator errorsNavigator = errorDoc.CreateNavigator();
try
{
webConnection.Execute(null, outputNavigator, errorsNavigator);
// TODO: Use the output from outputNavigator, to see if the Service Accepted.
}
catch (System.Exception ex)
{
e.CancelableArgs.Cancel = true;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("Error thrown when trying to connect to Web Service.");
sb.Append("\r\n");
sb.Append("Error as follows:");
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append(ex.Message);
sb.Append((ex.InnerException != null ? ex.InnerException.Message :""));
sb.Append("\r\n");
sb.Append("\r\n");
sb.Append("Please try submitting again. If the error persists, contact your administrator.");
MessageBox.Show(sb.ToString(),"Resubmit Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
e.CancelableArgs.Cancel = false;
}
This code works, and I am now able to interrogate the response coming back that is populated in the outputNavigator object.
So far so good.
But I purposely corrupted my WS endpoint, so Infopath would fail on the call. My catch block (see above code ) kicks in, but the error message coming back is not at all helpful in diagnosing the error. See below:
System.Net.WebException was unhandled by user code
Message="Exception from HRESULT: 0x8004304D"
Source="Microsoft.Office.InfoPath.Client.Internal.Host"
StackTrace:
at Microsoft.Office.InfoPath.Internal.MomExceptionHelper.ExecuteDataConnectionAction(XmlFormHost formHost, OMCall d)
at Microsoft.Office.InfoPath.Internal.WebServiceConnectionHost.Execute(XPathNavigator input, XPathNavigator output, XPathNavigator errors)
at ResubmitTradeFeedMessage2010.FormCode.FormEvents_Submit(Object sender, SubmitEventArgs e)
at Microsoft.Office.InfoPath.Internal.FormEventsHost.OnSubmit(DocReturnEvent pEvent)
at Microsoft.Office.Interop.InfoPath.SemiTrust._XDocumentEventSink2_SinkHelper.OnSubmitRequest(DocReturnEvent pEvent)
The service endpoint is a WCF endpoint using Basic HTTP Binding. The binding is also using configured with a behaviour of: Include Exception Details in Faults, so the Fault message will be returned to the consumer.
I checked the event log, and there is a log, that clearly states the problem. From this error it is clearly evident, what the problem is, and gives much more info. This is the error that is sent back to the client but for some reason, infopath, does not give me the error below, but the incomplete errror as above. I also create a .Net Client that consumes the same service as the infopath form. When I ran this client against the corrupted service, I received the correct error as below:
Event Type: Warning
Event Source: BizTalk Server 2009
Event Category: BizTalk Server 2009
Event ID: 5740
Date: 6/16/2010
Time: 5:31:09 PM
User: N/A
Computer: IMDEVTA18
Description:
The adapter "WCF-BasicHttp" raised an error message. Details "System.Xml.XmlException: Root element is missing.
at Microsoft.BizTalk.Adapter.Wcf.Runtime.BizTalkAsyncResult.End()
at Microsoft.BizTalk.Adapter.Wcf.Runtime.BizTalkServiceInstance.EndOperation(IAsyncResult result)
at Microsoft.BizTalk.Adapter.Wcf.Runtime.BizTalkServiceInstance.Microsoft.BizTalk.Adapter.Wcf.Runtime.ITwoWayAsync.EndTwoWayMethod(IAsyncResult result)
at AsyncInvokeEndEndTwoWayMethod(Object , Object[] , IAsyncResult )
at System.ServiceModel.Dispatcher.AsyncMethodInvoker.InvokeEnd(Object instance, Object[]& outputs, IAsyncResult result)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeEnd(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage7(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)".
I have also looked the errorNavigator, that is returned in the call : webConnection.Execute(null, outputNavigator, errorsNavigator); But unfortunately it is empty. Has anybody encountered the same issue, where infopath is not exposing/returning the correct error ?
Thanks,
Matt