如何使用ChannelFactory

本文关键字:ChannelFactory 何使用 | 更新日期: 2023-09-27 18:00:11

基本上我有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);

但intellisense根本不知道IMath。我不擅长代理、ChannelFactory等。现在我的问题是,如果服务IMath托管在http://www.someserver/IMath.svc,如何在客户端编写代码来使用它?

请不要考虑添加网络参考到紧咬。。。

更新:在服务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>

如何使用ChannelFactory

基本上,您只需要知道客户端的绑定和端点地址。

若要使用WCF服务,您需要创建一个代理。除非您特别需要ChannelFactory的原因,否则创建一个从ClientBase<>继承的"Client"类可能会更容易。

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服务通信。然后只需打开和关闭客户端对象,然后调用客户端。IMathOperation对服务执行操作。ClientBase<>将处理通道创建、处置、池化等

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

您可能希望在客户端放入各种包装器和助手类来处理异常,在尝试访问连接之前测试连接,封装打开和关闭通道等,以减少在客户端使用的繁琐。