Windows 服务托管 WCF 不接受 GET 请求
Posted
技术标签:
【中文标题】Windows 服务托管 WCF 不接受 GET 请求【英文标题】:Windows Service Hosting WCF Not accepting GET requests 【发布时间】:2018-12-02 22:10:12 【问题描述】:我的 Windows 服务中有以下内容:
protected override void OnStart(string[] args)
if (m_svcHost != null)
m_svcHost.Close();
string strAdrHTTP = "http://localhost:9001/CalcService";
//string strAdrTCP = "net.tcp://localhost:9002/CalcService";
Uri[] adrbase = new Uri(strAdrHTTP) ;
m_svcHost = new ServiceHost(typeof(WCFCalcLib.CalcService), adrbase);
ServiceEndpoint ep = m_svcHost.AddServiceEndpoint(typeof(ICalcService), new WebHttpBinding(), strAdrHTTP);
/*
ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
m_svcHost.Description.Behaviors.Add(mBehave);
BasicHttpBinding httpb = new BasicHttpBinding();
m_svcHost.AddServiceEndpoint(typeof(WCFCalcLib.ICalcService), httpb, strAdrHTTP);
m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
*
* */
/*
WebHttpBinding webHttpBinding = new WebHttpBinding();
webHttpBinding.MaxReceivedMessageSize = 65536 * 2;
webHttpBinding.MaxBufferPoolSize = 2147483647L;
webHttpBinding.MaxBufferSize = 2147483647;
webHttpBinding.MaxReceivedMessageSize = 2147483647L;
m_svcHost.AddServiceEndpoint(typeof(WCFCalcLib.ICalcService), webHttpBinding, strAdrHTTP);
*/
m_svcHost.Open();
当我尝试从浏览器或邮递员应用程序发出GET
请求时,我总是收到以下错误消息:
由于 EndpointDispatcher 的 ContractFilter 不匹配,接收方无法处理带有 Action '' 的消息。这可能是因为合约不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。
我不确定如何让它接受GET
请求。
如果我通过 Visual Studio 启动项目,我可以发出 GET
请求就好了,问题是如果我尝试在它托管在 Windows 服务中时这样做。
没有app.config
- 也许这就是问题发生的原因?
如何让托管在 Windows 服务中的 WCF 接受 GET
请求?
这是我得到的错误:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<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>
</Fault>
我现在改成这样了:
Uri httpBaseAddress = new Uri("http://localhost:9001/CalcService");
m_svcHost = new ServiceHost(typeof(WCFCalcLib.CalcService), httpBaseAddress);
//Add Endpoint to Host
m_svcHost.AddServiceEndpoint(typeof(WCFCalcLib.ICalcService), new WSHttpBinding(), "");
//Metadata Exchange
ServiceMetadataBehavior serviceBehavior = new ServiceMetadataBehavior();
serviceBehavior.HttpGetEnabled = true;
m_svcHost.Description.Behaviors.Add(serviceBehavior);
//Open
m_svcHost.Open();
现在我可以点击它,但仍然不能通过 GET 请求。它显示 wsdl。
谢谢。
【问题讨论】:
除了使用 IIS 还有其他选择吗? 如果我使用 Visual Studio 自带的 WCFClient,我可以访问它,但不能通过浏览器或邮递员访问 【参考方案1】:我按照这里的示例找到了答案,看起来我的 app.config 在其他一些东西中完全丢失了。
https://www.codeproject.com/Tips/1009004/WCF-RESTful-on-Windows-Service-Host
【讨论】:
以上是关于Windows 服务托管 WCF 不接受 GET 请求的主要内容,如果未能解决你的问题,请参考以下文章
想要将 WCF Web 服务作为 Windows 服务托管,而不是在 IIS 中托管