简体   繁体   中英

Can I get/set data into WCF Service by Host?

I have WCF Service hosted on WindowsServiceHost (to communicate WindowsFormsApp <> WindowsServiceHost)

Is there any way get data from WCFService to WindowsServiceHost? And in other way (set data from WindowsServiceHost to WCFService)

That is what have i done:

  1. I've made a project of WCF Service Library, implemented interface, contracts etc.
  2. I created new project - Windows service and added reference to project from #1 and to System.ServiceModel
  3. Configured app.conf:

     <system.serviceModel> <bindings> <netTcpBinding> <binding name="netTcp"> <security mode="Message"> </security> </binding> </netTcpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="mexBehavior"> <serviceMetadata httpGetEnabled="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="mexBehavior" name="KSPDJOBWinWCFService.KSPDJOBWinWCFService" > <endpoint address="KSPDJOBWinWCFService" binding="netTcpBinding" contract="KSPDJOBWinWCFService.IKSPDJOBWinWCFService" bindingConfiguration="netTcp" /> <host> <baseAddresses> <add baseAddress="http://localhost:8079"/> <add baseAddress="net.tcp://localhost:8090"/> </baseAddresses> </host> </service> </services> 

  4. I've hosted the WCF in OnStart method of Windows Service

     protected override void OnStart(string[] args) { host = new ServiceHost(typeof(KSPDJOBWinWCFService.KSPDJOBWinWCFService)); host.Open(); } 
  5. Added new solution with WinformsClient app (as WCF Client) and tested communication - all working fine.

  6. The problem is when i send a value from WinFormsClient to WCF Service, and want to read it from Windows Service aplication

Thanks for any Help.

You could hold the WCF service instance in a global variable and work with events. In this sample the WCF Service KSPDJOBWinWCFService exposes an event EventA and the Service Host will handle it. This is the place where you can process the values sent by your WCF Client.

public partial class Service : ServiceBase
{
    private ServiceHost _host;
    private KSPDJOBWinWCFService _instance;

    protected override void OnStart(string[] args)
    {
        try
        {
            _instance = new KSPDJOBWinWCFService();
            _instance.EventA += HandleEventA;
            _host = new ServiceHost(_instance);
            _host.Open();
        }
        catch (Exception ex)
        {
            // Logging
        }
    }

    public void HandleEventA(object sender, CustomEventArgs e)
    {
        // do whatever you want here
        var localVar = e.Value;
    }

    protected override void OnStop()
    {
        try
        {
            if (_instance != null)
            {
                _instance.Dispose();
            }
            _host.Close();
        }
        catch (Exception ex)
        {
            // Logging
        }
    }
}

The WCF Service then fires this event together with the values sent from the WCF client:

public class KSPDJOBWinWCFService : IKSPDJOBWinWCFService
{
    public event EventHandler<CustomEventArgs> EventA;

    public bool SomeWcfOperation(int value)
    {
        EventA?.Invoke(this, new CustomEventArgs(value));

        return true;
    }
}

Create event args that fulfill your needs:

public class CustomEventArgs : EventArgs
{
    public int Value { get; set; }

    public CustomEventArgs(int value)
    {
        Value = value;
    }
}

You can also expose values with public properties in your WCF Service. But events are also necessary.

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