【问题标题】:Overloading method in webservice c#webservice c#中的重载方法
【发布时间】:2017-02-03 09:52:00
【问题描述】:

我在一个 Web 服务中重载了如下方法:

[SoapHeader("Authentication")]
    [WebMethod(MessageName = "DeferringTransaction", Description = "Deferring Transaction")]
    public WsResponse SendTransaction(WsDeferringTransaction wsDeferringTransaction)
    {
        factory.LoadSettings();
        WsResponse response = new WsResponse();
        response.ErrorCode = "Test";
        response.ReturnCode = 1;
        return response;
    }
    [SoapHeader("Authentication")]
    [WebMethod(MessageName = "ProcessedTransaction", Description = "Processed Transaction")]

    public WsResponse SendTransaction(WsProcessedTransaction wsProcessedTransaction)
    {
        factory.LoadSettings();
        WsResponse response = new WsResponse();
        response.ErrorCode = "Test";
        response.ReturnCode = 1;
        return response;
    }

但是当我尝试使用这个 web 服务时,方法 SendTransaction 出现了另一个名称:SendTransaction 和 SendTransaction1

 ServiceReference1.WsDeferringTransaction wsDeferringTransaction = new ServiceReference1.WsDeferringTransaction()
        {
            CaptiveId = 1,
            TransactionDeferringId = 2
        };

        ServiceReference1.WsResponse r = ws.SendTransaction(u, wsDeferringTransaction);

        ServiceReference1.WsProcessedTransaction wsProcessedTransaction = new ServiceReference1.WsProcessedTransaction()
        {
            DocumentNumber = 4,
            TransactionSequenceNumber = 450
        };

        ServiceReference1.WsResponse r2 = ws.SendTransaction1(u,wsProcessedTransaction);

是否可以创建重载方法并以相同的名称使用它?问题可能是因为 SoapHeader 吗?顺便说一句,我已经有了带有 WsiProfiles.None 的 webServiceBinding:

   [WebServiceBinding(ConformsTo = WsiProfiles.None)]

【问题讨论】:

    标签: c# .net web-services soap overloading


    【解决方案1】:

    如果您使用的是 SOAP,则不能,方法名称在导出的 WSDL 中必须具有唯一名称

    根据您使用的技术,有不同的方法来指定方法名称。例如,在 WCF 中,您可以使用 [OperationContract] 属性来指定名称。

    else 方法重载很容易在 Web 服务类中实现。请点击链接查看:-

    https://www.codeproject.com/kb/webservices/overloadinginwebservice.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-07
      • 2016-04-01
      • 1970-01-01
      相关资源
      最近更新 更多