1.服务端
[ServiceContract]
public interface IService2_1
{
/*使用Name方式实现契约的重载。使用Name可以控制契约在WSDL中的名称不至于重复*/
[OperationContract(Name = "AddInt", Action = "yo/IService2_1/Add", ReplyAction = "yo/IService2_1/AddResponse")]
int Add(int a, int b);
[OperationContract(Name = "AddDouble", Action = "yo/IService2_1/AddDouble", ReplyAction = "yo/IService2_1/AddDoubleResponse")]
double Add(double a, double b);
}
public class Service2_1 : IService2_1
{
#region IService2_1 成员
public int Add(int a, int b)
{
return a + b;
}
public double Add(double a, double b)
{
return a + b;
}
#endregion
}
2.客户端 注意:这段是必须的,因为要手动生成通信通道
[ServiceContract]
public interface IService2_1
{
[OperationContract(Name = "AddInt")]
int Add(int a, int b);
[OperationContract(Name = "AddDouble")]
int Add(double a, double b);
}
public class Service2_1Client : ClientBase<IService2_1>, IService2_1
{
#region IService2_1 成员
public int Add(int a, int b)
{
return Channel.Add(a, b);
}
public int Add(double a, double b)
{
return Channel.Add(a, b);
}
#endregion
}
Binding binding = new WSHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8732/Design_Time_Addresses/WcfService2_1/Service1/");
IService2_1 proxy2_1 = ChannelFactory<IService2_1>.CreateChannel(binding, address);
using (proxy2_1 as IDisposable)
{
Console.WriteLine("手动方式创建客户端代理,调用服务计算的值:{0}", proxy2_1.Add(1, 2));
}