首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ChannelFactory

如何使用ChannelFactory
EN

Stack Overflow用户
提问于 2013-02-19 14:24:19
回答 1查看 3.5K关注 0票数 1

基本上我有来自MSDN.的代码

守则是:

代码语言:javascript
复制
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是服务器中的主机,我们根本不知道它的内部结构。那么如何在客户端使用它呢?我用了密码

代码语言:javascript
复制
ChannelFactory<IMath> myChannelFactory = new ChannelFactory<IMath>(myBinding, myEndpoint);

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

请不要认为添加web引用到clent..。

更新的:在服务wsdl中:我有如下内容:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-19 14:37:28

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

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

代码语言:javascript
复制
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<>将处理信道创建、处理、池等操作。

代码语言:javascript
复制
Client proxy = new Client();
proxy.Add(1, 2);

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14959795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档