0

[元の形式] ( http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication )から少し変更した次の WCF サーバーとクライアント コードがあります。

サーバ:

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

私の質問は:

クライアントコードからサーバーを終了するより良い方法はありますか? 上記でそれを殺している方法に欠陥や問題はありますか?

ありがとう!

4

1 に答える 1

2

サービスインスタンスを直接強制終了するのではなく、提供したコメントに基づいて; あなたはただあなたのサービスをマークするかもしれませんInstanceContextMode=PerCallそしてConcurrencyMode=Multiple。このように、すべてのリクエストに対して新しいインスタンスが作成され、このインスタンスはWCFホストによって割り当てられたスレッドで実行されます。

実行中に、操作が実行されているスレッドが変更される可能性があることに注意してください。

于 2013-02-13T14:21:25.173 に答える