C# 如何使用重载方法访问 Web 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1160299/
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 to access a web service with overloaded methods
提问by John Adams
I'm trying to have overloaded methods in a web service but I am getting a System.InvalidOperationException when attempting "Add Web Reference" in Visual Studio 2005 (here's the relevant snippets of code):
我正在尝试在 Web 服务中使用重载方法,但是在 Visual Studio 2005 中尝试“添加 Web 引用”时出现 System.InvalidOperationException(这是相关的代码片段):
public class FileService : System.Web.Services.WebService
{
private static readonly MetaData[] EmptyMetaData = new MetaData[0];
public FileService()
{
// a few innocent lines of constructor code here...
}
[WebMethod(MessageName = "UploadFileBasic",
Description = "Upload a file with no metadata properties")]
public string UploadFile(string trimURL
, byte[] incomingArray
, string fileName
, string TrimRecordTypeName)
{
return UploadFile(trimURL
, incomingArray
, fileName
, TrimRecordTypeName
, EmptyMetaData);
}
[WebMethod(MessageName = "UploadFile",
Description = "Upload a file with an array of metadata properties (Name/Value pairs)")]
public string UploadFile( string trimURL
, byte[] incomingArray
, string FileName
, string TrimRecordTypeName
, MetaData[] metaDataArray)
{
// body of UploadFile function here
I thought supplying a different MessageName property on the WebMethod attribute would fix this problem but here is the entire error message I get:
我认为在 WebMethod 属性上提供不同的 MessageName 属性可以解决这个问题,但这里是我得到的整个错误消息:
Both System.String UploadFileBasic(System.String, Byte[], System.String, System.String) and System.String UploadFile(System.String, Byte[], System.String, System.String) use the message name 'UploadFileBasic'. Use the MessageName property of the WebMethod custom attribute to specify unique message names for the methods.
System.String UploadFileBasic(System.String, Byte[], System.String, System.String) 和 System.String UploadFile(System.String, Byte[], System.String, System.String) 都使用消息名称“UploadFileBasic” '。使用 WebMethod 自定义特性的 MessageName 属性为方法指定唯一的消息名称。
The web service compiles OK; I cannot see what is wrong here.
web服务编译OK;我看不出这里有什么问题。
采纳答案by John Saunders
My suggestion is to not use overloaded method names. There is no such concept in WSDL, so why bother?
我的建议是不要使用重载的方法名称。WSDL 中没有这样的概念,何必呢?
回答by Keith
I would generally have a class object behind the web service interface that has the overloaded methods and then create individual methods in your asmx.cs file with different names. I know you can use the attributes but it just makes tidier code IMHO.
我通常会在具有重载方法的 Web 服务接口后面有一个类对象,然后在您的 asmx.cs 文件中创建具有不同名称的各个方法。我知道您可以使用这些属性,但恕我直言,它只会使代码更整洁。
回答by Vincenzo Ferraioli
You need to change this part:
您需要更改此部分:
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
to this one:
对这个:
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
回答by Afnan Ahmad
Operation overloading is not allowed for web services. But you can also follow the below steps.
Web 服务不允许操作过载。但您也可以按照以下步骤操作。
Firstly you need to change
首先你需要改变
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
To
到
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
Secondly MessageName property of WebMethod should be different for Overloaded method.
其次,对于 Overloaded 方法,WebMethod 的 MessageName 属性应该不同。
namespace foo
{
/// <summary>
/// Summary description for TestService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class TestService : System.Web.Services.WebService
{
[WebMethod(MessageName = "HelloWorld1")]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod(MessageName = "HelloWorld2")]
public string HelloWorld(string Value = "default")
{
return "Hello World";
}
}
}
But if you will call URL Like:
但是如果你调用 URL Like:
http://localhost:15558/TestService.asmx/HelloWorld2?Value=2
It will work.
它会起作用。
But if you will call URL Like:
但是如果你调用 URL Like:
http://localhost:15558/TestService.asmx/HelloWorld?Value=2
It will display HTTP 500
它会显示 HTTP 500