【问题标题】:WCF change address at runtime exceptionWCF 在运行时更改地址异常
【发布时间】:2015-03-03 04:34:55
【问题描述】:

我在使用自定义端点连接到 WCF 服务时遇到问题。

我有通过 WCF 服务与数据库通信的 excel 插件。我创建了服务,一切正常。我的 Excel 插件项目的 ServiceReference 中引用了 WCF 服务。

但是现在,我想要一个选项来设置运行 WCF 服务的服务器。因此,我让用户输入服务 URI,并尝试使用绑定和 EndpointAdrress 创建 ServiceClient。 但是,如果我使用服务器的 EndPointAddress 创建 ServiceClient,即使服务器地址与来自 ServiceReference 的地址相同,也会引发 EndpointNotFoundException。

我认为app.config和web.config文件可能有问题,但我不知道在哪里。

App.Config(Excel 插件)

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_ICampaignService" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:58375/CampaignService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ICampaignService"
            contract="CampaignDatabaseService.ICampaignService" name="BasicHttpBinding_ICampaignService" />
    </client>
    <behaviors>
        <serviceBehaviors>
            <behavior name="">
                <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" />
</system.serviceModel>

Web.Config(WCF 服务)

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
<add name="CampaignDatabase" connectionString="Data Source=dev\sql14;Initial Catalog=TSQL2012;Integrated Security=True;User id=addin_test;Password=testpassword;"/>
  </connectionStrings>
  <appSettings>
     <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
   </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
   <system.serviceModel>
     <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="basicHttpBinding" scheme="http" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
   <modules runAllManagedModulesForAllRequests="true"/>
    <!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
     -->
  <directoryBrowse enabled="true"/>
 </system.webServer>

 </configuration>

这就是我创建 ServiceClient 的方式

 EndpointAddress serviceAddress = new EndpointAddress(Properties.Settings.Default.serviceAddress);
 BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
 CampaignServiceClient connector = new CampaignServiceClient(binding,serviceAddress);

感谢您的意见

【问题讨论】:

  • 请同时显示您用于动态绑定到新端点的实际代码。
  • 你想在你的开发机器或用户机器上运行它吗?如果服务与加载项在同一台机器上运行,则在 localhost 调用服务只会找到该服务。
  • 目前,我只在 Visual Studio 中调试此解决方案。但基本思想是,WCF 服务可以安装在不同的机器上,并且 Excel 插件中的用户可以键入服务运行的服务器地址。并且插件将使用这个地址连接到 WCF 服务。

标签: c# wcf vsto wcf-binding wcf-client


【解决方案1】:

如果我想在运行时更改地址,我将使用 channelFactory 而不是生成的 serviceClient。 因为从 web.config 中生成了一个 fetching thinks,这在运行时更改是不好的。

类似的东西。请更改为真正的设置。

//define binding 
BasicHttpBinding myBinding = new BasicHttpBinding();

//define endpoint url (where service is deployed)
EndpointAddress myEndpoint = new EndpointAddress("http://localhost:58375/CampaignService.svc"); //change to real endpoint 

//Use channel factory instead of generated one
ChannelFactory<ICampaignService> myChannelFactory = new ChannelFactory<ICampaignService>(myBinding, myEndpoint); //Change to you WCF interface
ICampaignService myServiceClient = myChannelFactory.CreateChannel();

//and call it            
var result = myServiceClient.Add(1,1); //input to your method
//other methods

((IClientChannel)myServiceClient).Close();
myChannelFactory.Close();

【讨论】:

  • 我在 Visual Studio 中调试时遇到了同样的错误。 localhost上运行的服务没有问题吗?
猜你喜欢
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多