WCF - ServiceContract Operation 重载

方法重载在 OOP 中很常见,但在 WCF 中可能会有些麻烦。看下面的例子。

[ServiceContract]
public interface IContract
{
  void Test(int i);

  void Test(string s);
}

public class MyService : IContract
{
  public void Test(int i)
  {
    Console.WriteLine(i);
  }

  public void Test(string s)
  {
    Console.WriteLine(s);
  }
}

public class WcfTest
{
  public static void Test()
  {
    AppDomain.CreateDomain("Server1").DoCallBack(delegate
    {
      ServiceHost host = new ServiceHost(typeof(MyService), new Uri("http://localhost:8080/MyService"));
      host.AddServiceEndpoint(typeof(IContract), new BasicHttpBinding(), "");
      host.Open();
    });

    IContract channel = ChannelFactory<IContract>.CreateChannel(new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/MyService"));
    using (channel as IDisposable)
    {
      channel.Test(1);
    }
  }
}


在 Host 载入时发生异常,信息如下:

未处理 System.InvalidOperationException
  Message="ContractDescription 'IContract' has zero operations; a contract must have at least one operation."
  Source="System.ServiceModel"
  StackTrace:
    在 System.ServiceModel.Description.ContractDescription.EnsureInvariants()


虽然这个异常信息有点莫名其妙,但其实问题时出在方法重载上了。SOAP Message Action 并不能区分这两个方法,因此也无法确定调用目标。解决方案很简单,使用 OperationContract.Name。

[ServiceContract]
public interface IContract
{
  [OperationContract(Name="Test1")]
  void Test(int i);

  [OperationContract(Name="Test2")]
  void Test(string s);
}


这样一来,生成的客户端代理和消息中自然不会出现两个 Test 了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值