简体   繁体   中英

WCF Service hosted in a WinForm is not working

I want to create a very simple WebService (WCF) inside a Winforms application. To test this I created a WCF Service called Calculator inside my Winforms application. On the other side I created a simple Winforms application with a button to retrieve the Results of the service.

I have added the Webservice as a reference to the testproject. It recognizes the two implemented methods and everything just fine. The problem is that I get no response from the webservice (freezing my testapplication). I have most likely forgot something on the Webservice application. What could it be?

Here's the webservice:

 [ServiceContract] public interface ICalculator { [OperationContract] double AddNumbers(double number1, double number2); [OperationContract] string TestMethod(); 

}

public class Calculator : ICalculator
{
    #region ICalculator Member

    public double AddNumbers(double number1, double number2)
    {
        return number1 + number2;
    }

    public string TestMethod()
    {
        return "HELLO WORLD!";
    }

    #endregion
}

This is how I open the Webservice.

this.m_WebService = new ServiceHost(typeof(Calculator));
this.m_WebService.Open();

Here is the App.config

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Solution.Server.CalculatorBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="Solution.Server.CalculatorBehavior"
        name="Solution.Server.Calculator">
        <endpoint address="" binding="wsHttpBinding" contract="Solution.Server.ICalculator">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8731/Solution/Calculator/" />
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>

If I enter the URL ( http://localhost:8731/Solution/Calculator/ ) in the Webbrowser I get this screen:

网站图片

With self hosting WCF services, the main Things to check are:

  • Are you running in a Security context that allows you to start a httplistener?
  • Are the ports that you are using blocked
  • Configuration problem

Try testing With the WCF Test Client. http://msdn.microsoft.com/en-us/library/bb552364(v=vs.110).aspx

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