简体   繁体   English

我可以通过主机获取/设置数据到WCF服务吗?

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

I have WCF Service hosted on WindowsServiceHost (to communicate WindowsFormsApp <> WindowsServiceHost) 我在WindowsServiceHost上托管了WCF服务(以传达WindowsFormsApp <> WindowsServiceHost)

Is there any way get data from WCFService to WindowsServiceHost? 有没有办法从WCFService获取数据到WindowsServiceHost? And in other way (set data from WindowsServiceHost to WCFService) 以其他方式(将数据从WindowsServiceHost设置为WCFService)

That is what have i done: 这就是我所做的:

  1. I've made a project of WCF Service Library, implemented interface, contracts etc. 我做了一个WCF服务库项目,实现了接口,合同等。
  2. I created new project - Windows service and added reference to project from #1 and to System.ServiceModel 我创建了新项目--Windows服务,并添加了对#1和System.ServiceModel项目的引用
  3. Configured app.conf: 配置的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 我在Windows服务的OnStart方法中托管了WCF

     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. 使用WinformsClient应用程序(作为WCF客户端)添加了新的解决方案并测试了通信 - 一切正常。

  6. The problem is when i send a value from WinFormsClient to WCF Service, and want to read it from Windows Service aplication 问题是当我从WinFormsClient向WCF服务发送一个值,并希望从Windows服务应用程序中读取它

Thanks for any Help. 谢谢你的帮助。

You could hold the WCF service instance in a global variable and work with events. 您可以将WCF服务实例保存在全局变量中并使用事件。 In this sample the WCF Service KSPDJOBWinWCFService exposes an event EventA and the Service Host will handle it. 在此示例中,WCF服务KSPDJOBWinWCFService公开事件EventA ,服务主机将处理它。 This is the place where you can process the values sent by your WCF Client. 您可以在此处理WCF客户端发送的值。

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: 然后,WCF服务将与WCF客户端发送的值一起触发此事件:

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. 您还可以在WCF服务中公开具有公共属性的值。 But events are also necessary. 但事件也是必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM