简体   繁体   English

WCF中的MessageHeader问题

[英]MessageHeader Issue in WCF

I have two separate message contracts in WCF: 我在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; }
}

This works fine, but as soon as I change the type of the MessageHeader on GetBarRequest, the service no longer works: 这工作正常,但只要我在GetBarRequest上更改MessageHeader的类型,该服务就不再有效:

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

Now I have a MessageHeader called ResponseType in both GetFooRequest and GetBarRequest, but they have different types. 现在我在GetFooRequest和GetBarRequest中都有一个名为ResponseType的MessageHeader,但它们有不同的类型。 The service no longer works. 该服务不再有效。 Why can I not have a MessageHeader with the same name / different type on two completely different Message Contracts? 为什么我在两个完全不同的消息合同上没有具有相同名称/不同类型的MessageHeader?

The error I get is pretty cryptic: 我得到的错误非常神秘:

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.

Message contract displayed as primitive type on wsdl, so GetBarRequest and GetFooRequest looks following 消息协定在wsdl上显示为原始类型,因此GetBarRequestGetFooRequest看起来如下

WCF does not validate schema by name and type, only name, so type is changed name is the same, following error is occurred WCF不按名称和类型验证模式,仅验证名称,因此类型更改名称相同,发生以下错误

The ** operation references a message element [ http://tempuri.org/:ResponseType] that has already been exported from the ** operation. **操作引用已从**操作导出的消息元素[ http://tempuri.org/:ResponseType] You can change the name of one of the operations by changing the method name or using the Name property of OperationContractAttribute. 您可以通过更改方法名称或使用OperationContractAttribute的Name属性来更改其中一个操作的名称。 Alternatively, you can control the element name in greater detail using the MessageContract programming model. 或者,您可以使用MessageContract编程模型更详细地控制元素名称。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM