Windows Service Hosting WCF不接受GET请求

时间:2018-06-23 20:22:51

标签: c# wcf windows-services

我的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不匹配,因此无法在接收方处理带有操作''的消息。这可能是由于合同不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配造成的。检查发送方和接收方具有相同的合同和绑定(包括安全性要求,例如消息,传输,无)。

我不确定如何才能接受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 requeset..it显示wsdl。

谢谢。

1 个答案:

答案 0 :(得分:0)

我通过遵循下面的示例找到了答案,好像我的app.config在其他方面完全丢失了。

this