如何从也是WCF服务器父级的客户端结束WCF服务器

本文关键字:服务器 WCF 客户端 结束 | 更新日期: 2023-09-27 17:59:00

我有以下WCF服务器和客户端代码,我对它们的[原始形式]做了一些更改(http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication)。

服务器:

using System;
using System.ServiceModel;
namespace WCFServer
{
    [ServiceContract]
    public interface IStringReverser
    {
        [OperationContract]
        bool ReverseString(string value, out string reversed);
    }
    public class StringReverser : IStringReverser
    {
        public bool ReverseString(string value, out string reversed)
        {
            char[] retVal = value.ToCharArray();
            int idx = 0;
            for (int i = value.Length - 1; i >= 0; i--)
                retVal[idx++] = value[i];
            reversed = new string(retVal);
            return true;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            using (var host = new ServiceHost(typeof(StringReverser), new[]{new Uri("net.pipe://localhost")}))
            {
                host.AddServiceEndpoint(typeof(IStringReverser),
                  new NetNamedPipeBinding(),
                  "PipeReverse" + args[0]);
                host.Open();
                while (true)
                {
                }
            }
        }
    }
}

客户:

using System;
using System.Diagnostics;
using System.Globalization;
using System.ServiceModel;
namespace WCFClient
{
    [ServiceContract]
    public interface IStringReverser
    {
        [OperationContract]
        bool ReverseString(string value, out string reversed);
    }
    class Program
    {
        static void Main()
        {
            var currentThreadIdStr = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture);
            // Start off the service
            Process server = new Process();
            server.StartInfo.FileName = "WCFServer.exe";
            server.StartInfo.Arguments = currentThreadIdStr;
            server.StartInfo.UseShellExecute = false;
            server.Start();
            // Try to connect to the server
            ChannelFactory<IStringReverser> pipeFactory =
              new ChannelFactory<IStringReverser>(
                new NetNamedPipeBinding(),
                new EndpointAddress(
                  "net.pipe://localhost/PipeReverse" + System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture)));
            IStringReverser pipeProxy =
              pipeFactory.CreateChannel();
            Console.WriteLine("type '"quit'" to exit'n");
            string str;
            string reversedString;
            while ( (str = Console.ReadLine()) != "quit")
            {
                bool wasReversedSuccesfully = pipeProxy.ReverseString(str, out reversedString);
                Console.WriteLine("pipe: Succesful: " + wasReversedSuccesfully + "  reverse of " + str + " is:" + reversedString);
            }
            //// Kill the service
            server.Kill();
        }
    }
}

我的问题是:

有没有更好的方法从客户端代码结束服务器?我在上面杀死它的方式有什么缺陷或问题吗?

谢谢!

如何从也是WCF服务器父级的客户端结束WCF服务器

基于您提供的注释,而不是直接杀死服务实例;您可以将您的服务标记为InstanceContextMode=PerCallConcurrencyMode=Multiple。这样,对于每个请求,都将创建一个新实例,并且该实例将在WCF主机分配的线程上运行。

请注意,在执行过程中,正在执行操作的线程可能会发生更改。