简体   繁体   中英

Cannot use powershell to remotely start WCFapplication (System.ServiceModel)

I have ac# 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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM