簡體   English   中英

WinForm中托管的WCF服務無法正常工作

[英]WCF Service hosted in a WinForm is not working

我想在Winforms應用程序中創建一個非常簡單的WebService(WCF)。 為了對此進行測試,我在Winforms應用程序中創建了一個稱為計算器的WCF服務。 另一方面,我創建了一個帶有按鈕的簡單Winforms應用程序,以檢索服務的結果。

我已將Web服務添加為對testproject的引用。 它識別出兩種已實現的方法,一切都很好。 問題是我沒有從Web服務獲得任何響應(凍結我的測試應用程序)。 我很可能忘記了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
}

這就是我打開Web服務的方式。

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

這是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>

如果在Webbrowser中輸入URL( http://localhost:8731/Solution/Calculator/ ),則會顯示以下屏幕:

網站圖片

使用自托管WCF服務,要檢查的主要內容是:

  • 您是否在允許您啟動httplistener的安全性上下文中運行?
  • 您正在使用的端口是否被阻止
  • 配置問題

嘗試使用WCF測試客戶端進行測試。 http://msdn.microsoft.com/zh-CN/library/bb552364(v=vs.110).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM