WCF:如何真正使用 WSHttpBinding?我得到了异常
我创建了一个 WCF 服务:
Shared.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!
}
app.config:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="MyConfig" /> <!-- did not add anything to this yet -->
</wsHttpBinding>
</bindings>
</system.serviceModel>
The exception I get is: Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost/CalculatorService.svc. The client and service bindings may be mismatched. I don't see how they can be mismatched when I use a shared dll file between my server and client. The
BasicHttpBinding
works, just not the WSHttpBinding
(I haven't even attempted 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'..
I created a WCF Service:
Shared.dll:
[ServiceContract(ConfigurationName = "ICalculator")]
public interface ICalculator
{
[OperationContract()]
int Add(int a, int b);
}
Server:
[ServiceBehavior()]
public class Calculator : ICalculator
{
public int Add(int a, int b) { return a + b; }
}
Client (Attempt #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
}
}
Client (Attempt #2): -- Note: I added a Service Reference instead of creating a CalculatorClient myself (.NET created it for me).
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
}
Client (Attempt #3): -- I changed it to be a BasicHttpBinding() instead
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!
}
app.config:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="MyConfig" /> <!-- did not add anything to this yet -->
</wsHttpBinding>
</bindings>
</system.serviceModel>
The exception I get is: Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost/CalculatorService.svc. The client and service bindings may be mismatched. I don't see how they can be mismatched when I use a shared dll file between my server and client. The
BasicHttpBinding
works, just not the WSHttpBinding
(I haven't even attempted WS2007HttpBinding
.Exception: [System.ServiceModel.ProtocolException
]
{"Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost/CalculatorService.svc. The client and service bindings may be mismatched."}
Inner Exception: [System.Net.WebException
]
The remote server returned an error: (415) Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置要在 WSHttpBinding
http://msdn.microsoft.com/en-us/library/ms731884(v=VS.90).aspx
使用示例客户端/服务器 WSHttpBinding 进行更新,默认安全
客户端
服务器
You need to set the security to be used on the WSHttpBinding
http://msdn.microsoft.com/en-us/library/ms731884(v=VS.90).aspx
Updated with Sample Client/Server WSHttpBinding, default security
Client
Server