我有一个由Windows服务包装的WCF dll(使用net.tcp绑定),我希望在可以从dll中访问的OnStart期间加载服务中的全局数据。这可能很简单,但我没有看到它。有什么建议?
namespace MyApp
{
[ServiceContract]
public interface IMyApp
{
[OperationContract]
bool Export(MyObj obj); // omitted definition of myObj for brevity
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
class MyWCF : IMyApp
{
public bool Export(MyObj obj)
{
bool bResult;
string sExportPath = GlobalObject.GetEnvironmentPath(obj.Env); //GlobalObject class is defined within this namespace
bResult = RetrieveDocument(obj.ID, sExportPath);
return bResult;
}
}
}
------------------------- windows service ------------------------------
using MyApp;
namespace MyService
{
public partial class MyService: ServiceBase
{
internal static ServiceHost objServiceHost = null;
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (objServiceHost != null)
objServiceHost.Close();
bServiceStopped = false;
objServiceHost = new ServiceHost(typeof(MyWCF));
objServiceHost.Open();
Trace.TraceInformation("Load globaldata");
if (!GlobalObject.LoadGlobalData()))
{
Log("Failure attempting to Load Global Data...");
var controller = new System.ServiceProcess.ServiceController("MyService");
controller.Stop();
}
}
}
}