1

基本的に、MSDN のコードを持っています。

コードは次のとおりです。

using System;
using System.ServiceModel;

// This code generated by svcutil.exe.
[ServiceContract()]
interface IMath
{
     [OperationContract()]
     double Add(double A, double B);
}

public class Math : IMath
{
public double Add(double A, double B) 
{
    return A + B;
}
}

public sealed class Test
{
    static void Main()
   {
       // Code not shown.
   }

public void Run()
{
    // This code is written by an application developer.
    // Create a channel factory.
    BasicHttpBinding myBinding = new BasicHttpBinding();

    EndpointAddress myEndpoint = new EndpointAddress("http://localhost/MathService/Ep1");

    ChannelFactory<IMath> myChannelFactory = new ChannelFactory<IMath>(myBinding, myEndpoint);

    // Create a channel.
    IMath wcfClient1 = myChannelFactory.CreateChannel();
    double s = wcfClient1.Add(3, 39);
    Console.WriteLine(s.ToString());
((IClientChannel)wcfClient1).Close();

    // Create another channel.
    IMath wcfClient2 = myChannelFactory.CreateChannel();
    s = wcfClient2.Add(15, 27);
    Console.WriteLine(s.ToString());
((IClientChannel)wcfClient2).Close();
myChannelFactory.Close();
}
}

ただし、私の表面的な理解に基づくと、それはセルフホスト WCF です。上記のコードは、サービス コードとクライアント コードを組み合わせています。WCF がサーバー内のホストである場合、その内部構造はまったくわかりません。次に、クライアント側でそれを消費する方法は? コードを使用しました

ChannelFactory<IMath> myChannelFactory = new ChannelFactory<IMath>(myBinding, myEndpoint);

しかし、インテリセンスはIMathをまったく知りません。私はプロキシ、ChannelFactory などに強くありません。私の質問は、サービス IMath がhttp://www.someserver/IMath.svcでホストされている場合、クライアント側でコードを記述してそれを使用する方法ですか?

クライアントに Web 参照を追加するとは思わないでください...

更新: wsdl: サービスには次のようなものがあります:

<wsdl:binding name="BasicHttpBinding_iMath" type="tns:iMath">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" /> 
 - <wsdl:operation name="Add">
 <soap:operation soapAction="http://tempuri.org/iMath/add" style="document" /> 
 - <wsdl:input>
<soap:body use="literal" /> 
 </wsdl:input>
- <wsdl:output>
 <soap:body use="literal" /> 
  </wsdl:output>
  </wsdl:operation>
- <wsdl:operation name="LoadUnbillsFromOrion">
  <soap:operation soapAction="http://tempuri.org/iMath/LoadUnbillsFromOrion" style="document" /> 
- <wsdl:input>
  <soap:body use="literal" /> 
  </wsdl:input>
- <wsdl:output>
   <soap:body use="literal" /> 
   </wsdl:output>
   </wsdl:operation>
  </wsdl:binding>
 - <wsdl:service name="Math">
 - <wsdl:port name="BasicHttpBinding_iMath" binding="tns:BasicHttpBinding_iMath">
   <soap:address location="http://wsvc01/Imath.svc" /> 
  </wsdl:port>
 </wsdl:service>
4

1 に答える 1

1

基本的に、知っておく必要があるのは、クライアント側のバインディングとエンドポイント アドレスだけです。

WCF サービスを使用するには、プロキシを作成する必要があります。ChannelFactory に特に理由が必要でない限り、ClientBase<>から継承する「クライアント」クラスを作成する方がおそらく簡単です。

public class Client : ClientBase<IMath>
{
    private static Binding MyBinding { get; set; }

    private static EndpointAddress MyEndpoint { get; set; }

    public Client() : base(MyBinding, MyEndpoint)
    {
    }

    public double Add(double a, double b)
    {
        Open();
        var c = Channel.Add(a, b);
        Close();

        return c;
    }
}

次に、プロキシのインスタンスを作成して、エンドポイントを提供し、コンストラクターでバインドします (または、プロキシにデフォルトで自動的に実行させます。必要なことは何でも実行できます)。WCF サービスと通信します。次に、クライアント オブジェクトを開いて閉じ、Client.IMathOperation を呼び出して、サービスに対して操作を実行します。ClientBase<> は、チャネルの作成、破棄、プーリングなどを処理します。

Client proxy = new Client();
proxy.Add(1, 2);

おそらく、クライアント側でさまざまなラッパー クラスとヘルパー クラスを配置して例外を処理し、接続にアクセスする前に接続をテストし、チャネルの開閉をカプセル化して、クライアント側で使用する冗長性を軽減する必要があります。

于 2013-02-19T14:37:28.120 に答える