首页 > 单独文章 > 正文

用InfoPath 2007表单调用WCF服务的问题

时间:2008-04-12 09:47:01 作者:officeba 【认证】

尝试在InfoPath2007 表单中的托管代码里调用WCF服务的时候,遇到了一个问题,InfoPath会报错说找不到服务的Contract。但实际上这个服务的配置文件是没有问题的。

后来在这个文章里找到了解法,简单做了个演示例子,供大家参考。

首先,我做了一个很简单的WCF服务,发送一个代表学号的字符串请求,返回一个学生姓名。

该服务运行位置:http://localhost:50446/WCFTest/Service.svc

然后我写了一个WinConsole的客户端程序进行调用,结果如下,验证客户端的配置无误:

接下来,我打开InfoPath,放置几个简单的控件,然后在VSTA中编写“Get Name”按钮的响应函数:

ServiceClient client = new ServiceClient();
string result = client.GetStudentName(this.CreateNavigator().SelectSingleNode("/my:myFields/my:txtStudentID",this.NamespaceManager).Value);
this.CreateNavigator().SelectSingleNode("/my:myFields/my:msgBox", this.NamespaceManager).SetValue(result);

逻辑很简单,就是将StudentID传递给服务,取得的结显示在Student Name栏中。做法和之前的WinConsole程序无异

预览表单,输入学号,点击“Get Name”

 

得到了错误信息:

system.InvalidOperationException
Could not find default endpoint element that references contract 'IService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

原因为InfoPath未能认出Configuration的信息。

(注:我觉得很有可能是我配置的问题,所以如果您知道正解,欢迎留言赐教)

解决办法为,修改按钮响应函数为:

EndpointAddress address = new EndpointAddress("http://localhost:50446/WCFTest/Service.svc");
WSHttpBinding binding = new WSHttpBinding();
IServiceChannel channel = ChannelFactory<IServiceChannel>.CreateChannel(binding, address);
string result = channel.GetStudentName(this.CreateNavigator().SelectSingleNode("/my:myFields/my:txtStudentID",this.NamespaceManager).Value);
this.CreateNavigator().SelectSingleNode("/my:myFields/my:msgBox", this.NamespaceManager).SetValue(result);

直接使用服务Channel的接口进行调用,顺利得到预期结果:


相关文章

同类最新