4

I have a c# app using System.ServiceModel.dll. I can run the app locally, but when I try to use Power Shell to run it remotely, it hang:

Here is the simple code to recreate the problem:

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Discovery;

    namespace PowerShellSecurity
    {
        class Program
        {
            static void Main(string[] args)
            {
                var serviceUri = "net.pipe://localhost/Foo/Bar";
                var discoveryUri = new Uri("soap.udp://239.255.255.250:3702/");

                var service = new MyService();

                var serviceHost = new ServiceHost(service, new Uri(serviceUri));
                serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
                serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint(discoveryUri));
                serviceHost.AddServiceEndpoint(typeof(IMyService), new NetNamedPipeBinding(), serviceUri);

                serviceHost.Open();

                Console.WriteLine("It worked!");
            }
        }

        [ServiceContract]
        public interface IMyService
        {
            [OperationContract]
            void DoStuff();
        }

        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
        public class MyService : IMyService
        {
            public void DoStuff()
            {
                throw new NotImplementedException();
            }
        }
    }

I can run it at the localhost and it works. But if I run the following powershell command from another host:

icm -ComputerName myHost -ScriptBlock {iex "& 'c:\Users\me\Documents\Visual Studio 2013\Projects\PowerShellSecurity\PowerShellSecurity\bin\Debug\PowerShellSecurity.exe'"}

I can see the process hanging at myHost using procexp.

Then I used visual studio to attach to this process, I can see it is stuck at: serviceHost.Open();

How can I solve this problem, if I have to use power shell to run the application remotely?

Thank you very much!

SherryRuan
  • 79
  • 3
  • This looks like a service. Don't you want it to hang so that it listens for incoming connections? – Aaron Jensen Aug 05 '15 at 20:43
  • Thanks for reply. No, I think the open() just change the state of a communication object. When I run it locally, it doesn't hang. Those code is part of our application framework. My application does not directly use it, but need it for initialization. – SherryRuan Aug 05 '15 at 21:30
  • Check that no other process is using the IP address/port you want. I think `Open` will hang if the address/port is in use. Check for any zombie services. – Aaron Jensen Aug 05 '15 at 21:38
  • We've run into the same issue and it's because of the NetNamedPipeBinding. For some reason, serviceHost.Open will HANG when the process has been started via remote PowerShell (or via a Cmdlet invoked remotely). We're still investigating and if we find a reason why, we'll post it here. – CMerat Aug 21 '18 at 21:54
  • If you RDC into the machine and double click the .exe does it work? If so, are you logging in with the same credentials you use on your local machine? – Sal Aug 28 '18 at 18:02

1 Answers1

-1

If I'm not mistaken, named pipes are local to a single computer. As far as I know, you can't connect "remotely" using named pipes.

In this case, I'd recommend using a different binding (protocol.) For example, you could use http/https, nettcp, or WSHttp.

RobV8R
  • 1,036
  • 8
  • 16
  • The issue is not with connecting to the named pipe. When the process is started via remote PowerShell (which is similar to SSH in that it runs locally on the machine but is invoked remotely), it hangs and will simply not open the named pipe at all meaning that it is not even available locally. – CMerat Aug 22 '18 at 12:11