.net 中的 SOAP 服务即服务参考 [英] SOAP Service as Service reference in .net

查看:23
本文介绍了.net 中的 SOAP 服务即服务参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有 2 个 SOAP 服务引用,它运行良好有一段时间了,但今天我对我的两个服务进行了更新服务引用",因为我已经对它们进行了更新.但是现在方法的数据结构发生了很大变化,我不知道如何将其改回来.

I have 2 SOAP Service References in my project, and it have been working nicely for a while, but today i did an "Update Service Refrence" on both my services as i had done an update to them. But now the datastructure of the methods have change alot, and i can not figure out how to change it back.

在我的方法bookFlight"中,以参数 BookModel 为例.

On of my methods "bookFlight" for an example take the argument BookModel.

public class BookModel
{
    /// <summary>
    /// Id
    /// </summary>
    public int Id { get; set; }
    /// <summary>
    /// Credit card informaiton
    /// </summary>
    public CreditCardInfoModel CreditCard { get; set; }
}

在我只需要执行以下操作来调用 SOAP 方法之前

Before i simply had to do the following to call the SOAP methoid

mySoapClient.bookFlight(new BookModel() { ... });

但是在我更新了我的服务之后,我现在必须像下面这样调用它:

But after i have updated my service do i now have to call it like the following:

mySoapClient.bookFlight(new bookFlightRequest()
{
    Body = new bookFlightRequestBody(new BookModel()
    {
        ...
    })
});

我该怎么做才能让它回到第一个例子中的原始数据结构?

What can i do to get it back to the orginal data structue from the first example?

肥皂可以在这里找到:http://02267.dtu.sogaard.us/LameDuck.asmx

我的服务参考设置:

SOAP 代码(如果需要)?

The SOAP code if that is needed?

/// <summary>
    /// Summary description for LameDuck
    /// </summary>
    [WebService(Namespace = "http://02267.dtu.sogaard.us/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [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 LameDuck : System.Web.Services.WebService
    {
        private Context context;
        private BankPortTypeClient bankService;
        private int groupNumber;
        private accountType account;

        public LameDuck()
        {
            context = new Context();
            bankService = new BankPortTypeClient();
            groupNumber = int.Parse(WebConfigurationManager.AppSettings["GroupNumber"]);
            account = new accountType()
            {
                number = "50208812",
                name = "LameDuck"
            };
        }

        /// <summary>
        /// Book a flight
        /// </summary>
        /// <param name="model">Booking data</param>
        /// <exception cref="FlightNotFoundException">If the flight was not found</exception>
        /// <exception cref="CreditCardValidationFailedException">Credit card validation failed</exception>
        /// <exception cref="CreditCardChargeFailedException">Charging the credit card failed</exception>
        /// <returns>True or exception</returns>
        [WebMethod]
        public bool bookFlight(BookModel model)
        {
            var flight = context.Flights.Where(x => x.Id == model.Id).FirstOrDefault();
            if (flight == null)
                throw new FlightNotFoundException(model.Id.ToString());

            if (!bankService.validateCreditCard(groupNumber, new creditCardInfoType()
                                {
                                    name = model.CreditCard.Name,
                                    number = model.CreditCard.Number,
                                    expirationDate = new expirationDateType()
                                    {
                                        year = model.CreditCard.ExpirationDate.Year,
                                        month = model.CreditCard.ExpirationDate.Month
                                    }
                                }, flight.Price))
                throw new CreditCardValidationFailedException();

            if (!bankService.chargeCreditCard(groupNumber, new creditCardInfoType()
                                {
                                    name = model.CreditCard.Name,
                                    number = model.CreditCard.Number,
                                    expirationDate = new expirationDateType()
                                    {
                                        year = model.CreditCard.ExpirationDate.Year,
                                        month = model.CreditCard.ExpirationDate.Month
                                    }
                                }, flight.Price, account))
                throw new CreditCardChargeFailedException();

            return true;
        }

        /// <summary>
        /// Search for flights
        /// </summary>
        /// <param name="model">Search data</param>
        /// <returns>List of flights, may be empty</returns>
        [WebMethod]
        public List<Flight> getFlights(SearchFlightModel model)
        {
            var select = context.Flights.Select(x => x);

            if (model.DestinationLocation != null)
                select = select.Where(x => x.Info.DestinationLocation == model.DestinationLocation);
            if (model.DepartureLocation != null)
                select = select.Where(x => x.Info.DepartureLocation == model.DepartureLocation);
            if (model.Date != null)
                select = select.Where(x => x.Info.DepartueTime.Date == model.Date.Date);

            return select.ToList();
        }

        /// <summary>
        /// Cancel a flight
        /// </summary>
        /// <param name="model">Cancel data</param>
        /// <exception cref="FlightNotFoundException">If the flight was not found</exception>
        /// <exception cref="UnableToRefundException">If unable to refund the flight to the credit card</exception>
        /// <returns>True or exception</returns>
        [WebMethod]
        public bool cancelFlight(CancelModel model)
        {
            var flight = context.Flights.Where(x => x.Id == model.Id).FirstOrDefault();
            if (flight == null)
                throw new FlightNotFoundException(model.Id.ToString());

            if (!bankService.refundCreditCard(groupNumber, new creditCardInfoType()
                                {
                                    name = model.CreditCard.Name,
                                    number = model.CreditCard.Number,
                                    expirationDate = new expirationDateType()
                                    {
                                        year = model.CreditCard.ExpirationDate.Year,
                                        month = model.CreditCard.ExpirationDate.Month
                                    }
                                }, flight.Price, account))
                throw new UnableToRefundException();

            return true;
        }

        /// <summary>
        /// Get a flight by id
        /// </summary>
        /// <param name="id">flight id</param>
        /// <exception cref="FlightNotFoundException">If the flight was not found</exception>
        /// <returns>The flight</returns>
        [WebMethod]
        public Flight getFlight(int id)
        {
            var flight = context.Flights.Where(x => x.Id == id).FirstOrDefault();
            if (flight == null)
                throw new FlightNotFoundException(id.ToString());
            return flight;
        }

        /// <summary>
        /// Reset the database
        /// 
        /// This is only to be used for testing
        /// </summary>
        [WebMethod]
        public void ResetDatabase()
        {
            // Remove all flights
            foreach (var flight in context.Flights)
                context.Flights.Remove(flight);
            context.SaveChanges();

            // Add Flights
            context.Flights.Add(new Flight()
            {
                Airline = "DTU Airlines",
                Price = 350,
                Info = new FlightInfo()
                {
                    DepartureLocation = "DTU",
                    DestinationLocation = "Spain",
                    DepartueTime = DateTime.UtcNow.AddDays(1),
                    ArrivalTime = DateTime.UtcNow.AddDays(1).AddHours(2),
                    Carrier = "Carrier 1"
                }
            });
            context.Flights.Add(new Flight()
            {
                Airline = "DTU Airlines",
                Price = 350,
                Info = new FlightInfo()
                {
                    DepartureLocation = "Spain",
                    DestinationLocation = "DTU",
                    DepartueTime = DateTime.UtcNow.AddDays(2),
                    ArrivalTime = DateTime.UtcNow.AddDays(2).AddHours(2),
                    Carrier = "Carrier 1"
                }
            });
            context.Flights.Add(new Flight()
            {
                Airline = "DTU Airlines",
                Price = 450,
                Info = new FlightInfo()
                {
                    DepartureLocation = "DTU",
                    DestinationLocation = "Italy",
                    DepartueTime = DateTime.UtcNow.AddDays(1),
                    ArrivalTime = DateTime.UtcNow.AddDays(1).AddHours(2),
                    Carrier = "Carrier 1"
                }
            });
            context.Flights.Add(new Flight()
            {
                Airline = "DTU Airlines",
                Price = 450,
                Info = new FlightInfo()
                {
                    DepartureLocation = "Italy",
                    DestinationLocation = "DTU",
                    DepartueTime = DateTime.UtcNow.AddDays(1),
                    ArrivalTime = DateTime.UtcNow.AddDays(1).AddHours(2),
                    Carrier = "Carrier 1"
                }
            });
            context.Flights.Add(new Flight()
            {
                Airline = "DTU Airlines",
                Price = 650,
                Info = new FlightInfo()
                {
                    DepartureLocation = "DTU",
                    DestinationLocation = "Turkey",
                    DepartueTime = DateTime.UtcNow.AddDays(1),
                    ArrivalTime = DateTime.UtcNow.AddDays(1).AddHours(2),
                    Carrier = "Carrier 2"
                }
            });
            context.Flights.Add(new Flight()
            {
                Airline = "DTU Airlines",
                Price = 650,
                Info = new FlightInfo()
                {
                    DepartureLocation = "Turkey",
                    DestinationLocation = "DTU",
                    DepartueTime = DateTime.UtcNow.AddDays(1),
                    ArrivalTime = DateTime.UtcNow.AddDays(1).AddHours(2),
                    Carrier = "Carrier 2"
                }
            });
            context.SaveChanges();
        }
    }

解决方案设置:

推荐答案

.ASMX 是遗留 Web 服务(在 Microsoft 技术堆栈中).通过 Visual Studio 添加对遗留 Web 服务的服务引用时,您可以单击高级"按钮,然后单击服务引用设置"对话框中的添加 Web 引用..."按钮进行添加.

.ASMX are legacy web services (in the Microsoft technology stack). When adding a service reference to a legacy web service via visual studio, you can click on the "Advanced" button and then the "Add Web Reference..." button in the Service Reference Settings dialog to add it.

我不知道为什么突然需要它,但我头顶上的一些事情是,在后台生成代理的代码很可能与 .ASMX 和 WCF 不同.另一种选择是如果您更改绑定(我相信 basicHttpBinding 是唯一支持 .ASMX - SOAP 1.1 - 除非您使用自定义绑定).

I'm not sure why it was suddenly needed, but a couple of things off the top of my head are that under the hood the code to generate the proxies is most likely different for .ASMX than for WCF. Another option is if you changed the bindings (I believe basicHttpBinding is the only one that supports .ASMX - SOAP 1.1 - unless you use a custom binding).

这篇关于.net 中的 SOAP 服务即服务参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆