简体   繁体   English

由ChannelFactory的接口调用时,如何从WCF工作流(WF4)返回字符串?

[英]How do I return a string from WCF Workflow (WF4) when invoked by a Interface with ChannelFactory?

If I have a workflow hosted (WF4 + WCF) and running in IIS implementing the following interface: 如果我托管(WF4 + WCF)并在IIS中运行的工作流,则实现以下接口:

[ServiceContract]
public interface IMessageService
{   
     [OperationContract]
     string SendMessage(string message);
}

(Using RecieveActivity and SendReply from System.ServiceModel.Activities ) (使用System.ServiceModel.Activities RecieveActivitySendReply

And invoke it like this: 并像这样调用它:

    var channel = new ChannelFactory<IMessageService>().CreateChannel(<init params>);
    string answer = channel.SendMessage("Testmessage");

Then answer is always null. 那么answer始终为空。 If I consume the workflow through the WcfTestClient I can see there is an returned xml object. 如果我通过WcfTestClient使用工作流,则可以看到有一个返回的xml对象。

How can I make the workflow return a string to populate answer ? 如何使工作流返回字符串以填充answer

(I'd like to avoid the "Add a ServiceReference and return a lot of xml-approach") (我想避免“添加ServiceReference并返回很多xml方法”)

You need a [return] attribute to specify how to find the result. 您需要一个[return]属性来指定如何查找结果。

In the SendReplyToReceive you can choose between sending back a Message or Parameters . SendReplyToReceive您可以选择发回MessageParameters In my experience, you need to choose Parameters but only send back one. 以我的经验,您需要选择“ Parameters但只返回一个。 Assuming you give the return parameter the name "result" then you need this attribute on your interface contract: 假设您给return参数指定名称“ result”,那么您在接口协定上需要此属性:

[return: MessageParameter(Name = "result")]

Here is a full example of one of mine; 这是我的一个完整例子。

namespace NewOrbit.ExVerifier.Model.Workflow.Case
{
    using System;
    using System.ServiceModel;

    using NewOrbit.ExVerifier.Model.Workflow;

    [ServiceContract(Namespace = "urn://exverifier.neworbit.co.uk/")]
    public interface ICaseWorkflow
    {
        [OperationContract(Action = "urn://exverifier.neworbit.co.uk/NewOrbit.ExVerifier.Model.Workflow.Case.ICaseWorkflow/Start",
            ReplyAction = "urn://exverifier.neworbit.co.uk/NewOrbit.ExVerifier.Model.Workflow.Case.ICaseWorkflow/StartReply")]
        [return: MessageParameter(Name = "result")]
        WorkflowInstanceIdentifier Start(int caseID);

        [OperationContract(Action = "urn://exverifier.neworbit.co.uk/NewOrbit.ExVerifier.Model.Workflow.Case.ICaseWorkflow/ApplicationStateChanged",
            ReplyAction = "urn://exverifier.neworbit.co.uk/NewOrbit.ExVerifier.Model.Workflow.Case.ICaseWorkflow/ApplicationStateChangedReply")]
        [return: MessageParameter(Name = "result")]
        bool ApplicationStateChanged(Guid instanceID, int applicationID);

        [OperationContract(Action = "urn://exverifier.neworbit.co.uk/NewOrbit.ExVerifier.Model.Workflow.Case.ICaseWorkflow/Cancel",
            ReplyAction = "urn://exverifier.neworbit.co.uk/NewOrbit.ExVerifier.Model.Workflow.Case.ICaseWorkflow/CancelReply")]
        [return: MessageParameter(Name = "result")]
        bool Cancel(Guid instanceID);
    }
}

Incidentally, in your example I am not sure how you are getting away with not specifying the OperationContract but great that you are - they are a real pain because the format you have to specify them in is different in the contract and in the workflow. 顺便说一句,在您的示例中,我不确定未指定OperationContract会怎样,但是您确实很满意-它们确实很痛苦,因为您必须在合同和工作流中指定的格式不同

Also, just in case you don't know and it can cause some really subtle bugs: The parameters you pass in are recognised by name, so it is imprtant that the name you specify for inbound parameters in your interface is the same as in your workflow. 另外,万一你不知道,它可能会导致一些非常微妙的错误:你传递的名字被认可的参数,所以它是imprtant这个名字你指定在接口的入站的参数是相同的工作流程。 Obvious when you think about it but can catch you out. 当您考虑时很明显,但是可以吸引您。 Oh, and avoid method names that are too long as they will break as well with misleading error messages. 哦,请避免使用太长的方法名称,因为它们会因误导性错误消息而中断。

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

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