简体   繁体   English

带有故障消息的WCF服务单向方法

[英]WCF service one-way method with fault message

I wanted to create a one-way method for wcf service. 我想为wcf服务创建一个单向方法。 Everything is ok unless I want to return fault message - if any. 一切都没问题,除非我想要返回错误信息 - 如果有的话。

I am using wscf blue code generator for wsdl and contract files. 我正在为wsdl和合同文件使用wscf蓝色代码生成器。

In wsdl operation element I have only 'Input' and 'Fault' child elements. 在wsdl操作元素中,我只有'Input'和'Fault'子元素。 When I run the service i got the information that cannot declare fault message in one-way communication and .. one of the solutions is to set one-way to false .. what I did and well it works ok. 当我运行服务时,我得到了无法在单向通信中声明故障消息的信息,并且其中一个解决方案是将单向设置为false ..我做了什么以及它工作正常。 If error occurs I get the message. 如果发生错误,我会收到消息。

However I am wondering if I broke the web service/wsdl creation rules ... Because from wsdl service schema generator creates for me one-way communication (set to 'true') and other in other programming languages' generators would probably do the same ... 但是我想知道我是否打破了web服务/ wsdl创建规则...因为从wsdl服务模式生成器为我创建单向通信(设置为'true')和其他编程语言中的其他生成器可能会做同样的事情...

So Is it correct what i Did? 所以我做了什么? How can I change wsdl so that the generator could create the code appropriately? 如何更改wsdl以便生成器可以正确创建代码?

If in theory I cannot use fault in one-way communication then Do I need extra structure on response to return any error? 如果理论上我不能在单向通信中使用故障那么我是否需要额外的结构响应以返回任何错误?

Thanks! 谢谢!

It's true that one-way operations can't return faults to clients. 确实, 单向操作无法向客户端返回故障 If you want to generate a void not one-way service operation in WCF you will have to add an empty output element for that operation in your WSDL contract. 如果要在WCF中生成void 而不是单向服务操作,则必须在WSDL协定中为该操作添加空输出元素

The .NET based WSDL parsers (such as WSCF.blue and svcutil ) will generate one-way service operations for <wsdl:operation> elements that don't have a corresponding <wsdl:output> element. 基于.NET的WSDL解析器(例如WSCF.bluesvcutil )将为<wsdl:operation>元素生成单向服务操作,这些元素没有相应的<wsdl:output>元素。

For example this WSDL operation is one-way : 例如,这个WSDL操作是单向的

<wsdl:definitions>
    <wsdl:portType>
        ... 
        <wsdl:operation name="Foo">
           <wsdl:input message="tns:FooRequest" />
        </wsdl:operation>
    </wsdl:portType>
</wsdl:definitions>

On the other hand, defining an operation with an empty <wsdl:output> element (ie an XSD type containing an empty sequence ) will be mapped to a void not one-way method in .NET, which can optionally contain a fault declaration: 另一方面,使用空<wsdl:output>元素(即包含空序列的XSD类型)定义操作将映射到.​​NET中的void 而不是单向方法,可以选择包含错误声明:

<wsdl:definitions>
    <wsdl:portType>
        ... 
        <wsdl:operation name="Foo">
           <wsdl:input message="tns:FooRequest" />
           <wsdl:output message="tns:FooResponse" />
           <wsdl:fault message="tns:FooFault" />
        </wsdl:operation>
    </wsdl:portType>
</wsdl:definitions>

where the <tns:FooResponse> type is simply defined as: 其中<tns:FooResponse>类型简单地定义为:

<xs:element name="FooResponse">
    <xs:complexType>
        <xs:sequence/>
    </xs:complexType>
</xs:element>

In WCF, one-way means operation has no returned values and the client does not care about the success or failure of the invocation. 在WCF中,单向意味着操作没有返回值,客户端不关心调用的成功或失败。

In WCF, we also got some other operations which are naturally one-way, such as you operate MSMQ via WCF, then the operations are normally set to one-way. 在WCF中,我们还有一些自然是单向的其他操作,例如您通过WCF操作MSMQ,然后操作通常设置为单向。

So, If you want to return something to indicate the invocation status, then one-way is not a correct choice, you can just use the normal wcf operation which is not one-way instead. 所以,如果你想返回一些东西来指示调用状态,那么单向不是一个正确的选择,你可以只使用非单向的普通wcf操作。

For one-way operation, here is an article for your reference WCF ONE-WAY 对于单向操作,这里有一篇文章供您参考WCF ONE-WAY

I feel you did correct thing. 我觉得你做对了。 For any type of client (C#, JAVA, C++, PHP) they just need WSDL to create the proxy. 对于任何类型的客户端(C#,JAVA,C ++,PHP),他们只需要WSDL来创建代理。 I feel it should work in other languages as well. 我觉得它也适用于其他语言。 Best to use SoapUI (Neutral Soap client) to test your service. 最好使用SoapUI(中性肥皂客户端)来测试您的服务。

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

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