2013-10-03 11 views
0

Winform應用程序主機WCF服務,從客戶端接收串簡單的方法,其他類的這種方法,開闢新instane打開過程和做的東西:如何從WCF服務事件每個會話回到我的主要形式

namespace ServiceLibrary 

    [ServiceContract()] 
    public interface IService1 
    { 
     [OperationContract] 
     string startProcess(string str); 
    } 


[ServiceBehavior(
    ConcurrencyMode = ConcurrencyMode.Multiple, 
    InstanceContextMode = InstanceContextMode.PerSession)] 

public class service1 : IService1 
{ 
     public string startProcess(string str) 
     { 
      Jo job = new job(); 
      job.Event += job_Event; 
      job.Start(str); 
     } 

} 

裏面Job類我有事件與我所有的類屬性引發的事件(名稱,大小等)

public delegate void StartEventHandler(Job obj); 
public event StartEventHandler Event; 

,並從我服務的IM也subscride這一事件,並從這個事件我想Ť Ø發送到我的主要形式這個對象,以uodate我的UI:

job.Event += job_Event; 

    public void job_Event(Job obj) 
    { 
     // Send to to my main form and update UI 
    } 

我的問題是,因爲我ServiceBehavior是ConcurrencyMode.Multiple,而不是Single我有我的服務的幾次會議,我不知道如何提出一個事件在我的形式。

這是我建立了我的服務從我的主要形式有:

urlService = "net.tcp://" + ipAddress.ToString() + ":8000/MyService"; 
ServiceHost new ServiceHost(typeof(ServiceLibrary.service1)); 

        NetTcpBinding tcpBinding = new NetTcpBinding(); 
        tcpBinding.TransactionFlow = false; 
        tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign; 
        tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows; 
        tcpBinding.Security.Mode = SecurityMode.None; 
host.AddServiceEndpoint(typeof(ServiceLibrary.IService1), tcpBinding, urlService); 
        ServiceMetadataBehavior metadataBehavior; 
        metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>(); 
        if (metadataBehavior == null) 
        { 
         // Create the proxy object that is generated via the svcutil.exe tool 
         metadataBehavior = new ServiceMetadataBehavior(); 
         metadataBehavior.HttpGetUrl = new Uri("http://" + _ipAddress.ToString() + ":8001/MyService"); 
         metadataBehavior.HttpGetEnabled = true; 
         metadataBehavior.ToString(); 
         host.Description.Behaviors.Add(metadataBehavior); 
         urlMeta = metadataBehavior.HttpGetUrl.ToString(); 
        } 

        host.Open(); 

回答

1

您將需要靜態函數的類來訪問你的形式。那樣的事情呢?

public class MyFormFunctions 
{ 
    public static job_Event(Job obj) 
    { 
     /// do something to form 
    } 
} 
     public string startProcess(string str) 
     { 
      Job job = new job();    
      MyFormFunctions.job_Event(job); 

     }