0

我有以下服务合同:

[ServiceContract(Namespace = "http://WebAdmin.Services.AdministrativeService", Name = "AdministrativeService")]
public interface IAdministrativeService
{
    /// <summary>
    /// Add the group based on the provided criteria 
    /// </summary>
    /// <example>
    /// <code>
    ///     service.AddWebGroup
    ///             (new User   {
    ///                             SequenceNumber = 1,
    ///                         },
    ///              new Group 
    ///                         {
    ///                             GroupName = "Example Group"
    ///                         },
    ///             new Application
    ///                         { 
    ///                             ApplicationCode = "DS"
    ///                         }
    ///             );
    /// </code>
    /// </example>
    /// <param name="webUser"><see cref="WebUser"/>The user a</param>
    /// <param name="group"><see cref="Group"/></param>
    /// <param name="application"><see cref="Application"/></param>
    /// <returns><see cref="Group"/></returns>
    [OperationContract]
    [TransactionFlow(TransactionFlowOption.Mandatory)]
    [FaultContract(typeof(ErrorFault))]
    [PrincipalPermission(SecurityAction.Demand, Role = "WEB_ADMIN_SVC")]
    Group AddWebGroup(WebUser webUser, Group group, Application application);

}

使用以下数据合同:

[DataContract]
public class WebUser
{
    [DataMember]
    public long? SequenceNumber { get; set; }

    [DataMember]
    public List<UserRole> Roles { get; set; }
}

    [DataContract]
public class UserRole
{
    [DataMember]
    public long? RoleID { get; set; }
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public Application Application { get; set; }
    [DataMember]
    public Group Group { get; set; }
}
{
[DataContract]
public class Application
{
    [DataMember]
    public string ApplicationCode { get; set; }
    [DataMember]
    public string ApplicationName { get; set; }
    [DataMember]
    public string ContactEmailAddress { get; set; }
    [DataMember]
    public string DivisionCode { get; set; }
}
[DataContract]
public class Group
{
    [DataMember]
    public long? GroupID { get; set; }
    [DataMember]
    public string GroupName { get; set; }
}

通过以下实现:

    [ServiceBehavior(
ConcurrencyMode = ConcurrencyMode.Single,
InstanceContextMode = InstanceContextMode.PerSession,
ReleaseServiceInstanceOnTransactionComplete = false,
TransactionIsolationLevel = System.Transactions.IsolationLevel.Serializable
)]
public class AdministrativeService : ServiceBase, IAdministrativeService
{
    #region Implementation of IAdministrativeService

    /// <summary>
    /// Add the group based on the provided criteria 
    /// </summary>
    /// <example>
    /// <code>
    ///     service.AddWebGroup
    ///             (new User   {
    ///                             SequenceNumber = 1,
    ///                         },
    ///              new Group 
    ///                         {
    ///                             GroupName = "Example Group"
    ///                         },
    ///             new Application
    ///                         { 
    ///                             ApplicationCode = "DS"
    ///                         }
    ///             );
    /// </code>
    /// </example>
    /// <param name="webUser"><see cref="WebUser"/>The user adding the group</param>
    /// <param name="group"><see cref="Group"/>The Group being added</param>
    /// <param name="application"><see cref="Application"/>The associated application</param>
    /// <returns><see cref="Group"/> With the ID set</returns>
    [OperationBehavior(TransactionAutoComplete = true, TransactionScopeRequired = true)]
    public Group AddWebGroup(WebUser webUser, Group group, Application application)
    {
        if (webUser == null)
            throw new ArgumentException("user");
        if (webUser.SequenceNumber == long.MinValue)
            throw new ArgumentException("user sequence number");

        if (group == null || string.IsNullOrWhiteSpace(group.GroupName))
            throw new ArgumentNullException("group");
       if (application == null || string.IsNullOrWhiteSpace(application.ApplicationCode))
            throw new ArgumentException("application");


        var service = Resolve<IWebGroupService>();

        var criteria = new GroupCriteriaEntity
                           {
                               ApplicationCode = application.ApplicationCode,
                               Group = new GroupEntity{GroupName = group.GroupName,},
                               User = new UserEntity{SequenceNumber = webUser.SequenceNumber,}
                           };
        GroupEntity result = null;
        try
        {
            result = service.InsertWebGroup(criteria);
        }
        catch (Exception ex)
        {
            ex.Data.Add("result",criteria);
            ExceptionPolicy.HandleException(ex, "Service Policy");
        }
        var groupResult = new Group
        {
            GroupID = result.SequenceNumber
        };

        return groupResult;

    }

服务模型绑定正在使用带有 TransportMessageCredential 的 wsHttpBinding ...

所有这些优秀的东西都可以在开发环境中使用。事务按预期提交和回滚。用户凭证允许和阻止用户执行代码,总而言之,每个人都很高兴。

测试环境应该是与开发环境相同的设置。但是,当代理客户端调用服务时,出现以下异常:

处理实例ID:44a7c5ae-17fc-4d14-b55e-1aa53bb156e0

发生并捕获了“System.ArgumentException”类型的异常。

2012 年 6 月 13 日 15:39:11 类型:System.ArgumentException,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089 消息:传入的参数不可序列化。参数名称:值源:mscorlib 帮助链接:ParamName:值数据:System.Collections.ListDictionaryInternal TargetSite:Void Add(System.Object, System.Object) 堆栈跟踪:在 System.Collections.ListDictionaryInternal.Add(Object key, Object value ) 在Administrative.Service.AdministrativeService.AddWebGroup(WebUser webUser, Group group, Application application) 在 SyncInvokeAddWebGroup(Object , Object[] , Object[] ) 在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] 输入, Object[]& 输出) 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) 在 System.ServiceModel。

关于为什么这会发生在一台机器上而不是另一台机器上的任何想法?当传递的所有对象都是代理客户端中生成的对象并带有 DataContract 标记时,为什么我会收到一个错误,即我正在传递一个不可序列化的对象?

4

1 回答 1

2

看起来异常发生在您执行以下操作的异常处理程序中:ex.Data.Add("result",criteria);,但我没有在定义 GroupCriteriaEntity 的数据合约(您展示的那些)中看到,那也是可序列化的吗?

于 2012-06-13T21:33:12.290 回答