基本上我有来自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,如何在客户端编写代码来使用它?
请不要认为将网络引用添加到客户端...
更新:在服务 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>