0

When the host process with some WCF service is running - all is ok. But when hosting process is not running - WCF is unable to create object and use it.

In other words - I have a process called "ScrnSrvcR2.exe", it is hosting the "ScreensApi" object which is exposed via WCF. So, when "ScrnSrvcR2.exe" process is running WCF clients can connect and use "ScreensApi" object. But when "ScrnSrvcR2.exe" process is not running - clients cannot connect.

The question - is it possible to make remote system (including also localhost as a particular case of remote system) to start that hosting process automatically by a first request to create "ScreensApi" object?

Thus, in this case I want from WCF some kind of DCOM-style functionality when remote system can call the CreateRemoteComObject function and the Object-hosting process would be started automatically and provide an instance of an object.

Is it possible to do it with WCF? How?

Just for a reference below is a piece of code I'm using to create WCF object:

// 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));
        }
    }
}

What I may need to add to my code to make it auto-start WCF-object hosting process automatically?

Thank you in advance.

4

0 回答 0