当重用引用程序集中的类型时,应用程序无法识别WCF服务客户端
本文关键字:应用程序 识别 客户端 服务 WCF 类型 引用 程序 程序集 集中 | 更新日期: 2023-09-27 18:19:26
我有一个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 {
}
}
[Required]
在生成可重用类型时混淆了SvcUtil。可能是一个错误,也可能只是不受支持。
我不建议直接在视图中使用WCF DTO,而是使用视图模型。或者,使用伙伴类。
为什么要使用伙伴类进行验证?