【问题标题】:Linq to SQl partial class not being passed across web serviceLinq to SQl 部分类未通过 Web 服务传递
【发布时间】:2011-10-20 00:41:13
【问题描述】:

我使用 linq to SQL 作为我的 ORM,然后将 linq To Sql 对象通过 Web 服务传递到我的 aspx UI 页面。

当我将 Linq 直接传递给 sql 对象时,这一直工作正常,它们被序列化并且我能够以 json 的形式访问它们。当我通过 poco DTO 时,这个 web 服务也可以正常工作,因为这些对象中的一些数据来自几个不同的 linq to SQL 类。

我遇到的问题是当我尝试使用 linq to SQL 类时,在自定义部分类中定义了额外的字段。部分类的我的自定义端中的字段不会通过 Web 服务,部分类的 linq to sql 生成端中的字段确实会遇到。

当我创建对象时,这些字段就在那里,我可以在 Visual Studio 调试器中看到它们,直到它们通过服务,但它们不在另一边的 json 中。

这是部分类

using System.Runtime.Serialization;

namespace GPSO.ATOMWebDataLayer.LinqToSql
{
    public partial class Geofence
    {
        public string FillColor { get; set; } 
    }
}

这里是网络服务

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web;
using System.Web.Profile;
using System.Web.Security;
using System.Xml;
using GPSO.ATOMWebBusinessLayer;
using GPSO.ATOMWebDataLayer;
using GPSO.ATOMWebDataLayer.LinqToSql;
using GPSO.ATOMWebDataLayer.Objects;
using GPSOnline.ReportWebService;

using Contact = GPSO.ATOMWebDataLayer.LinqToSql.Contact;
using Event = GPSO.ATOMWebBusinessLayer.Event;
using Geofence = GPSO.ATOMWebBusinessLayer.Geofence;

namespace GPSOnline
{
    [ServiceContract(Namespace = "GPSOnline")]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ATOMWebService
    {
        private string _atomDbConnectionString;
        private string _aspNetDbConnectionString;
        private string _satelliteDbConnectionString;

        [OperationContract]
        [WebGet]
        public IEnumerable<GPSO.ATOMWebDataLayer.LinqToSql.Geofence> GetAllGeofences()
        {
            NoCache();

            var geofenceRepo = new GeofenceRepository(GetConnectionString());
            return geofenceRepo.GetAllGeofences().ToList();
        }
    }
}

and here is the Repository 

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using GPSO.ATOMWebBusinessLayer;
using GPSO.ATOMWebDataLayer.LinqToSql;
using GPSO.MapUtilities;
using GPSO.Repository;
using Microsoft.SqlServer.Types;
using Geofence = GPSO.ATOMWebBusinessLayer.Geofence;

namespace GPSO.ATOMWebDataLayer
{
    public class GeofenceRepository 
    {
        private static string _connectionString = "";

        public GeofenceRepository(string connectionString)           
        {
            _connectionString = connectionString;
        }
        public IEnumerable<LinqToSql.Geofence> GetAllGeofences()
        {
            var dataContext = new AtomWebDataContext(_connectionString);
            return allGeofences = dataContext.Geofences.ToList();    
        }
}

这里是 ui 页面

  <%@ Page Language="C#" MasterPageFile="~/Private/ATOMWEB.master" AutoEventWireup="true"
    Async="true" Inherits="GPSOnline.Assets" Title="ATOM®web Asset Map" CodeBehind="Assets.aspx.cs" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<%@ Register Src="../UserControls/ucAssetPicker_ClientEvent.ascx" TagName="ucAssetPicker"
    TagPrefix="uc1" %>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolderLeft" runat="Server">
    <asp:ScriptManagerProxy ID="ScriptManagerProxy" runat="server">
        <Services>
            <asp:ServiceReference Path="/ATOMWEBService.svc" />
        </Services>
        <Scripts>
            <asp:ScriptReference Path="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;client=gme-gpsonline" />
            <asp:ScriptReference Path="~/scripts/MapPanel.js" />
            <asp:ScriptReference Path="~/scripts/jquery-1.3.2.min.js" />
            <asp:ScriptReference Path="~/scripts/AssetLabel.js" />
            <asp:ScriptReference Path="~/scripts/NumberFormatting.js" />
            <asp:ScriptReference Path="~/scripts/DateFormatting.js" />
            <asp:ScriptReference Path="~/scripts/Utilities.js" />
            <asp:ScriptReference Path="~/scripts/geofences.js" />
            <asp:ScriptReference Path="~/scripts/mapUtilities.js" />
        </Scripts>
    </asp:ScriptManagerProxy>
    <script type="text/javascript">
        var map = null,
            atomWebServiceProxy = null,
            selectedAssetId = null,
            userPreferences = null,
            userId = null,
            assets = [],
            assetMarkers = [],
            updatedAssets = [],
            assetStatusPanel = null,
            assetContextItem = null,
            trackAsset = false,
            firstTime = true,
            deviceChannels = { },
            assetPanelLayouts = { },
            geoFences = { };

        function contentPageLoad() {
            atomWebServiceProxy = new GPSOnline.ATOMWebService();
        }

        /// <summary>

        /// <summary>        
        /// calls the webservice to get all the geo fences. 
        /// Runs gotGeoFences when service call successfull
        /// </summary>
        function getGeofences() {
           atomWebServiceProxy.GetAllGeofences(gotGeofences);          
        }


        /// <summary>
        /// Add the collection of geoFences to the page       
        /// </summary>        
        /// <param name="Result"> A collection of GeoFences </param>  
        function gotGeofences(result) {

            geoFences = new GeoFencesObj(map, result, "chkToggleGeofences", "<%=RadContextMenuGotoGeofence.ClientID%>", !userPreferences.DefaultShowGeofences);

            if (userPreferences.DefaultLocationType == 2 && userPreferences.HomeSiteId != null) {
                geoFences.zoomToGeofence(userPreferences.HomeSiteId);
            }
        }
}

【问题讨论】:

    标签: wcf web-services linq-to-sql


    【解决方案1】:

    您需要使用[DataMemberAttribute]:

    namespace GPSO.ATOMWebDataLayer.LinqToSql
    {
        [DataMember]
        public partial class Geofence
        {
            public string FillColor { get; set; } 
        }
    }
    

    【讨论】:

    • 谢谢你,是的,我试过了。但我在页面上收到错误消息。我附上了萤火虫的屏幕截图。
    • 说真的,你不能复制和粘贴文本吗?这是服务器错误。服务器抛出未处理的异常。去查看应用程序事件日志。
    • 是的,不用担心,我查看了日志,部分类中还有另一个字段,为了简洁起见,我没有使用 setter 方法。反过来,所有字段都必须同时具有 getter 和 setter。
    • 为了后代,他的错误信息是“NetworkError: 500 Internal Server Error - localhost:52360/ATOMWEBService.svc/jsdebug
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多