当我使用Visual Studio 2008(SP1)上的"添加服务引用"导入给定服务时,所有请求/响应消息都被不必要地包装到消息合同中(命名为 - >"operationName"+"Request"/"Response"+最后"1").
代码生成器说:
// CODEGEN: Generating message contract since the operation XXX is neither RPC nor
// document wrapped.
Run Code Online (Sandbox Code Playgroud)
从Java服务生成wsdl的人说他们正在指定DOCUMENT-LITERAL/WRAPPED.
任何帮助/指针/线索将受到高度赞赏.
更新:这是我的wsdl的示例,其中一个操作看起来很可疑.请注意,与响应相比,请求的消息元素属性不匹配.
<!- imports namespaces and defines elements -->
<wsdl:types>
<xsd:schema targetNamespace="http://WHATEVER/" xmlns:xsd_1="http://WHATEVER_1/" xmlns:xsd_2="http://WHATEVER_2/">
<xsd:import namespace="http://WHATEVER_1/" schemaLocation="WHATEVER_1.xsd"/>
<xsd:import namespace="http://WHATEVER_2/" schemaLocation="WHATEVER_2.xsd"/>
<xsd:element name="myOperationResponse" type="xsd_1:MyOperationResponse"/>
<xsd:element name="myOperation" type="xsd_1:MyOperationRequest"/>
</xsd:schema>
</wsdl:types>
<!- declares messages - NOTE the mismatch on the request element attribute compared to response -->
<wsdl:message name="myOperationRequest">
<wsdl:part element="tns:myOperation" name="request"/>
</wsdl:message>
<wsdl:message name="myOperationResponse">
<wsdl:part element="tns:myOperationResponse" …Run Code Online (Sandbox Code Playgroud) 我正在尝试将MessageContract添加到我的WCF服务,类似于此问题中的内容:WCF:使用流式传输与消息合同
这是我得到的异常: 无法加载操作"UploadFile",因为它具有System.ServiceModel.Channels.Message类型的参数或返回类型,或具有MessageContractAttribute的类型和不同类型的其他参数.使用System.ServiceModel.Channels.Message或使用MessageContractAttribute类型时,该方法不得使用任何其他类型的参数.
这是我的合同:
[ServiceContract]
public interface IFile
{
[OperationContract]
bool UploadFile(FileUpload upload);
}
[MessageContract]
public class FileUpload
{
[MessageHeader(MustUnderstand = true)]
public int Username { get; set; }
[MessageHeader(MustUnderstand = true)]
public string Filename { get; set; }
[MessageBodyMember(Order = 1)]
public Stream ByteStream { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我在app.config中使用的绑定配置:
<netTcpBinding>
<binding name="TCPConfiguration" maxReceivedMessageSize="67108864" transferMode="Streamed">
<security mode="None" />
</binding>
</netTcpBinding>
Run Code Online (Sandbox Code Playgroud)
现在我想这可能与我正在使用的绑定类型有关,但我不完全确定.
我已经写了一个定制的命名空间和命名空间前缀由WCF生成的SOAP消息在这里.
但是,我找不到在Message类中重写的正确方法,以便自定义消息的SOAP标头.
我想发这个消息:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<h:Protocol xmlns="http://www.xyz.de/Protocol" xmlns:h="http://www.xzy.de/Protocol">
<version>IFD_1.4</version>
</h:Protocol>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</s:Body>
</s:Envelope>
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope">
<s:Header>
<if:Protocol xmlns="http://www.xyz.de/Protocol" xmlns:if="http://www.xzy.de/Protocol">
<version>IFD_1.4</version>
</if:Protocol>
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</s:Body>
</s:Envelope>
Run Code Online (Sandbox Code Playgroud)
不同之处在于第一个标题的名称空间是"if"而不是"f".
有没有办法使用自定义MessageFormatter和自定义Message类来做到这一点?
情况:我需要构建一个基于XSD的Web服务.
当XSD说:时,可以创建一个WCF Web服务:
elementFormDefault="qualified" attributeFormDefault="qualified"
Run Code Online (Sandbox Code Playgroud)
要么
elementFormDefault="unqualified" attributeFormDefault="unqualified"
Run Code Online (Sandbox Code Playgroud)
我在Stackoverflow或Google上找不到任何明确的答案.这些可能之一或两者兼而有之吗?
如果可能,它是否可以与MessageContracts和DataContracts组合?
基于Microsoft它看起来不可能:
attributeFormDefault被忽略.
elementFormDefault必须是合格的.所有元素都必须符合DataContractSerializer支持的模式.这可以通过将xs:schema/@ elementFormDefault设置为"qualified"或通过在每个单独的元素声明上将xs:element/@ form设置为"qualified"来实现.
我也发现这不喜欢它:
通常,由于单个文档中混合使用全局和局部元素可能会产生混淆和奇怪的语法,以及默认命名空间可能出现的问题,因此建议坚持使用elementFormDefault ="qualified".它可能更冗长,但是哪个命名空间/模式拥有给定元素也更清楚.如果XML文档作者想要减少前缀的数量,他们总是可以依赖"默认命名空间".此外,由于使用默认命名空间时可能存在冲突,因此attributeFormDefault应该是不合格的.
我对WCF很新,只是有一个关于如何正确获取MessageContract继承的问题.我的设置的简化版本如下 - "基本"消息类型,然后是从其继承的另一个"测试"消息.
[MessageContract]
public abstract class BaseMessage
{ }
[MessageContract]
public class TestMessage : BaseMessage
{ }
Run Code Online (Sandbox Code Playgroud)
然后我在ServiceContract上有一个异步的OperationContract定义为:
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginFindRequest(BaseMessage request, AsyncCallback callback, object asyncState);
Run Code Online (Sandbox Code Playgroud)
我得到的问题是在调用BeginFindRequest方法并传入请求参数的TestMessage实例时,WCF框架将TestMessage实例反序列化为服务/服务器端的BaseMessage.由于这被定义为抽象类,因此会导致以下错误:
"消息无法反序列化为MessageContract类型的BaseMessage,因为它没有默认的(无参数)构造函数."
从我可以在MessageContract继承上找到的有限信息,它似乎应该工作.
所以我的问题是 - 为了让这个工作,我错过了什么; 或者我应该在ServiceContract上专门针对该类型定义一个单独的OperationContract - 缺点是我最终可能会有许多额外的OperationContracts?
所有,我在加载 WCF 服务时遇到以下错误。无法加载操作“GetEffort”,因为它具有 System.ServiceModel.Channels.Message 类型的参数或返回类型,或者具有 MessageContractAttribute 和其他不同类型参数的类型。使用 System.ServiceModel.Channels.Message 或带有 MessageContractAttribute 的类型时,该方法不得使用任何其他类型的参数。
这是服务合同
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "/GetEffort?date={date}&empoyeeID={empoyeeID}")]
Message GetEffort(DateTime date, int empoyeeID);
Run Code Online (Sandbox Code Playgroud)
我不知道发生了什么事。我看到类似的帖子说消息合同不匹配。但在这里我返回一个 Message 类型本身。
我有以下类型,可以在WCF中用作消息合同:
[MessageContract(IsWrapped = true,
WrapperNamespace = "http://example.com/services",
WrapperName = "EchoRequest")]
public class EchoRequest
{
public EchoRequest() { }
public EchoRequest(String value)
{
Value = value;
}
[MessageBodyMember(Name = "Value",
Namespace = "http://example.com/services",
Order = 0)]
public String Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我使用svcutil.exe生成此类型的代理时,我将获得一个客户端,该客户端能够与托管它的服务进行通信,并且元素上的XML名称空间根据Message Contract属性正确。
当我Message.CreateMessage(...)在其实例上使用时,名称空间将还原为默认名称(http://schemas.datacontract.org/2004/07 / ...)。当我使用的实例时DataContractSerializer,也会发生同样的事情。我尝试将名称空间传递给DataContractSerializer构造函数,并且只有包装器包含在名称空间中:
var requestMessage = new EchoRequest("hello, world!");
var serializer = new DataContractSerializer(typeof(EchoRequest),
"EchoRequest",
"http://example.com/services");
var stream = new MemoryStream();
serializer.WriteObject(stream, requestMessage); …Run Code Online (Sandbox Code Playgroud) 我正在尝试[MessageContract]针对现有的示例消息测试类,我正在寻找一种简化开发的方法,将示例消息文件读入我的[MessageContract]类的实例并查看它是如何工作的(我正在处理一个这里特别复杂的合同,非WCF原产地).
我的[MessageContract]班级看起来像这样:
[MessageContract(IsWrapped = true, WrapperName = "wrapper", WrapperNamespace = "somens")]
public class RequestMessage
{
[MessageHeader(Name = "HeaderElem", Namespace = "otherns")]
public XElement CorrelationTimeToLive { get; set; }
[MessageBodyMember(Name = "id", Namespace = "somens")]
public XElement id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我可以Message使用以下代码将文件读入类的实例:
var xr = XmlReader.Create("sample_message.xml");
var msg = Message.CreateMessage(xr, int.MaxValue, MessageVersion.Soap12);
Run Code Online (Sandbox Code Playgroud)
然而,这并不是特别有用,因为它根本不允许我测试我的[MessageContract]课程.
WCF的内容中的某个地方是将此Message实例转换为特定[MessageContract]类的实例的系统,但它是什么?
我收到了一位客户关于其 Web 服务客户端如何工作的规范。该规范是从服务发送和接收的实际 SOAP XML 消息以及相应的 XSD。客户希望我实现一个符合客户要求的网络服务。客户端是用 axis2 ws-stack 编写的,我想做的是在 WCF 中创建一个 Web 服务,它将接受客户端发出的请求并返回符合他们期望的 XML 的响应。在这个问题中,我将只发布与请求相关的 XML 和 XSD,因为如果我能让它工作,响应将以类似的方式做出。
我收到的 XML 如下:
POST /axis2/services/SampleService HTTP/1.1
Content-Type: text/xml; charset=UTF-8
SOAPAction: "sendCommand"
User-Agent: Axis2
Host: 127.0.0.1:7777
Content-Length: 347
<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<SendCommandRequest xmlns="http://something.org/">
<CMD>
<Station Address="ABC">
<Platform Address="DEF">
<Command>5</Command>
</Platform>
</Station>
</CMD>
</SendCommandRequest>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
相应的 XSD 如下所示:
<xsd:complexType name="SendCommandRequestType">
<xsd:sequence>
<xsd:element name="Station">
<xsd:complexType>
<xsd:attribute name="Address" type="xsd:string" use="required" />
<xsd:sequence>
<xsd:element minOccurs="0" maxOccurs="1" name="Platform">
<xsd:complexType>
<xsd:attribute name="Address" type="xsd:string" use="required" …Run Code Online (Sandbox Code Playgroud)