2

我正在寻找一种从 NET 4.0 应用程序使用此 Web 服务的方法:

http://resultsservice.lottery.ie/ResultsService.asmx

像往常一样,我将服务引用添加到服务中,显然代理已创建,一切看起来都很正常,但是调用“GetResults”方法会引发异常:

var service = new LotteryResultsServiceSoapClient("LotteryResultsServiceSoap");
var results = service.GetResults(DrawType.EuroMillions, 1);

你调用的对象是空的。

这很少见,因为该服务的其他方法 (GetProjectedJackpot) 运行良好。我尝试添加 Web 引用而不是服务引用,但令人惊讶的是……它工作正常。所以,也许我做错了什么。

问题是我无法使用 Web 引用,因为我想将此应用程序移植到 WindowsRT,而 Metro 应用程序不支持 Web 引用。

4

1 回答 1

1

Web Reference generated proxies include a User-Agent value in the header of the outgoing HTTP message. Service Reference generated proxies do not add this value by default. Luckily, this behavior is pretty well documented.

It appears that the null object exception is resulting from the missing User-Agent value in the Service Reference proxy. The value being populated by the Web Reference is Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.269). Using Charles Proxy I was able to verify that adding this value to all out-going HTTP messages generated by the Service Reference will solve your problem.

WCF provides several possible ways to modify outgoing soap messages and HTTP requests. Here is a really great article that discusses a couple of these solutions and contains some good code samples.

于 2012-09-18T15:27:29.120 回答