在引用的程序集中重用类型时,应用程序无法识别WCF服务客户端

时间:2014-08-26 17:43:34

标签: c# .net wcf

我有一个WCF服务,它为其DTO使用外部程序集。该服务的使用者也可以参考这个外部组件。计划是使用这些实体在服务和消费者之间传递信息。我已经多次使用其他服务完成了这项工作。

在这种情况下,它无法正常工作。 Intellisense识别驻留在WCF服务中的类(即:CreateEventRequest),但不显示标准TrackingServiceClient,这意味着无法实例化服务。

如果我取消选中“在引用的程序集中重用类型”,则一切都按预期工作。虽然,显然,我想重用DTO程序集。

DTO

[DataContract]
public class SiteTrackingEvent : IExtensibleDataObject
{
    #region Enums

    [DataContract]
    /// <summary>
    /// The different types of action categories that a user can perform
    /// </summary>
    public enum EventTypes
    {
        [EnumMember]
        None = 0,
        [EnumMember]
        Registrant = 1,
        [EnumMember]
        OnlineRequest = 2,
        [EnumMember]
        VTRequest = 3,
        [EnumMember]
        Download = 4,
        [EnumMember]
        ITRequest = 5
    }

    #endregion //Enums

    #region Properties
    [DataMember(Order = 1)]
    [Required]
    public string PolicyNumber { get; set; }
    [DataMember(Order = 2)]
    public EventTypes EventType { get; set; }
    [DataMember(Order = 3)]
    public string EventTypeDescription { get; set; }
    [DataMember(Order = 4)]
    public string PolicyOwnerSsnTin { get; set; }
    [DataMember(Order = 5)]
    public string PolicyOwnerName { get; set; }
    [DataMember(Order = 6)]
    public string SourcePath { get; set; }
    [DataMember(Order = 7)]
    public string SourceNumber { get; set; }
    [DataMember(Order = 8)]
    public string SaidPrimary { get; set; }
    [DataMember(Order = 9)]
    public string OfficeCode { get; set; }
    [DataMember(Order = 10)]
    public string BrokerDealerCode { get; set; }
    [DataMember(Order = 11)]
    public DateTime TimeStamp { get; set; }

    #endregion //Properties

    #region IExtensibleDataObject

    private ExtensionDataObject extensionDataObject_value;

    public ExtensionDataObject ExtensionData
    {
        get
        {
            return extensionDataObject_value;
        }
        set
        {
            extensionDataObject_value = value;
        }
    }

    #endregion //IExtensibleDataObject

}

服务界面

[ServiceContract]
public interface ITrackingService
{
    [OperationContract]
    void CreateEvent(ITrackingServiceContracts.CreateEventRequest trackingEvent);
}

namespace ITrackingServiceContracts
{
    [DataContract]
    public class CreateEventRequest
    {
        [DataMember(Order = 1)]
        [Required]
        public SiteTrackingEvent TrackingEvent { get; set; }

    }

}

服务的实施

public class TrackingService : ITrackingService
{

    public void CreateEvent(CreateEventRequest trackingEvent)
    {
        Validate(trackingEvent);
        //DO WORK HERE
    }

    public void Validate(CreateEventRequest request)
    {
        if (request == null)
            throw new FaultException("Request object is null.");

        List<ValidationResult> results = new List<ValidationResult>();
        if (!Validator.TryValidateObject(request, new ValidationContext(request, null, null), results, true))
        {
            throw new FaultException(String.Format("The request object is not valid: {0}.", String.Concat(results.Select(r => r.ErrorMessage))));
        }
    }

}

有人可以在这里看到我遗失/做错的事吗?

修改

根据@ErikFunkenbusch请求,以下是生成的reference.cs文件的相关部分。

选中“在引用的程序集中重用类型” 时(正在按预期工作的版本):

public partial class TrackingServiceClient : System.ServiceModel.ClientBase<ServiceTester.UsageTracking.TrackingService.ITrackingService>, ServiceTester.UsageTracking.TrackingService.ITrackingService {

    public TrackingServiceClient() {
    }

    public TrackingServiceClient(string endpointConfigurationName) : 
            base(endpointConfigurationName) {
    }

    public TrackingServiceClient(string endpointConfigurationName, string remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public TrackingServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(endpointConfigurationName, remoteAddress) {
    }

    public TrackingServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
            base(binding, remoteAddress) {
    }

    public void CreateEvent(ServiceTester.UsageTracking.TrackingService.CreateEventRequest mlaEvent) {
        base.Channel.CreateEvent(mlaEvent);
    }
}

选中“在引用的程序集中重用类型” 时(未按预期工作的版本)。注意:这是整个生成的文件。很明显它没有正确生成:

namespace ServiceTester.UsageTracking.TrackingService2 {
    using System.Runtime.Serialization;
    using System;


    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="CreateEventRequest", Namespace="http://schemas.datacontract.org/2004/07/Services.UsageTracking.ServiceInterfaces." +
        "ITrackingServiceContracts")]
    [System.SerializableAttribute()]
    public partial class CreateEventRequest {
    }
}

1 个答案:

答案 0 :(得分:1)

显然,DataContract上的[Required]会在生成可重用类型时混淆SvcUtil。可能是一个错误,或者可能只是不受支持。

我不建议您直接在视图中使用WCF DTO,而是使用View Models。或者,使用好友类。

Why are buddy classes used for validation?