c# - 具有相似签名的多个网络方法

标签 c# .net web-services asmx

在 .net 2.0 中使用 ASMX。

重现问题的代码:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

namespace TestWebService
{

    public class SomeClass
    {
        public int Number;
    }

    public class SomeClassRet
    {
        public string AString;
    }


    [WebService(Namespace = "http://MyNamespace")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class MyTestService : System.Web.Services.WebService
    {

        [WebMethod]
        [SoapDocumentMethod("http://MyNamespace/MyMeth", ParameterStyle = SoapParameterStyle.Bare, Use = System.Web.Services.Description.SoapBindingUse.Literal )]
        public SomeClassRet MyMeth1(SomeClass requestParams)
        {
            return null;
        }

        [WebMethod]
        [SoapDocumentMethod("http://AotherNamespace/MyMeth", ParameterStyle = SoapParameterStyle.Bare, Use = System.Web.Services.Description.SoapBindingUse.Literal )]
        public SomeClassRet MyMeth2(SomeClass requestParams)
        {
            return null;
        }
    }
}

我想要做的是拥有两个具有相同签名的 Web 方法,除了具有不同的 SOAPAction。上面的代码可以编译,但是在尝试生成 WSDL 时出现如下错误:

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


    [InvalidOperationException: Service 'TestWebService.MyTestService' does not conform to WS-I Basic Profile v1.1. Please examine each of the normative statement violations below. To turn off conformance check set the ConformanceClaims property on corresponding WebServiceBinding attribute to WsiClaims.None.
    R2710: The operations in a wsdl:binding in a DESCRIPTION MUST result in wire signatures that are different from one another. An endpoint that supports multiple operations must unambiguously identify the operation being invoked based on the input message that it receives. This is only possible if all the operations specified in the wsdl:binding associated with an endpoint have a unique wire signature. 
      -  Input message 'MyMeth1SoapIn' from namespace 'http://MyNamespace' has wire signature 'http://MyNamespace:requestParams'.
      -  Input message 'MyMeth2SoapIn' from namespace 'http://MyNamespace' has wire signature 'http://MyNamespace:requestParams'.
    The Profile defines the "wire signature" of an operation in a wsdl:binding to be the fully qualified name of the child element of the soap:Body of the SOAP input message it describes. For the case of an empty soap:Body this name is an empty string. In the case of rpc-literal binding, the operation name is used as a wrapper for the part accessors. In the docum...]
       System.Web.Services.Description.ProtocolReflector.ReflectBinding(ReflectedBinding reflectedBinding) +557501
       System.Web.Services.Description.ProtocolReflector.Reflect() +703
       System.Web.Services.Description.ServiceDescriptionReflector.ReflectInternal(ProtocolReflector[] reflectors) +394
       System.Web.Services.Description.ServiceDescriptionReflector.Reflect(Type type, String url) +109
       System.Web.Services.Protocols.DocumentationServerType..ctor(Type type, String uri) +156
       System.Web.Services.Protocols.DocumentationServerProtocol.Initialize() +284
       System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) +50
       System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +77

    [InvalidOperationException: Unable to handle request.]
       System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) +285
       System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +183

    [InvalidOperationException: Failed to handle request.]
       System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +354
       System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
       System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +193
       System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我是在尝试做错事还是不受支持?任何解决方法? 我无法更改输入或输出参数的名称。如果可能的话,目标是除了 SOAPAction 之外具有相同的签名。

谢谢。

编辑:经过一些进一步的实验,我决定将重复的方法(具有不同的 SOAPAction)放入另一个 .asmx 文件中。

最佳答案

要在 web 服务中重载方法,您需要提供消息名称属性,或者您可能需要删除关闭一致性检查。

[WebMethod]
[SoapDocumentMethod("http://MyNamespace/MyMeth",MessageName="MyMeth1" ParameterStyle = SoapParameterStyle.Bare, Use = System.Web.Services.Description.SoapBindingUse.Literal )]
public SomeClassRet MyMeth1(SomeClass requestParams)
{
return null;
}

[WebMethod]
[SoapDocumentMethod("http://AotherNamespace/MyMeth",MessageName="MyMeth1Overload" ParameterStyle = SoapParameterStyle.Bare, Use = System.Web.Services.Description.SoapBindingUse.Literal )]
public SomeClassRet MyMeth2(SomeClass requestParams)
{
return null;
}

并且您可以通过将以下行添加到 service.cs 文件来关闭一致性。

[WebServiceBinding(ConformsTo = WsiProfiles.None)]

这里有更多详细信息:http://msdn2.microsoft.com/en-us/library/system.web.services.webservicebindingattribute.conformsto.aspx

关于c# - 具有相似签名的多个网络方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7720147/

相关文章:

javascript - Window.open 弹出窗口卡住整个桌面

c# - Office 2013 c# 加载项 - 空工作簿对象 HRESULT : 0x800A03EC error

web-services - 找不到要下载的 Axis 1.4

image - 如何避免 "To protect your privacy, some pictures in this message were not downloaded"出现在 Outlook 中

c# - 有没有更好的方法用最小起订量编写这个测试?

c# - 为什么这段代码不会死锁?

c# - 在 .net Windows 服务中处理停止

c# - 当 UWP 中有枢轴时,滑动手势不起作用

.net - AutomationProperties.Name VS x :Name

java - 使用 wsimport 生成的注释可以被覆盖吗?