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

C# 我可以通过主机将数据获取/设置到WCF服务中吗?,c#,wcf,C#,Wcf,我在WindowsServiceHost上托管了WCF服务(用于与WindowsFormsApp WindowsServiceHost通信) 有没有办法将数据从WCFService获取到WindowsServiceHost? 以其他方式(将数据从WindowsServiceHost设置为WCFService) 这就是我所做的: 我做了一个WCF服务库的项目,实现了接口,合同等 我创建了新的项目-Windows服务,并将对项目的引用从#1添加到System.ServiceModel 配置的app.

我在WindowsServiceHost上托管了WCF服务(用于与WindowsFormsApp WindowsServiceHost通信)

有没有办法将数据从WCFService获取到WindowsServiceHost? 以其他方式(将数据从WindowsServiceHost设置为WCFService)

这就是我所做的:

  • 我做了一个WCF服务库的项目,实现了接口,合同等
  • 我创建了新的项目-Windows服务,并将对项目的引用从#1添加到System.ServiceModel
  • 配置的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>
    
  • 使用WinformsClient应用程序(作为WCF客户端)添加了新的解决方案,并测试了通信-一切正常

  • 问题是当我从WinFormsClient向WCF服务发送一个值,并希望从Windows服务应用程序读取该值时


  • 感谢您的帮助。

    您可以将WCF服务实例保存在全局变量中并处理事件。在此示例中,WCF服务
    KSPDJOBWinWCFService
    公开事件
    EventA
    ,服务主机将处理该事件。这是您可以处理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
            }
        }
    }
    
    然后,WCF服务将触发此事件以及从WCF客户端发送的值:

    public class KSPDJOBWinWCFService : IKSPDJOBWinWCFService
    {
        public event EventHandler<CustomEventArgs> EventA;
    
        public bool SomeWcfOperation(int value)
        {
            EventA?.Invoke(this, new CustomEventArgs(value));
    
            return true;
        }
    }
    

    您还可以在WCF服务中公开具有公共属性的值。但事件也是必要的。

    您是否将WCF服务托管在已实现的Windows服务(另一个由
    ServiceBase
    派生的服务)中?您希望在这些服务之间交换数据,对吗?如果是这样的话,你能把代码粘贴到你的WCF服务是如何托管的吗?嗨,马克,我回答说你工作得很辛苦:))谢谢你马克的精彩教程。我必须在我的WCFService类下面添加
    [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    :公共类KSPDJOBWinWCFService:IKSPDJOBWinWCFService
    public class KSPDJOBWinWCFService : IKSPDJOBWinWCFService
    {
        public event EventHandler<CustomEventArgs> EventA;
    
        public bool SomeWcfOperation(int value)
        {
            EventA?.Invoke(this, new CustomEventArgs(value));
    
            return true;
        }
    }
    
    public class CustomEventArgs : EventArgs
    {
        public int Value { get; set; }
    
        public CustomEventArgs(int value)
        {
            Value = value;
        }
    }