如何使用浏览器探索WCF方法?

时间:2013-01-17 22:34:36

标签: c# .net wcf webhttpbinding

我有一个net.tcp服务,我想让其他平台(特别是PHP)可以访问它。为此,我使用http绑定。

我用:

创建一个http端点
ServiceHost svh = new ServiceHost(typeof(MyService));
var httpLocation = "http://" + address + ":4041";
svh.AddServiceEndpoint(typeof(IMyService), new WebHttpBinding(WebHttpSecurityMode.None), 
                                   httpLocation);

svh.AddServiceEndpoint(
                    ServiceMetadataBehavior.MexContractName,
                    MetadataExchangeBindings.CreateMexHttpBinding(),
                    httpLocation + "/mex"
                    );

svh.Open();

现在,当我试图通过访问http://localhost:4041来浏览服务时,我得到了:

<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none">
<Code>
<Value>Sender</Value>
<Subcode>
<Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</Value>
</Subcode>
</Code>
<Reason>
<Text xml:lang="en-US">
The message with Action '' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver. Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).
</Text>
</Reason>

我做错了什么?

2 个答案:

答案 0 :(得分:1)

浏览时,请确保包含.svc.asmx的完整路径以及尾随?wsdl

localhost:4041/ServiceVirtualDirectory/Service.svc?wsdl

答案 1 :(得分:1)

您是否尝试过访问http://localhost:4041/MyService.svc?wsdl

不确定您的服务是否符合REST设计,这就是您使用WebHttpBinding的原因。 如果您的服务实际上是REST-ful,那么它的格式就是 http://localhost:4041/MyService.svc/MyMethod/MyData您应该在浏览器上看到结果。基于this ASP.NET线程,您需要附加WebHttpBehavior

如果您的服务不是REST-ful,您应该根据您的要求考虑BasicHttpBinding(使用常规SOAP消息)或其表兄弟(参见this SO线程)。

希望这有帮助。