簡體   English   中英

從主機WinForms程序訪問自托管WCF服務

[英]Accessing a Self-Hosted WCF Service From the Host WinForms Program

我已經創建了一個Windows Forms應用程序來運行WCF服務,該服務基本上將完全由在同一台計算機上運行的單個軟件所消耗,並且我還希望托管應用程序充當將顯示的“儀表板”為了透明起見,有關請求的相關狀態更新。 但是,盡管使用Windows Forms應用程序啟動和停止該服務,但我無法使其以任何其他有意義的方式與其交互。

一些說明/我嘗試過的方法:

  • 只能通過一個程序(可能不是托管程序)訪問該服務。
  • 該服務設置為使用InstanceContextMode.Single,但是ServiceHost對象的SingletonInstance屬性始終為null。
  • 將服務引用添加到其自己的托管服務中會使主機程序無響應(可能不會意外)。

對於模糊性,我深表歉意,但是我基本上是在訪問服務對象,然后再訪問服務本身。 我是否缺少某些東西,或者有完全更好的方法嗎?

謝謝,

GBB

編輯:為清晰起見,我想出了解決方案-將宿主窗口引用設置為服務可訪問的宿主窗口類的靜態成員。

public partial class frmMainWindow : Form
{
    public static frmMainWindow CurrentInstance;
    ServiceHost serviceHost;        

    public frmMainWindow ()
    {
        InitializeComponent();
        CurrentInstance = this;            
    }

    void StartService()
    {
        // service host stuff here for starting TestServer service
    }

    void StopService()
    {
        // stop the service
    }

    // update the status textbox in the host form
    public void SetStatus(string status)
    {
        textStatus.Text = status;
    }



[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class TestServer : ITestServer
{
    frmMainWindow HostWindow = null;

    public TestServer ()
    {
        HostWindow = frmMainWindow .CurrentInstance;
        HostWindow.SetStatus("Service started");
    }

編輯的原始文章中概述的解決方案-將對宿主窗口的引用設置為宿主窗口類的靜態成員。

暫無
暫無

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

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