【问题标题】:How to pass parameters to SOAP web service using asp.net mvc 4如何使用 asp.net mvc 4 将参数传递给 SOAP Web 服务
【发布时间】:2016-11-12 08:21:29
【问题描述】:

我想将参数传递给 SOAP Web 服务中的方法。当用户添加预订时,我想在 Web 服务中调用 smsSend() 方法。

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult AddBooking(booking bk)
    {
        if(ModelState.IsValid)
        {                
            db.bookings.Add(bk);
            db.SaveChanges();
            return RedirectToAction("Index", "Home");
        }
        return View(bk);
    }

我做了很多谷歌搜索,但对我没有任何帮助。我该怎么做才能有人帮助我将参数传递给 Web 服务。

【问题讨论】:

    标签: c# asp.net-mvc web-services asp.net-mvc-4 soap


    【解决方案1】:

    如果您使用的是 Visual Studio,则可以将服务引用添加到外部服务。 Visual Studio 将创建到引用的强类型 C# 绑定。您将需要 SOAP 服务的 URL 来添加它。请参阅此处的 Stack Overflow 问题(Web 应用程序的过程相同):

    How to add a web service reference in Visual Studio 2015 to a console app

    该过程的 MSDN 文档的链接在这里:

    https://msdn.microsoft.com/en-us/library/bb628652.aspx

    添加服务引用后,您只需添加一个 using 指令即可使用该服务,该指令包含您在添加服务时设置的命名空间并使用 Visual Studio 为您创建的绑定。

    如果您没有看到添加服务引用,您可能正在使用可能没有此功能的 .Net Core 项目。

    已编辑以添加有关您链接的特定服务的信息:

    using SmsTest.SmsService;
    
    namespace SmsTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                var msg = "Your SMS Message";
                var serviceProviderId = 42;
    
                // Use the constructor overloads for ServiceClient to configure how to connect
                var service = new MyServiceClient();
                service.smsSend(serviceProviderId, msg);
            }
        }
    }
    

    这是使用您提供的信息,我的程序的默认命名空间为SmsTest,服务引用的命名空间为SmsService

    【讨论】:

    • Visual Studio 创建的绑定应该有类和调用服务的方法。这些方法应该具有允许您将数据传递给 Web 服务所期望的特定方法的参数。如果您将 SOAP 服务的 URL 或文档提供给我们,我们或许可以提供更多帮助。
    • public ActionResult AddBooking(booking bk) { if(ModelState.IsValid) { db.bookings.Add(bk); db.SaveChanges(); var msg = "Your Service has been booked by "+bk.tContactNum; var emID = bk.SE_ID; var service = new GuideMeService(); service.smsSend(emID,msg); return RedirectToAction("Index", "Home"); } return View(bk); } 是我的答案,但它显示它有一些无效的参数
    • 服务提供者ID应该是一个整数,假设SE_ID是1。我不确定它具体说明了什么,但您可以查看他们的文档。否则,请检查客户端构造函数的重载,可能需要传递一些连接信息以使其连接。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    相关资源
    最近更新 更多