我在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上显示为原始类型,因此GetBarRequest
和GetFooRequest
如下所示
WCF不会通过名称和类型(仅是名称)来验证架构,因此更改类型的名称是相同的,发生以下错误
**操作引用了已从**操作导出的消息元素[http://tempuri.org/:ResponseType]。您可以通过更改方法名称或使用OperationContractAttribute的Name属性来更改操作之一的名称。或者,您可以使用MessageContract编程模型更详细地控制元素名称。
[MessageHeader(Name = "TextResponseType")]
public string ResponseType { get; set; } - Name property resolve the issue