WCF客户端端点

时间:2013-01-31 12:08:32

标签: wcf workflow-foundation-4 endpoint

我有一个正常工作的WCF服务,我无法改变。但是在我的客户端(基于Workflow)我希望地址是动态的,所以我所做的是创建一个新的TestCustomEndpoint,如

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.Web;

namespace WorkflowService1
{
    public class TestEndpoints : ServiceEndpoint
    {
        public TestEndpoints(ContractDescription contract)
            : base(contract)
        {
            this.Binding = new NetTcpBinding();
            ResetBindingConfiguration(this.Binding);
            this.IsSystemEndpoint = false;
        }
        private void ResetBindingConfiguration(dynamic binding)
        {
            binding.SendTimeout = TimeSpan.FromMinutes(5);
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            binding.MaxBufferSize = Int32.MaxValue;
        }
    }

    public class TestEndpointsElementCollectionElement :
            StandardEndpointCollectionElement<TestEndpoints,
              TestEndpointsElement>
    {
    }

    public class TestEndpointsElement : StandardEndpointElement
    {
        protected override ServiceEndpoint CreateServiceEndpoint(ContractDescription contractDescription)
        {
            return new ServiceEndpoint(contractDescription);
        }

        protected override Type EndpointType
        {
            get { return typeof(TestEndpoints); }
        }

        protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ServiceEndpointElement serviceEndpointElement)
        {
            endpoint.Address = new System.ServiceModel.EndpointAddress("http://foo/bar/Automation.svc");
        }

        protected override void OnApplyConfiguration(ServiceEndpoint endpoint, ChannelEndpointElement channelEndpointElement)
        {
            endpoint.Address = new System.ServiceModel.EndpointAddress("http://foo/bar/Automation.svc");
        }

        protected override void OnInitializeAndValidate(ServiceEndpointElement serviceEndpointElement)
        {
        }

        protected override void OnInitializeAndValidate(ChannelEndpointElement channelEndpointElement)
        {
        }
    }
}

我的web.config看起来如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
    </appSettings>
    <system.web>
        <compilation debug="true" strict="false" explicit="true" targetFramework="4.5" >
        </compilation>
        <httpRuntime targetFramework="4.5"/>
    </system.web>
    <system.serviceModel>
        <extensions>
            <endpointExtensions>
                <add name="TestCustomEndpoint" type="WorkflowService1.TestEndpointsElementCollectionElement,WorkflowService1" />
            </endpointExtensions>
        </extensions>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IProjectConfigurationsService" />
                <binding name="BasicHttpBinding_IAutomation"/>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint
              binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAutomation" kind="TestCustomEndpoint"
              contract="IAutomation" name="BasicHttpBinding_IAutomation" />
        </client>
        <behaviors>
            <serviceBehaviors>
                <behavior>
                    <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
                    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
                    <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
                    <serviceDebug includeExceptionDetailInFaults="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>

</configuration>

但是当我使用其中一个活动调用工作流模板时,我收到以下错误。

Invalid endpoint type for endpoint extension configuration object.  This endpoint extension manages configuration of endpoint type 'WorkflowService1.TestEndpoints, WorkflowService1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and cannot act upon type 'System.ServiceModel.Description.ServiceEndpoint, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'." (System.ArgumentException)

我的问题是:是否可以覆盖端点类型以使用我的自定义端点而无需更改服务端点绑定。

0 个答案:

没有答案