Wcf 我们可以作为Windows服务承载工作流服务吗?
我正在处理一个日志应用程序,它要求我拥有一个作为服务公开的工作流(工作流服务)。我们希望将其作为Windows服务宿主(不希望在IIS中作为.svc文件宿主工作流服务)。将其作为windows服务的另一个原因是能够通过命名管道与服务通信Wcf 我们可以作为Windows服务承载工作流服务吗?,wcf,workflow,named-pipes,Wcf,Workflow,Named Pipes,我正在处理一个日志应用程序,它要求我拥有一个作为服务公开的工作流(工作流服务)。我们希望将其作为Windows服务宿主(不希望在IIS中作为.svc文件宿主工作流服务)。将其作为windows服务的另一个原因是能够通过命名管道与服务通信 我们可以通过命名管道公开工作流服务而不在IIS中托管它吗 是的,这是可能的。您必须创建自己的服务。请参阅MSDN,特别是Windows服务中的主机部分。是的,当然可以。至少,我已经在Workflow4候选版本中完成了同样多的工作 考虑一下 // a generi
我们可以通过命名管道公开工作流服务而不在IIS中托管它吗 是的,这是可能的。您必须创建自己的服务。请参阅MSDN,特别是Windows服务中的主机部分。是的,当然可以。至少,我已经在Workflow4候选版本中完成了同样多的工作 考虑一下
// a generic self-hosted workflow service hosting thingy. Actual
// implementation should contain more logging and thread safety, this
// is an abbreviated version ;)
public class WorkflowHost
{
// NOTE: with Workflow, it helps to maintain a concept of
// Workflow definition [the Activity or WorkflowService from
// a designer] and a Workflow instance [what is running within
// WorkflowInvoker, WorkflowApplication, WorkflowServiceHost].
// a definition may be used to generate an instance. an instance
// contains run-time state and cannot be recycled into a new
// instance. therefore, to repeatedly re-host a WorkflowService
// we need to maintain references to original definitions and
// actual instances. ergo services and hosts maps
//
// if you are special purpose and require support for one and
// only one service and endpoint\uri, then you may reduce this
// to a simple tuple of Uri, WorkflowService, WorkflowServiceHost
// services represents a definition of hosted services
private readonly Dictionary<Uri, WorkflowService> _services =
new Dictionary<Uri, WorkflowService> ();
// hosts represents actual running instances of services
private readonly Dictionary<Uri, WorkflowServiceHost> _hosts =
new Dictionary<Uri, WorkflowServiceHost> ();
// constructor accepts a map of Uris (ie service endpoints) to
// workflow service definitions
public WorkflowHost (IDictionary<Uri, WorkflowService> services)
{
foreach (KeyValuePair<Uri, WorkflowService> servicePair in services)
{
_services.Add (servicePair.Key, servicePair.Value);
}
}
// have your windows service invoke this to start hosting
public void Start ()
{
if (_hosts.Count > 0)
{
Stop ();
}
foreach (KeyValuePair<Uri, WorkflowService> servicePair in _services)
{
WorkflowService service = servicePair.Value;
Uri uri = servicePair.Key;
WorkflowServiceHost host = new WorkflowServiceHost (service, uri);
host.Open ();
_hosts.Add (uri, host);
}
}
// have your windows service invoke this to stop hosting
public void Stop ()
{
if (_hosts.Count > 0)
{
foreach (KeyValuePair<Uri, WorkflowService> servicePair in
_services)
{
WorkflowService service = servicePair.Value;
Uri uri = servicePair.Key;
IDisposable host = _hosts[uri];
host.Dispose ();
}
_hosts.Clear ();
}
}
}
谢谢约翰尼的详细回复。我会试着看看它是否能带我去某个地方。我知道有一种方法可以将WCF公开为windows服务。我的具体问题与作为WCF公开并作为Windows服务托管的工作流有关。逻辑上的假设是,一旦您手头有了WCF(无论其类型如何),您就应该能够将其作为windows服务托管。我将尝试这种方法,以发现它符合我的要求。不用担心,但仔细看看。提供的解决方案==WindowsService+WF+WCF,这不是您所需要的吗?确认,在示例代码之间添加了更多上下文。这是如何通过WCF演示WindowsService托管的WorkflowServices的,是否更清楚?很好。你能把你的应用程序放在某个地方吗。那将是很大的帮助。谢谢你的回复。我有这个想法。我主要关心的是,如果我尝试使用Windows服务托管基于工作流的WCF服务,它是否会出错、中断、抛出异常。一旦我能做到这一点,我将分享我的经验。@arsayed嘿,我知道这是7年后的事了,但你能做到吗?
// windows service. personally i would abstract service behind
// an interface and inject it, but again, for brevity ;)
public partial class WorkflowWindowsService : ServiceBase
{
WorkflowHost _host;
public WorkflowWindowsService ()
{
InitializeComponent();
Dictionary<Uri, WorkflowService> services =
new Dictionary<Uri, WorkflowService> ();
// do your service loading ...
// create host
_host = new WorkflowHost (services);
}
protected override void OnStart(string[] args)
{
_host.Start ();
}
protected override void OnStop()
{
_host.Stop ();
}
}
// converts a string value [either pure xaml or filename] to a
// WorkflowService definition
public WorkflowService ToWorkflowService (string value)
{
WorkflowService service = null;
// 1. assume value is Xaml
string xaml = value;
// 2. if value is file path,
if (File.Exists (value))
{
// 2a. read contents to xaml
xaml = File.ReadAllText (value);
}
// 3. build service
using (StringReader xamlReader = new StringReader (xaml))
{
object untypedService = null;
// NOTE: XamlServices, NOT ActivityXamlServices
untypedService = XamlServices.Load (xamlReader);
if (untypedService is WorkflowService)
{
service = (WorkflowService)(untypedService);
}
else
{
throw new ArgumentException (
string.Format (
"Unexpected error reading WorkflowService from " +
"value [{0}] and Xaml [{1}]. Xaml does not define a " +
"WorkflowService, but an instance of [{2}].",
value,
xaml,
untypedService.GetType ()));
}
}
return service;
}