WCF透明代理,但未在ChannelFactory上指定端点地址

时间:2016-09-23 14:14:42

标签: c# asp.net .net wcf proxy

我已经配置了WCF服务,并且我使用Channel Factory在另一个程序集中使用透明代理。我面临的问题是:在通道工厂的构造函数中,我必须手动指定要使用的服务的端点地址名称,何时应该通过使用服务的接口来查找地址。我在这做错了什么?

下面是代码:

服务配置:

<system.serviceModel>
    <services>
      <service name="WcfTestService.Service1" behaviorConfiguration= "ServiceBehavior">
        <endpoint address="" binding="wsHttpBinding" contract="Contracts.DataContract.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- 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.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_IService1" />
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/WCFTestService/Service1.svc" binding="wsHttpBinding"
        bindingConfiguration="WSHttpBinding_IService1" contract="Contracts.DataContract.IService1"
        name="WcfTestService.Service1">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

有效的代理,指定端点名称:

public class Service1Proxy : IService1
    {
        private IService1 service;

        public Service1Proxy() {
            service = new ChannelFactory<IService1>("WcfTestService.Service1").CreateChannel();
        }

        public string GetData(int value)
        {
            return service.GetData(value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            return service.GetDataUsingDataContract(composite);
        }
    }

抛出异常的代理:

public class Service1Proxy : IService1
    {
        private IService1 service;

        public Service1Proxy() {
            service = new ChannelFactory<IService1>().CreateChannel();
        }

        public string GetData(int value)
        {
            return service.GetData(value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            return service.GetDataUsingDataContract(composite);
        }
    }

使用最后一个代码块,通道工厂会抛出以下异常:

ChannelFactory.Endpoint上的Address属性为null。 ChannelFactory的端点必须具有指定的有效地址。

我认为它可以通过界面创建服务,但这似乎无法工作,我必须指定端点名称&#34; WcfTestService.Service1&#34;。一些同事说这是可能的,但没有人能告诉我如何。

我很感激任何见解。

0 个答案:

没有答案