为什么方法重载没有在WCF允许?方法、WCF

由网友(欲擒故纵纵然满城骚动@)分享简介:假设这是一个的ServiceContract [ServiceContract]public interface MyService{[OperationContract]int Sum(int x, int y);[OperationContract]int Sum(double x, double y);}...

假设这是一个的ServiceContract

[ServiceContract]
public interface MyService
{
    [OperationContract]
    int Sum(int x, int y);

    [OperationContract]
    int Sum(double x, double y);

}

方法重载允许在C#中,但WCF不允许超载营运合约 该托管程序将抛出一个 InvalidOperationException异常,而托管

Method overloading is allowed in C#, but WCF does not allow you to overload operation contracts The hosting program will throw an InvalidOperationException while hosting

推荐答案

在简单地说,你不能重载方法的原因,是因为有一个事实,即WSDL不支持C#里面同样重载的概念present。下面交提供为什么这是不可能的细节。

In a nutshell, the reason you cannot overload methods has to do with the fact that WSDL does not support the same overloading concepts present inside of C#. The following post provides details on why this is not possible.

http://jeffbarnes.net/blog/post/2006/09/21/Overloading-Methods-in-WCF.aspx

要解决这个问题,你可以明确指定的名称属性 OperationContract的

To work around the issue, you can explicitly specify the Name property of the OperationContract.

[ServiceContract]
public interface MyService
{
    [OperationContract(Name="SumUsingInt")]
    int Sum(int x, int y);

    [OperationContract(Name="SumUsingDouble")]
    int Sum(double x, double y);
}
阅读全文

相关推荐

最新文章