1

我是新手,WCF所以请多多包涵。

为什么我在我的服务中NullReferenceException occured尝试创建一个新DateTime对象时会得到一个。WCF

这是代码行:

DateTime fromDate = new DateTime(DateTime.Now.Ticks);

我尝试了各种方法,例如:

DateTime fromDate = new DateTime();
DateTime fromDate = DateTime.Now;

所有 3 行代码都给我一个NullReferenceException occured错误。

截屏:

在此处输入图像描述

我可以在 中使用DateTime对象WCF吗?

编辑:

代码到该行:

public class CampaignSchedulePlacementService : ICampaignSchedulePlacementService
    {

        public PlacementAdvertRoutingAddressPairsResponce GetPlacementAdvertRoutingAddressPairs(string userName, string password, string routingAddressCode, int monthsBack, int monthsAhead)
        {
            return GetPlacementAdvertRoutingAddressPairsByFilters(userName, password, routingAddressCode, monthsBack, monthsAhead);
        }

        #region Methods

        private PlacementAdvertRoutingAddressPairsResponce GetPlacementAdvertRoutingAddressPairsByFilters(string userName, string password, string routingAddressCode, int monthsBack, int monthsAhead)
        {
            PlacementAdvertRoutingAddressPairsResponce placementAdvertRoutingAddressPairsResponce = new PlacementAdvertRoutingAddressPairsResponce();
            StagingEntityModel stagingEntityModel = null;

            try
            {
                try
                {
                    string connectionString = WebConfigurationManager.ConnectionStrings["Test"].ConnectionString;
                    stagingEntityModel = new StagingEntityModel(RMS.Common.Core.Public.GetEntityConnectionString(connectionString, "Data.EntityFramework.Model.StagingEntityModel"));
                    stagingEntityModel.Connection.Open();
                }
                catch (Exception ex)
                {
                    placementAdvertRoutingAddressPairsResponce.Message = "Failed opening the connection :" + String.Format("{0}\r\n with an inner exception of {1}\r\nand a stack trace of {2}", ex.Message,
                                                                                ex.InnerException != null ? ex.InnerException.Message : "",
                                                                                ex.StackTrace);

                    return placementAdvertRoutingAddressPairsResponce;
                }

                User currentUser = stagingEntityModel.User.FirstOrDefault(x => x.UserName == userName && x.Password == password);

                if (currentUser != null)
                {
                    DateTime fromDate = new DateTime(DateTime.Now.Ticks); //.AddMonths(monthsBack * -1);
                    DateTime toDate = new DateTime(DateTime.Now.Ticks); //.AddMonths(monthsAhead);

编辑:

调用堆栈:

在此处输入图像描述

4

1 回答 1

3

我可以在 WCF 中使用 DateTime 对象吗?

最肯定的。

虽然我看不到堆栈跟踪,但这条线DateTime fromDate = new DateTime(DateTime.Now.Ticks);不会真的失败。没什么好说的null。但是,这一行:

stagingEntityModel.User.FirstOrDefault(x => x.UserName == userName && x.Password == password);

肯定会失败。我想你会发现.Usernull。而且我认为您还会发现 Visual Studio,因为它似乎做了很多,跳到其他一些不是失败的声明。

于 2012-12-27T12:07:47.943 回答