如何使用asp.net mvc 4将参数传递给SOAP Web服务

时间:2016-07-10 06:03:51

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

我想将参数传递给SOAP 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服务。

1 个答案:

答案 0 :(得分:0)

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

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