0

私は 2 つの単純なコンソール アプリケーションを作成しました。1 つは wcf サービスをホストし、もう 1 つは wcf サービス クライアントです。

1 台のマシン (ホストとクライアント) で両方のアプリケーションを実行すると問題なく動作しますが、クライアントまたはホストを別のマシンに移動EndpintNotFountExceptionしようとすると、ホストに接続しようとすると発生します。

これは私のサービスのコードです:

namespace ConsoleApplication7
{
    using System.ServiceModel;

    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        void Register(string name);
    }
}

そして実装クラス:

namespace ConsoleApplication7
{
    public class SimpleService : ISimpleService
    {
        public void Register(string name)
        {
            DataBase.Save(name);
        }
    }
}

ご覧のとおり、非常にシンプルなサービスです:)

これはホスト プログラムのコードです。

using System;
using System.ServiceModel;

class Program
{
    static void Main(string[] args)
    {
        var service = new ServiceHost(typeof(SimpleService));
        service.Open();

        while (true)
        {
            Console.ReadLine();
            foreach (var s in DataBase.Get())
            {
                Console.WriteLine(s);
            }
        }
    }
}

サービスを開き、Enter キーを押すと、データベースの内容が出力されます。私はそれが他のものを持っていないことを知っていますがClose()、それは私の問題を示すためだけです.

app.configファイルは次のとおりです。

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name ="myBinding">
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
    <client />
    <services>
      <service name="ConsoleApplication7.SimpleService" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="myBinding" contract="ConsoleApplication7.ISimpleService"/>
        <endpoint address="" binding="basicHttpBinding" contract="ConsoleApplication7.ISimpleService"/>
        <endpoint address ="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:9101"/>
            <add baseAddress="http://localhost:9102"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior" >
          <serviceMetadata httpGetEnabled="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

クライアントコード:

using System;
using System.ServiceModel;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Click to open");
        Console.ReadLine();

        try
        {
            var service = new ServiceReference1.SimpleServiceClient(new BasicHttpBinding(), new EndpointAddress("http://192.168.1.11:9102/"));
            service.Open();
            service.Register("itsMe");
        }
        catch (Exception  e)
        {
            Console.WriteLine(e);
        }

        Console.ReadLine();
    }
}

クライアントapp.configはビジュアルスタジオがAddServiceReferenceオプションで作成。

と の両方をベース アドレスとしてapp.config試したファイルをホストします。http://localhost:9102http://192.168.1.11:9102

1 台のマシンでホスト アプリケーションとクライアント アプリケーションを開くと、問題なく動作します。それらのアプリの1つを他のマシンに移動するとSystem.EndpointNotFoundException、実行時に戻りますclient.Open()

私が間違っているかもしれないアイデアはありますか?私はnet.tcpバインディングを試してみましたが、同じ結果になりました。

両方のマシンで Windows ファイアウォールが無効になっている

4

1 に答える 1

0

クライアントまたはサービスを別のマシンに移動するときにサービスアドレスを変更することについては言及していません。使用しているアドレスはlocalhost:xxxx、プログラムが実行されているマシンでのみ認識されます。たとえば、サービスが Machine1 にあり、クライアントを Machine2 に移動した場合、クライアントはサービスをそのままでは見ることができません。 Machine2 にあると想定します。ルーターアドレスのように見えますが、おそらく IP アドレスで同様の問題が発生しています。

マシン名 (たとえば、http://machine1:9102) を使用するようにサービスの構成ファイルを更新し、そのアドレスを使用してクライアントからサービスにアクセスすることをお勧めします。例えば:

var service = new ServiceReference1.SimpleServiceClient(new BasicHttpBinding(), new EndpointAddress("http://machine1:9102/"));
于 2013-02-18T06:33:22.257 に答える