C# 如何在 WCF 中有两个同名的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14472918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i have two methods with same name in WCF?
提问by Sagar
Possible Duplicate:
why you cant overload a method in WCF?
可能的重复:
为什么不能在 WCF 中重载方法?
i am working on one project in which i use the WCF services. My problem is that, in wcf service i am having one method named "Display()" which is used by me client1. Now i want to add another method with same name but with one parameter, ie. "Display(string name)", so that new clinet2 can use the new method and old client1 can use the old method. how can i achieve this. here is the code which i have written. 10Q in advance.
我正在开发一个使用 WCF 服务的项目。我的问题是,在 wcf 服务中,我有一种名为“Display()”的方法,供我的 client1 使用。现在我想添加另一个具有相同名称但带有一个参数的方法,即。“显示(字符串名称)”,以便新的 clinet2 可以使用新方法,而旧的 client1 可以使用旧方法。我怎样才能做到这一点。这是我写的代码。提前10Q。
namespace ContractVersioningService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string Display();
[OperationContract]
string GoodNight();
}
}
namespace ContractVersioningService
{
public class Service1 : IService1
{
public string Display()
{
return "Good Morning";
}
public string GoodNight()
{
return "Good Night";
}
}
}
namespace ContractVersioningService
{
[ServiceContract(Namespace = "ContractVersioningService/01", Name = "ServiceVersioning")]
public interface IService2 : IService1
{
[OperationContract]
string Disp(string greet);
}
}
namespace ContractVersioningService
{
public class Service2 : Service1, IService2
{
public string Display(string name)
{
return name;
}
public string Disp(string s)
{
return s;
}
}
}
采纳答案by C-va
Why WCF doesnot support method overloading directly ?
Because WSDL doesnot support method overloading(not OOPs). WCF generates WSDL which specifies the location of the service and the operation or methods the service exposes.
WCF use Document/Literal WSDL Style: Microsoft proposed this standard where the soap body element will contain the web method name.
By default all the WCF services conform to the document literal standard where the soap body should include the method name.
The only way is using Name attribute. For eg,
[OperationContract(Name="Integers")] int Display(int a,int b) [OperationContract(Name="Doubles")] double Display(double a,double b)
因为 WSDL 不支持方法重载(不是 OOP)。WCF 生成 WSDL,它指定服务的位置以及服务公开的操作或方法。
WCF 使用 Document/Literal WSDL 样式:Microsoft 提出了这个标准,其中soap body 元素将包含Web 方法名称。
默认情况下,所有 WCF 服务都符合文档文字标准,其中肥皂正文应包含方法名称。
唯一的方法是使用 Name 属性。例如,
[OperationContract(Name="Integers")] int Display(int a,int b) [OperationContract(Name="Doubles")] double Display(double a,double b)
The the compiler will generate the following, which makes sense for wsdl to locate
编译器会生成以下内容,wsdl定位就有意义
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName=
"ServiceRef.IService1")]
public interface IService1
{
[System.ServiceModel.OperationContractAttribute(
Action="http://tempuri.org/Service1/AddNumber",
ReplyAction="http://tempuri.org/IHelloWorld/IntegersResponse")]
int Display(int a,int b)
[System.ServiceModel.OperationContractAttribute(
Action="http://tempuri.org/IHelloWorld/ConcatenateStrings",
ReplyAction="http://tempuri.org/Service1/DoublesResponse")]
double Display(double a,double b)
}
回答by Christian.K
OK, I'm going to make this an answer, since the comments get overly excessive by now.
好的,我将把它作为一个答案,因为现在评论已经过度了。
You basically have two options:
你基本上有两个选择:
Use a single interface (note that interface inheritance, like you suggest in your question, technically counts as oneinterface here), but then you have togive each service operation a distinct name. You can either do that by naming the C# methods distinct, or by applying the
[OperationContract(Name = "distinctname")]
attribute.Use two separate interfaces, without any inheritance relationship in between them, publishing each on a different endpoint. You can have then have a service operation in each, having the same name, but with different parameters. You can still implement both interfaces with one implementation class, if you like/need to, of course.
使用单个接口(请注意,接口继承,就像您在问题中建议的那样,在技术上在这里算作一个接口),但是您必须为每个服务操作指定一个不同的名称。您可以通过将 C# 方法命名为不同的方法或通过应用
[OperationContract(Name = "distinctname")]
属性来实现。使用两个独立的接口,它们之间没有任何继承关系,将每个接口发布在不同的端点上。然后,您可以在每个中拥有一个服务操作,具有相同的名称,但具有不同的参数。当然,如果您喜欢/需要,您仍然可以使用一个实现类来实现两个接口。