WCF 运行时异常更改地址 [英] WCF change address at runtime exception

查看:25
本文介绍了WCF 运行时异常更改地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem connecting to WCF Service with custom endpoint.

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

I have excel addin that communicates with database via WCF Service. I created service and everything works fine. WCF Service is referenced in ServiceReference of my Excel addin project.

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

But now, I want to have an option to set server where WCF service is running. So I let user to input the service URI and I'm trying to create ServiceClient with binding and EndpointAdrress. But if I create ServiceClient with EndPointAddress of the server EndpointNotFoundException is raised even if the server address is same as from ServiceReference.

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

I think there might be a problem in app.config and web.config files, but I dont know where.

App.Config(Excel 插件)

App.Config (Excel Addin)

<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 服务)

Web.Config (WCF Service)

<?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 的方式

And this is how I create ServiceClient

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

谢谢您的意见

推荐答案

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

If I want to change address at runtime I will use channelFactory instead of generated serviceClient. Becouse generated one fetching thinks from web.config and this is not good to change at the runtime.

类似的东西.请更改为真实设置.

Something like this. Please change to the real one settings.

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

这篇关于WCF 运行时异常更改地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆