c# - 具有相同返回类型和名称的 MessageBodyMembers 导致异常 - 元素已导出

标签 c# .net wcf

我正在遵循一种模式,该模式具有用于 WCF 服务的请求和响应的对象。我有多个具有相同返回类型和名称的请求对象。任何帮助将不胜感激。

我收到以下异常:

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description.DataContractSerializerOperationBehavior contract: http://tempuri.org/:IService ----> System.InvalidOperationException: The Service.ServiceContract.IService.RetrieveUsers operation references a message element [http://tempuri.org/:WeekEndingId] that has already been exported from the Service.ServiceContract.IService.RetrieveDepartments operation. You can change the name of one of the operations by changing the method name or using the Name property of OperationContractAttribute. Alternatively, you can control the element name in greater detail using the MessageContract programming model.

编辑:我在属性上使用了 Name 属性来赋予它们唯一的名称,这确实解决了问题,但我们需要为所有请求使用名称“WeekEndingId”。我想尝试找到解决此问题的方法,同时仍然使用相同的属性名称。

下面列出了导致问题的类:

检索部门请求:

[MessageContract(WrapperName = "RetrieveDepartmentsRequest", WrapperNamespace = "http://Service.V1")]
    public class RetrieveDepartmentsRequest
    {
        [MessageBodyMember(Order = 0)]
        public int WeekEndingId { get; set; }

        [MessageBodyMember(Order = 1)]
        public string UserId { get; set; }

        [MessageBodyMember(Order = 2)]
        public string MachineName { get; set; }
    }

检索用户请求:

[MessageContract(WrapperName = "RetrieveUsersRequest", WrapperNamespace = "http://Service.V1")]
public class RetrieveUsersRequest
{
    [MessageBodyMember(Order = 0)]
    public int WeekEndingId { get; set; }

    [MessageBodyMember(Order = 1)]
    public string UserId { get; set; }

    [MessageBodyMember(Order = 2)]
    public string MachineName { get; set; }
}

I服务:

[OperationContract]
[FaultContract(typeof(ServiceFault))]
RetrieveDepartmentsResponse RetrieveDepartments(RetrieveDepartmentsRequest request);

[OperationContract]
[FaultContract(typeof(ServiceFault))]
RetrieveUsersResponse RetrieveUsers(RetrieveUsersRequest request);

最佳答案

我认为这是一种类型冲突。在那里面: WeekEndingId 在 RetrieveDepartmentsRequest 中是一个整数,在 RetrieveUsersRequest 中是一个小数。 使用 MessageBodyMember 的 Name 属性来解决冲突。 或者 只需更改 RetrieveUsersRequest 中的属性名称即可。

或者更好:WeekEndingId 不应该总是一个 int 吗?

关于c# - 具有相同返回类型和名称的 MessageBodyMembers 导致异常 - 元素已导出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15120613/

相关文章:

c# - 使用 NUnit3 控制台运行测试

c# - CsvWriter,CS1503 参数 2 : cannot convert CultureInfo

.net - System.Data.SQLite 如何处理 .NET 数据类型?

.net - VS2010 ASP.NET MVC 应用程序中的错误?

c# - wcf方法参数中的反序列化json为空

c# winforms - 在模式窗体之间传递参数

C# 是具有泛型类型约束的泛型类型

c# - 如何决定 Office 365 上的退回电子邮件?

c# - RESTful WCF json 请求和 json 响应返回 null

WCF 4 REST 服务无法返回 StatusDescription,仅返回 StatusCode