gpt4 book ai didi

c# - WCF 中的 MessageHeader 问题

转载 作者:行者123 更新时间:2023-11-30 15:38:15 25 4
gpt4 key购买 nike

我在 WCF 中有两个单独的消息协定:

[MessageContract(WrapperName = "GetFooRequest", WrapperNamespace = "http://whatever.services.schema")]
public class GetFooRequest
{
[MessageHeader]
public int ResponseType { get; set; }

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

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

[MessageContract(WrapperName = "GetBarRequest", WrapperNamespace = "http://whatever.services.schema")]
public class GetBarRequest
{
[MessageHeader]
public int ResponseType { get; set; }

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

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

这工作正常,但是一旦我更改 GetBarRequest 上的 MessageHeader 的类型,该服务就不再工作:

[MessageHeader]
public string ResponseType { get; set; }

现在我在 GetFooRequest 和 GetBarRequest 中都有一个名为 ResponseType 的 MessageHeader,但它们有不同的类型。该服务不再有效。为什么我不能在两个完全不同的消息契约上使用同名/不同类型的 MessageHeader?

我得到的错误非常神秘:

Error: Cannot obtain Metadata from http://localhost:42575/Services/Test/TestService.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:42575/Services/Test/TestService.svc    Metadata contains a reference that cannot be resolved: 'http://localhost:42575/Services/Test/TestService.svc'.    Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:42575/Services/Test/TestService.svc.  The client and service bindings may be mismatched.    The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error    URI: http://localhost:42575/Services/Test/TestService.svc    There was an error downloading 'http://localhost:42575/Services/Test/TestService.svc'.    The underlying connection was closed: The connection was closed unexpectedly.

最佳答案

消息协定在 wsdl 上显示为原始类型,因此 GetBarRequestGetFooRequest 如下所示

WCF不按名称和类型验证schema,只验证名称,所以类型改成名称不变,出现如下错误

The ** operation references a message element [http://tempuri.org/:ResponseType] that has already been exported from the ** 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.

[MessageHeader(Name = "TextResponseType")]
public string ResponseType { get; set; } - Name property resolve the issue

关于c# - WCF 中的 MessageHeader 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11674829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com