[英]instantiating multiple WCF services
Suppose you are controlling a set of industrial devices through WCF services. 假设您正在通过WCF服务控制一组工业设备。 Each device will host their own WCF service. 每个设备将托管自己的WCF服务。 Let's call this generic WCF service MyDeviceController
I want to write once and deploy on each device. 让我们将此通用WCF服务MyDeviceController
调用一次,然后在每个设备上进行部署。 However for testing purposes, I also want to deploy all the WCF instances on one local box. 但是,出于测试目的,我还希望将所有WCF实例部署在一个本地机器上。
Given this context, how do you deploy multiple instances of a WCF services both local and multiple boxes? 在这种情况下,如何部署WCF服务的多个实例(本地和多个)?
If I am being too vague, please help me clarify my question. 如果我不太清楚,请帮助我阐明我的问题。 I more than happy to edit it. 我很高兴编辑它。
I wrote an easy to handle service host once: 我曾经写过一个易于处理的服务主机:
public static class SimpleServiceHost<ServiceContract, Service>
{
private static Thread hostThread;
private static object _lockStart = new object();
private static object _lockStop = new object();
public static bool IsRunning { get; set; }
public static void WaitUntilIsStarted()
{
while (!IsRunning)
{
Thread.Sleep(100);
}
}
public static void Start(Binding binding, string host, string path, int port)
{
var serviceUri = new UriBuilder(binding.Scheme, host, port, path).Uri;
Start(binding, serviceUri);
}
public static void Start(Binding binding, Uri serviceUri)
{
lock (_lockStart)
{
if (hostThread == null || !hostThread.IsAlive)
{
hostThread = new System.Threading.Thread(() =>
{
using (ServiceHost internalHost = new ServiceHost(typeof(Service)))
{
internalHost.Faulted += new EventHandler((o, ea) =>
{
IsRunning = false;
throw new InvalidOperationException("The host is in the faulted state!");
});
internalHost.AddServiceEndpoint(typeof(ServiceContract), binding, serviceUri);
try
{
internalHost.Open();
IsRunning = true;
}
catch
{
IsRunning = false;
}
while (true)
Thread.Sleep(100);
}
});
}
hostThread.Start();
}
}
public static void Stop()
{
lock (_lockStart)
{
lock (_lockStop)
{
hostThread.Abort();
IsRunning = false;
}
}
}
}
So if you are willing to use multiple contracts you can just call like this: 因此,如果您愿意使用多个合同,则可以这样调用:
SimpleServiceHost<IService, Service>.Start(new BasicHttpBinding(), "localhost", "TestService", 8086);
Otherwise make my service host work like a factory and return instances when calling start. 否则,使我的服务主机像工厂一样工作,并在调用start时返回实例。 You probably will need to enhance the class to fulfill your requirements. 您可能需要增强课程来满足您的要求。 But you save a lot of code especially when you need to dynamically host several services ;-) 但是您节省了大量代码,尤其是当您需要动态托管多个服务时;-)
Regards Jan 问候Jan
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.