Java客户端的WCF服务问题

时间:2012-02-11 23:20:06

标签: wcf datetime java-client

这是一个有趣的问题,我会尽力解释。如果您有任何问题,请询问。

我编写了一个WCF服务,假设与JAVA客户端通信。此服务是通过WSDL首先通过合同创建的。现在,根据WCF测试客户端,一切正常,即使在PHP客户端上进行测试也能正常工作。但是当谈到Java客户端时,请求消息和后续响应消息无法返回:我得到一个空对象SOAP错误。以下是我认为问题所在:

根据XSD和WSDL,我有一个我想要接受的DateTime值。来自客户端的dateTime值的格式为:2012-01-01T12:00:00.00Z。不幸的是,此输入对于内置的.NET日期时间无效。因此,为了解决这个问题,我更改了我的代码以获取字符串数据类型,将该字符串转换为Datetime以将其发送到数据库,从该dateTime中的数据库获取响应并将其转换回字符串以进行响应返回一个类似于输入的值。

我构建了一个记录器来检查是否正在向我的wcf服务发送消息。从那以后,我发现没有收到来自客户的消息。我唯一的猜测是因为日期时间问题。

有没有办法以下列格式获取dateTime数据类型:2012-01-01T12:00:00.000Z?如果可以的话,这意味着请求将匹配我的数据类型,也许它会起作用。

以下是一些代码:

    public partial class findSeatsRequest
{

    [MessageBodyMemberAttribute(Namespace="http://soa.cs.uwf.edu/airlineData", Order=0, Name="departAirport")]
    public string DepartAirport;

    [MessageBodyMemberAttribute(Namespace="http://soa.cs.uwf.edu/airlineData", Order=1, Name="arriveAirport")]
    public string ArriveAirport;

    [MessageBodyMemberAttribute(Namespace="http://soa.cs.uwf.edu/airlineMessage", Order=2, Name="earliestDepartTime")]
    public string EarliestDepartTime;

    [MessageBodyMemberAttribute(Namespace="http://soa.cs.uwf.edu/airlineMessage", Order=3, Name="latestDepartTime")]
    public string LatestDepartTime;

    [MessageBodyMemberAttribute(Namespace="http://soa.cs.uwf.edu/airlineMessage", Order=4, Name="minimumSeatsAvailable")]
    public int MinimumSeatsAvailable;

    [MessageBodyMemberAttribute(Namespace="http://soa.cs.uwf.edu/airlineMessage", Order=5, Name="maximumFlightsToReturn")]
    public int MaximumFlightsToReturn;

    public findSeatsRequest()
    {
    }

    public findSeatsRequest(string departAirport, string arriveAirport, string earliestDepartTime, string latestDepartTime, int minimumSeatsAvailable, int maximumFlightsToReturn)
    {
        this.DepartAirport = departAirport;
        this.ArriveAirport = arriveAirport;
        this.EarliestDepartTime = earliestDepartTime;
        this.LatestDepartTime = latestDepartTime;
        this.MinimumSeatsAvailable = minimumSeatsAvailable;
        this.MaximumFlightsToReturn = maximumFlightsToReturn;
    }
}


    public partial class findSeatsResponse
{

    [MessageBodyMemberAttribute(Namespace="http://soa.cs.uwf.edu/airlineData", Order=0, Name="flight")]
    [XmlElementAttribute("flight")]
    public System.Collections.Generic.List<flightType> Flight;

    public findSeatsResponse()
    {
    }

    public findSeatsResponse(System.Collections.Generic.List<flightType> flight)
    {
        this.Flight = flight;
    }
}

        public virtual findSeatsResponse findSeats(findSeatsRequest request)
    {
        string departAirport = request.DepartAirport;
        string arriveAirport = request.ArriveAirport;
        string earliestDepartTime = request.EarliestDepartTime;
        string latestDepartTime = request.LatestDepartTime;
        int minimumSeatsAvailable = request.MinimumSeatsAvailable;
        int maximumFlightsToReturn = request.MaximumFlightsToReturn;
        SqlCommand cmd = null;
        DataSet ds = new DataSet();
        List<flightType> flight = new List<flightType>();
        EventLogger log = new EventLogger();

        findSeatsRequest inValue = new findSeatsRequest();
        inValue.DepartAirport = departAirport;
        inValue.ArriveAirport = arriveAirport;
        inValue.EarliestDepartTime = earliestDepartTime;
        inValue.LatestDepartTime = latestDepartTime;
        inValue.MinimumSeatsAvailable = minimumSeatsAvailable;
        inValue.MaximumFlightsToReturn = maximumFlightsToReturn;

        string latestT = inValue.LatestDepartTime.Replace("T", " ");
        string latestZ = latestT.Replace("Z", "");
        DateTime _latestDepartTime = DateTime.ParseExact(latestZ, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);

        string earliestT = inValue.EarliestDepartTime.Replace("T", " ");
        string earliestZ = earliestT.Replace("Z", "");
        DateTime _earliestDepartTime = DateTime.ParseExact(earliestZ, "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.InvariantCulture);

        log.WriteToDataBase(DateTime.Now, "FindSeats", 1, "This is the request: " + inValue);


        //Check Maximum Flights
        if (inValue.MaximumFlightsToReturn > 100 | inValue.MaximumFlightsToReturn < 0)
        {
            throw new FaultException(
                "You cannot select more than 100 flights to return, or the maximum flights to return is negative.",
                new FaultCode("OutOfRange"));
        }

        // Check Minimum Seats Available.
        if (inValue.MinimumSeatsAvailable < 0)
        {
            throw new FaultException(
                "You minimum seats available cannot be negative.",
                new FaultCode("OutOfRange"));
        }

        // Check for valid Departure Airport
        if (departAirport != null && departAirport != "ANY")
        {
            try
            {
                string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                SqlConnection conn = new SqlConnection(strConn);

                conn.Open();

                string check = "SELECT DepartAirport FROM Flight WHERE DepartAirport='" + departAirport + "'";
                cmd = new SqlCommand(check, conn);
                cmd.CommandText = check;
                SqlDataReader depAirport;
                depAirport = cmd.ExecuteReader();

                if (depAirport.HasRows == false)
                {
                    throw new FaultException(
                        "Invalid Airport code used.",
                        new FaultCode("Invalid Text Entry"));
                }
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        // Check for valid Arrival Airport
        if (arriveAirport != null && arriveAirport != "ANY")
        {
            try
            {
                string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
                SqlConnection conn = new SqlConnection(strConn);

                conn.Open();

                string check = "SELECT ArriveAirport FROM Flight WHERE ArriveAirport='" + arriveAirport + "'";
                cmd = new SqlCommand(check, conn);
                cmd.CommandText = check;
                SqlDataReader arrAirport;
                arrAirport = cmd.ExecuteReader();

                if (arrAirport.HasRows == false)
                {
                    throw new FaultException(
                        "Invalid Airport code used.",
                        new FaultCode("Invalid Text Entry"));
                }
            }
            finally
            {
                if (cmd != null)
                    cmd.Dispose();
            }
        }

        try
        {
            string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            SqlConnection conn = new SqlConnection(strConn);

            conn.Open();

            cmd = new SqlCommand("usp_NewFindSeats", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@DepartureAirport", inValue.DepartAirport));
            cmd.Parameters.Add(new SqlParameter("@ArrivalAirport", inValue.ArriveAirport));
            cmd.Parameters.Add(new SqlParameter("@EarliestDepTime", _earliestDepartTime));
            cmd.Parameters.Add(new SqlParameter("@LatestDepTime", _latestDepartTime));
            cmd.Parameters.Add(new SqlParameter("@minSeatsAvailable", inValue.MinimumSeatsAvailable));
            cmd.Parameters.Add(new SqlParameter("@maxFlightsRequested", inValue.MaximumFlightsToReturn));

            using (SqlDataReader sqlReader = cmd.ExecuteReader())
            {
                while (sqlReader.Read())
                {
                    flightType Flight = new flightType();

                    Flight.FlightId = sqlReader.GetString(0);
                    Flight.DepartAirport = sqlReader.GetString(1);
                    Flight.ArriveAirport = sqlReader.GetString(2);
                    Flight.DepartTime = sqlReader.GetDateTime(3).ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
                    Flight.ArriveTime = sqlReader.GetDateTime(4).ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
                    Flight.FlightSeatsAvailable = sqlReader.GetInt32(5);
                    Flight.FlightSeatPriceUSD = sqlReader.GetDouble(6);

                    flight.Add(Flight);
                }
            }

1 个答案:

答案 0 :(得分:0)

对于这个特殊的问题,问题并不是我认为在开始时的任何问题。首先,必须在我的C#代码的请求操作中使用WrapperNames,并且我使用的名称不正确。

其次,我需要指定我必须在接口层中执行的SOAP Body编码。

 [XmlSerializerFormatAttribute(SupportFaults=true, Style=OperationFormatStyle.Document ,Use=OperationFormatUse.Literal)]