c# - 如何从应用程序访问Windows服务的属性

标签 c# asp.net windows-services

我有一个 Windows 服务,它包含返回类型 DataTable 的属性

public partial class ServiceForCount : ServiceBase
{
    public DataTable AllData
    {
        get;
        set;
    }
    public ServiceForCount()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
    }
    protected override void OnStop()
    {
    }
    protected override void OnCustomCommand(int sizeOfFile)
    {
        //base.OnCustomCommand(command);
    }
}

如果我想从我的应用程序中调用事件,我可以这样调用

   ServiceController sc = new ServiceController("ServiceForCount");
  sc.ExecuteCommand(size);

我想将 DataTable 放入我的应用程序中。我该怎么做。

最佳答案

你不能用 ServiceController.ExecuteCommand 做到这一点.这只是一种方式,它的目的不是那样。

您可以通过在 Windows 服务中托管 Web 服务(WCF 自托管、WebApi OWIN 自托管)或使用远程处理来实现。 Web 服务需要公开的只是一个返回对象列表而不是数据表的方法。因此,您需要将 DataTable 转换为 IEnumerable<DataObject>其中 DataObject是您的服务契约(Contract)。

你可以先看看这些:

  1. WebApi 2 Self Hosted Web Services .
  2. How to host a WCF Service in a managed application .

关于c# - 如何从应用程序访问Windows服务的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25030213/

相关文章:

c# - Windows 服务中的 SQL 连接

c# - 数据库已锁定 C# 的 SQLite dll

c# - 字符串连接如何忽略空值和换行符

ASP.NET - ActionResult 参数在传递字符串时总是返回 null - 为什么?

javascript - Google map 在 .html 页面中工作正常,但在 ASP.NET 页面中不行

c# - 进程终止时的 IDisposable 代码

c# - Json转换 : How to ignore Integer types where the value is a specific number

c# - CRM 创建 CrmConnection 的新实例

asp.net - 使用 Web Deploy 部署 .NET 4.5 Web 应用程序

vb6 - TreeView 控件 (comctl32.ocx) 不加载触摸屏服务