简体   繁体   中英

Windows Service Hosting WCF Not accepting GET requests

I have the following in my windows service:

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();
}

When I try to make a GET request from a browser or from the postman application I always get the following error message:

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, eg Message, Transport, None).

I'm not sure how I can get it to accept GET requests.

If I start the project through Visual Studio, I'm able to make a GET request just fine, the problem is if I try to do it when it is hosted in the Windows service.

There is no app.config - maybe this is why the problem is happening?

How can I get the WCF that is hosted in the Windows service accept GET requests?

This is the error I 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>

I have now changed it to this:

    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();

Now I'm able to hit it but not via a GET requeset still..its showing the wsdl.

Thanks.

I found my answer by following the example here looks like my app.config was missing entirely among a few other things.

https://www.codeproject.com/Tips/1009004/WCF-RESTful-on-Windows-Service-Host

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM