我创建了一个 WCF 服务:
共享.dll:
[ServiceContract(ConfigurationName = "ICalculator")]
public interface ICalculator
{
[OperationContract()]
int Add(int a, int b);
}
服务器:
[ServiceBehavior()]
public class Calculator : ICalculator
{
public int Add(int a, int b) { return a + b; }
}
客户(尝试#1):
public class CalculatorClient : ClientBase<ICalculator>, ICalculator
{
private static Binding binding = new WSHttpBinding("MyConfig");
private static EndpointAddress remoteAddress = new EndpointAddress(...);
public CalculatorClient() : base(binding, remoteAddress) { }
public int Add(int a, int b)
{
return Channel.Add(a, b); //Exception
}
}
客户端(尝试#2): -- 注意:我添加了一个服务引用,而不是自己创建一个 CalculatorClient(.NET 为我创建了它)。
static void Main(string[] args)
{
Binding binding = new WSHttpBinding("MyConfig");
EndpointAddress remoteAddress = new EndpointAddress(...);
CalculatorClient client = new CalculatorClient(binding, remoteAddress);
int result = client.Add(5, 4); //Exception
}
客户端(尝试 #3): -- 我将其改为 BasicHttpBinding()
static void Main(string[] args)
{
Binding binding = new BasicHttpBinding("MyConfig");
EndpointAddress remoteAddress = new EndpointAddress(...);
CalculatorClient client = new CalculatorClient(binding, remoteAddress);
int result = client.Add(5, 4); //This works!
}
应用程序配置:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="MyConfig" /> <!-- did not add anything to this yet -->
</wsHttpBinding>
</bindings>
</system.serviceModel>
我得到的例外是:内容类型应用程序/soap+xml;服务http://localhost/CalculatorService.svc不支持 charset=utf-8 。客户端和服务绑定可能不匹配。当我在服务器和客户端之间使用共享 dll 文件时,我看不出它们是如何不匹配的。
BasicHttpBinding
作品,只是不是(WSHttpBinding
我什至没有尝试过WS2007HttpBinding
.
异常:[ System.ServiceModel.ProtocolException
] {“内容类型 application/soap+xml;charset=utf-8 不受服务http://localhost/CalculatorService.svc支持。客户端和服务绑定可能不匹配。”} 内部异常:[ System.Net.WebException
]远程服务器返回错误:(415)无法处理消息,因为内容类型'application/soap+xml; charset=utf-8' 不是预期的类型 'text/xml; 字符集=utf-8'..