Appfabric WF4-WCF services, how to retrieve current url in codeactivity without httpcontext?

99封情书 提交于 2019-12-22 09:05:14

问题


I have developed a wf-wcf services with a code activity and in it I want to retrieve the current URL of the service. If I'm disabling the persistence feature of appfabric I can retrieve the URL using

HttpContext.Current.Request.Url.ToString()

If the persistence feature is enabled then the httpcontext is null.

Is there a different way to retrieve the URL of the WCF that hosts my code activity?


回答1:


You need to implment IReceiveMessageCallback and add that to an activity's context. In the OnReceiveMessage you get passed the current OperationContext allowing you to inspect the incoming message.

This WF4 samples shows how to do this.

Example code:

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();
        }
    }
}

and the sample workflow 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>


来源:https://stackoverflow.com/questions/2765072/appfabric-wf4-wcf-services-how-to-retrieve-current-url-in-codeactivity-without

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!