在客户端不使用配置文件,通过写代码的方式调用寄宿在IIS上Wcf服务的时候,出现一个比较奇怪的问题。
客户端的代码如下:
Binding httpBinding = new BasicHttpBinding(); EndpointAddress httpAddressIISHost = new EndpointAddress("http://localhost/wcfservices/CalculatorService.svc"); using (Client iisClient = new Client(httpBinding, httpAddressIISHost)) { try { Console.WriteLine("begin invocate calculator service via http transport"); Console.WriteLine("x + y = {0} where x = {1} and y = {2}", iisClient.Add(1, 3), 1, 3); } catch (Exception e) { Console.WriteLine(e.Message); } finally { Console.ReadLine(); } }
服务端配置文件如下:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="metadataBehavior" name="Services.CalculatorService"> <endpoint binding="wsHttpBinding" contract="Contracts.ICalculator"/> </service> </services> </system.serviceModel> </configuration>
调用的时候报出错误:
Content Type text/xml; charset=utf-8 was not supported by service http://localhost/wcfservices/CalculatorService.svc. The client and service bindings may be mismatched.
偶尔一不小心我把配置文件的service节点的name属性改成了Services.CalculatorServices,也就是后面加了"s",服务却能被成功调用了。但是实际上这时候通过浏览器问地址http://localhost/wcfservices/CalculatorService.svc的时候是不成功。
不能理解为何会出现这种情况。做个记号。