当前位置 : 主页 > 网络推广 > seo >

Appfabric WF4-WCF服务,如何在没有httpcontext的情况下检索codeactivity中的当前url?

来源:互联网 收集:自由互联 发布时间:2021-06-16
我已经开发了一个带有代码活动的wf-wcf服务,并且我想要检索服务的当前URL. 如果我禁用appfabric的持久性功能,我可以使用检索URL HttpContext.Current.Request.Url.ToString() 如果启用了持久性功能
我已经开发了一个带有代码活动的wf-wcf服务,并且我想要检索服务的当前URL.
如果我禁用appfabric的持久性功能,我可以使用检索URL

HttpContext.Current.Request.Url.ToString()

如果启用了持久性功能,则httpcontext为null.

是否有不同的方法来检索承载我的代码活动的WCF的URL?

您需要实现 IReceiveMessageCallback并将其添加到活动的上下文中.在OnReceiveMessage中,您将传递当前的OperationContext,允许您检查传入的消息.

This WF4样本显示了如何执行此操作.

示例代码:

using System.Activities;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activities;

namespace DeclarativeServiceLibrary2
{
    public sealed class GetWCFMessageTo : NativeActivity
    {
        public Receive Receive { get; set; }

        public OutArgument<string> WcfTo { get; set; }

        protected override void Execute(NativeActivityContext context)
        {
            context.Properties.Add("ReceiveMessageCallback", new ReceiveMessageCallback());
            context.ScheduleActivity(Receive, CompletionCallback);
        }

        private void CompletionCallback(NativeActivityContext context, ActivityInstance completedInstance)
        {
            var receiveMessageCallback = context.Properties.Find("ReceiveMessageCallback") as ReceiveMessageCallback;
            WcfTo.Set(context, receiveMessageCallback.WcfRequestTo);
        }
    }

    [DataContract]
    class ReceiveMessageCallback : IReceiveMessageCallback
    {
        public string WcfRequestTo { get; private set; }

        public void OnReceiveMessage(OperationContext operationContext, ExecutionProperties activityExecutionProperties)
        {
            WcfRequestTo = operationContext.RequestContext.RequestMessage.Headers.To.ToString();
        }
    }
}

和示例工作流程XAMLX

<WorkflowService mc:Ignorable="sap" ConfigurationName="Service1" sap:VirtualizedContainerService.HintSize="307,306" Name="Service1" mva:VisualBasic.Settings="Assembly references and imported namespaces serialized as XML namespaces" xmlns="http://schemas.microsoft.com/netfx/2009/xaml/servicemodel" xmlns:d="clr-namespace:DeclarativeServiceLibrary2;assembly=DeclarativeServiceLibrary2" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="clr-namespace:Microsoft.VisualBasic;assembly=System" xmlns:mva="clr-namespace:Microsoft.VisualBasic.Activities;assembly=System.Activities" xmlns:p="http://tempuri.org/" xmlns:p1="http://schemas.microsoft.com/netfx/2009/xaml/activities" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:s1="clr-namespace:System;assembly=System" xmlns:s2="clr-namespace:System;assembly=System.Xml" xmlns:s3="clr-namespace:System;assembly=System.Core" xmlns:s4="clr-namespace:System;assembly=System.ServiceModel" xmlns:sa="clr-namespace:System.Activities;assembly=System.Activities" xmlns:sad="clr-namespace:System.Activities.Debugger;assembly=System.Activities" xmlns:sap="http://schemas.microsoft.com/netfx/2009/xaml/activities/presentation" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=System" xmlns:scg1="clr-namespace:System.Collections.Generic;assembly=System.ServiceModel" xmlns:scg2="clr-namespace:System.Collections.Generic;assembly=System.Core" xmlns:scg3="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:sd="clr-namespace:System.Data;assembly=System.Data" xmlns:sl="clr-namespace:System.Linq;assembly=System.Core" xmlns:st="clr-namespace:System.Text;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <p1:Sequence DisplayName="Sequential Service" sad:XamlDebuggerXmlReader.FileName="c:\temp\DeclarativeServiceLibrary2\DeclarativeServiceLibrary2\Service1.xamlx" sap:VirtualizedContainerService.HintSize="277,276" mva:VisualBasic.Settings="Assembly references and imported namespaces serialized as XML namespaces">
    <p1:Sequence.Variables>
      <p1:Variable x:TypeArguments="CorrelationHandle" Name="handle" />
      <p1:Variable x:TypeArguments="x:Int32" Name="data" />
      <p1:Variable x:TypeArguments="x:String" Name="WcfRequestTo" />
    </p1:Sequence.Variables>
    <sap:WorkflowViewStateService.ViewState>
      <scg3:Dictionary x:TypeArguments="x:String, x:Object">
        <x:Boolean x:Key="IsExpanded">True</x:Boolean>
      </scg3:Dictionary>
    </sap:WorkflowViewStateService.ViewState>
    <d:GetWCFMessageTo sap:VirtualizedContainerService.HintSize="255,22" WcfTo="[WcfRequestTo]">
      <d:GetWCFMessageTo.Receive>
        <Receive x:Name="__ReferenceID0" CanCreateInstance="True" DisplayName="ReceiveRequest" sap:VirtualizedContainerService.HintSize="255,90" OperationName="GetData" ServiceContractName="p:IService">
          <Receive.CorrelationInitializers>
            <RequestReplyCorrelationInitializer CorrelationHandle="[handle]" />
          </Receive.CorrelationInitializers>
          <ReceiveMessageContent>
            <p1:OutArgument x:TypeArguments="x:Int32">[data]</p1:OutArgument>
          </ReceiveMessageContent>
        </Receive>
      </d:GetWCFMessageTo.Receive>
    </d:GetWCFMessageTo>
    <SendReply Request="{x:Reference __ReferenceID0}" DisplayName="SendResponse" sap:VirtualizedContainerService.HintSize="255,90">
      <SendMessageContent>
        <p1:InArgument x:TypeArguments="x:String">["Received " &amp; data &amp; " WCF To header: " &amp; WcfRequestTo]</p1:InArgument>
      </SendMessageContent>
    </SendReply>
  </p1:Sequence>
</WorkflowService>
网友评论