我有一个小型的Intranet服务,需要工作,我们正在将测试配置从一个域提交到另一个域,没有将建立的域信任,并且由于输入模板会大量更改数据,因此我们决定将Infopath用于脚本编写者/实验室的测试人员输入他们的数据。我想,亲爱的,也许我应该做一个小型WCF服务器并将其托管在我们的IIS服务器上……应该很容易,对吧?
排队笑
我已经使用从Infopath到bool TestInfoPathSubmit(string testString);
的OperationContract的简单字符串提交测试了连接性。它在开发服务器和生产服务器上都可以正常工作,减去了似乎总是发生的少量权限高峰。
我已经从使用工作的xml并使用.NET ResultDataSet.xsd
应用程序生成的xsd.exe
文件中使用了很多东西,所以我知道它是有效的,因此我可以跳过链接该代码。但是这里是其余的:
界面/合同
[DataContractFormat(Style = OperationFormatStyle.Document)]
[ServiceContract]
public interface IService
{
[XmlSerializerFormat]
[OperationContract]
bool ResultFromInfopath(XmlDocument xdoc);
}
注意:已注意到
[XmlSerializerFormat]
可以避免XmlDocument
无法在WCF中进行序列化/反序列化的问题。服务
public class Service1 : IService
{
public bool ResultFromInfopath(XmlDocument xdoc)
{
try
{
var xreader = new XmlNodeReader(xdoc);
var dataset = new ResultDataSet();
dataset.ReadXml(xreader);
Debug.Print("XML has been read in and ready to process");
return true;
}
catch (Exception ex)
{
Debug.Print(ex.Message);
return false;
}
}
}
网络配置
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpBinding" scheme="http" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
没什么幻想,试图保持简单。我使用xsd文件创建了Infopath文档模板,并将Submit链接到Web Service,然后选择了作为完整XmlDocument提交的选项,包括处理说明。这是我犯错的地方。不要包括处理说明。 MSDN - XmlDictionaryReader是消息流的默认解码器,它不接受InfoPath放入
XmlDocument
中嵌入的处理指令。您返回的错误是提交无法完成:
InfoPath cannot submit the form.
An error occurred while the form was being submitted.
The SOAP response indicates that an error occurred:
Error in deserializing body of request message for operation 'ResultFromInfopath'.
<detail>
<ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<HelpLink i:nil="true"/>
<InnerException>
<HelpLink i:nil="true"/>
<InnerException>
<HelpLink i:nil="true"/>
<InnerException i:nil="true"/>
<Message>
Processing instructions (other than the XML declaration) and DTDs are not supported. Line 1, position 485.
</Message>
<StackTrace>
at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
at System.Xml.XmlUTF8TextReader.ReadDeclaration()
at System.Xml.XmlUTF8TextReader.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.ReadCurrentNode(XmlDocument doc, XmlReader reader)
at System.Xml.XmlDocument.ReadNode(XmlReader reader)
at System.Xml.Serialization.XmlSerializationReader.ReadXmlNode(Boolean wrapped)
at System.Xml.Serialization.XmlSerializationReader.ReadXmlDocument(Boolean wrapped)
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderIService.Read2_ResultFromInfopath()
at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Deserialize(XmlSerializationReader reader)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
</StackTrace>
<Type>
System.Xml.XmlException
</Type>
</InnerException>
<Message>
There is an error in XML document (1, 485).
</Message>
<StackTrace>
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle)
at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest)
</StackTrace>
<Type>
System.InvalidOperationException
</Type>
</InnerException>
<Message>
Error in deserializing body of request message for operation 'ResultFromInfopath'.
</Message>
<StackTrace>
at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, XmlSerializer serializer, MessagePartDescription returnPart, MessagePartDescriptionCollection bodyParts, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.XmlSerializerOperationFormatter.DeserializeBody(XmlDictionaryReader reader, MessageVersion version, String action, MessageDescription messageDescription, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
</StackTrace>
<Type>
System.ServiceModel.CommunicationException
</Type>
</ExceptionDetail>
</detail>
下面提供了答案。
最佳答案
由于XmlDictionaryReader
无法从Infopath读取指令,因此您需要将Infopath的数据提交设置为“为所选参数提交以下数据:”->“字段或组”。从XSD中选择基础节点作为Infopath的元素,然后让数据连接向导完成其余的工作。
将要提交的XmlDocument xdoc
必须读入数据集,此时,您可以很好地使用数据。
关于c# - 如何使用Infopath向WCF Web服务提交表单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20648152/