Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用ChannelFactory_C#_Wcf_Proxy - Fatal编程技术网

C# 如何使用ChannelFactory

C# 如何使用ChannelFactory,c#,wcf,proxy,C#,Wcf,Proxy,基本上我有来自 代码是: 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(doubl

基本上我有来自

代码是:

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();
}
}
使用系统;
使用System.ServiceModel;
//此代码由svcutil.exe生成。
[ServiceContract()]
接口IMath
{
[运营合同()]
双加(双A,双B);
}
公共数学课:IMath
{
公共双加(双A,双B)
{
返回A+B;
}
}
公开密封等级考试
{
静态void Main()
{
//代码未显示。
}
公开募捐
{
//此代码由应用程序开发人员编写。
//创建一个渠道工厂。
BasicHttpBinding myBinding=新的BasicHttpBinding();
EndpointAddress myEndpoint=新的EndpointAddress(“http://localhost/MathService/Ep1");
ChannelFactory myChannelFactory=新的ChannelFactory(myBinding,myEndpoint);
//创建一个频道。
IMath wcfClient1=myChannelFactory.CreateChannel();
双s=wcfClient1.Add(3,39);
Console.WriteLine(s.ToString());
((IClientChannel)wcfClient1.Close();
//创建另一个频道。
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);
ChannelFactory myChannelFactory=新的ChannelFactory(myBinding,myEndpoint);
但是intellisense根本不知道IMath。 我不擅长代理、ChannelFactory等。现在我的问题是,如果服务IMath是主机,如何在客户端编写代码来使用它

请不要认为向clent添加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>

- 
- 
- 
- 
- 
- 
- 
- 

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

要使用WCF服务,您需要创建一个代理。除非您特别需要ChannelFactory的原因,否则创建继承自的“客户端”类可能更容易


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

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

要使用WCF服务,您需要创建一个代理。除非您特别需要ChannelFactory的原因,否则创建继承自的“客户端”类可能更容易


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

IMath及其实现进入其自己的程序集,您从客户端引用该程序集。我看不到任何托管服务的代码。您拥有的是一个纯客户端代码,带有不需要的接口实现,但该实现没有被使用。对不起,我不理解。我刚刚更新了我的代码。根本没有端点。IMath及其实现进入其自己的程序集,您从客户端引用该程序集。我看不到任何托管服务的代码。您拥有的是一个纯客户端代码,带有不需要的接口实现,但该实现没有被使用。对不起,我不理解。我刚刚更新了我的代码。根本没有端点。更新了一些非常基本的实现。您需要从某个地方获取绑定和端点,可以通过在初始化过程中的某个时间点设置的静态变量从代码中获取,也可以通过配置文件或其他方式获取。感谢您的更新。我没有终点(也许我错了,但我还没有找到终点)。我只有一个soap地址,请查看我更新的wsdl。这是一个非常好的从开始到结束的教程,可以正确设置和使用WCF。就像另一个家伙说的,从你展示的情况来看,你实际上没有任何服务被托管。作为WCF配置的一部分,您将创建服务绑定和端点(以及其他内容)。你需要这些连接到服务。另一个人创建了服务,web配置根本没有端点。这是一项紧急任务,我没有时间学习材料。我们可以将http地址添加到引用中吗?是的,如果您知道http地址,则可以从中创建。如果你也找不到的话,他可能使用了BasicHttpBinding。更新了一些非常基本的实现。您需要从某个地方获取绑定和端点,可以通过在初始化过程中的某个时间点设置的静态变量从代码中获取,也可以通过配置文件或其他方式获取。感谢您的更新。我没有终点(也许我错了,但我还没有找到终点)。我只有一个soap地址,请查看我更新的wsdl。这是一个非常好的从开始到结束的教程,可以正确设置和使用WCF。就像另一个家伙说的,从你展示的情况来看,你实际上没有任何服务被托管。作为WCF配置的一部分,您将创建服务绑定和端点(以及其他内容)。你需要这些连接到服务。另一个人创建了服务,web配置根本没有端点。这是一项紧急任务,我没有时间学习材料。我们可以将http地址添加到引用中吗?是的,如果您知道http地址,则可以从中创建。如果你也找不到的话,他可能用了BasicHttpBinding
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;
    }
}
Client proxy = new Client();
proxy.Add(1, 2);