当运行某些WCF服务的主机进程正在运行时 - 一切正常。 但是当托管进程没有运行时--WCF无法创建对象并使用它。
换句话说 - 我有一个名为“ScrnSrvcR2.exe”的进程,它正在托管通过WCF公开的“ScreensApi”对象。 因此,当“ScrnSrvcR2.exe”进程正在运行时,WCF客户端可以连接并使用“ScreensApi”对象。但是当“ScrnSrvcR2.exe”进程未运行时 - 客户端无法连接。
问题 - 是否可以通过第一个创建“ScreensApi”对象的请求自动启动远程系统(包括 localhost 作为远程系统的特定情况)?< / p>
因此,在这种情况下,当远程系统可以调用 CreateRemoteComObject 函数并且将启动对象托管过程时,我希望从WCF获得某种 DCOM样式功能自动并提供对象的实例。
是否可以使用WCF执行此操作? 怎么样?
以下是一个我用来创建WCF对象的代码:
// this code works fine when "ScrnSrvcR2.exe" is running
protected void InitializeScreens()
{
string endPointAddr = null;
string transport = "net.tcp://";
string hostAddress = "localhost";
string servicePath = "ScreensApi";
// [... optional reading of transport, hostAddress, servicePath values skipped... ]
if (string.IsNullOrEmpty(endPointAddr))
endPointAddr = transport + hostAddress + "/" + servicePath;
System.ServiceModel.Channels.Binding bnd = null;
NetTcpBinding tcpBinding = new NetTcpBinding();
bnd = tcpBinding;
tcpBinding.Name = "epNetTcp"; // this name is defined in config file
EndpointAddress endpointAddress = new EndpointAddress(endPointAddr);
bool isGoodProxy = false;
try
{
try
{
this.ScreensProxy = new ScreensApiClient(bnd, endpointAddress);
string un = null, pw = null;
// [... optional reading of login/password skipped...]
if (!string.IsNullOrEmpty(un) && !string.IsNullOrEmpty(pw))
{
ToLogger(TraceLevel.Verbose, string.Format("Station[ {0} ].Screens => Use credentials: {1}, {2}", this.Name, un, pw));
this.ScreensProxy.ClientCredentials.UserName.UserName = un;
this.ScreensProxy.ClientCredentials.UserName.Password = pw;
}
un = null; pw = null;
// [... optional reading of login/password skipped...]
if (!string.IsNullOrEmpty(un) && !string.IsNullOrEmpty(pw))
{
ToLogger(TraceLevel.Verbose, string.Format("Station[ {0} ].Screens => Use Windows-credentials: {1}, {2}", this.Name, un, pw));
this.ScreensProxy.ClientCredentials.Windows.ClientCredential.UserName = un;
this.ScreensProxy.ClientCredentials.Windows.ClientCredential.Password = pw;
}
this.ScreensProxy.Open();
this.ScreensProxy.Ping(); // note: Ping() is a method of ScreensApi object, just to check if it is alive
this.ScreensStationDescriptor = this.ScreensProxy.CreateStationDescriptor(this.Owner.Name, this.Name, CommonUtils.HostInfoStamp());
this.ScreensProxy.InitializeStation(this.ScreensStationDescriptor);
this.ScreensRuntimeContext = this.ScreensProxy.GetStationContext(this.ScreensStationDescriptor);
isGoodProxy = true;
}
catch (Exception exc)
{
ToLogger(TraceLevel.Error, string.Format("Station[ {0} ].Screens -> {1}\n at {2}", this.Name, ErrorUtils.FormatErrorMsg(exc), exc.StackTrace));
throw;
}
}
finally
{
if (!isGoodProxy)
{
this.ScreensProxy = null;
this.ScreensStationDescriptor = null;
this.ScreensRuntimeContext = null;
ToLogger(TraceLevel.Warning, string.Format("Station[ {0} ].Screens -> Remove ScreensProxy object ref as invalid!", this.Name));
}
}
}
我可能需要添加到我的代码中以使其自动启动WCF对象托管过程?
提前谢谢。