我在使用自定义端点连接到WCF服务时遇到问题。
我有excel addin通过WCF服务与数据库通信。我创建了服务,一切正常。 WCF服务在我的Excel插件项目的ServiceReference中引用。
但是现在,我希望有一个选项来设置运行WCF服务的服务器。所以我让用户输入服务URI,我正在尝试使用绑定和EndpointAdrress创建ServiceClient。 但是,如果我使用服务器的EndPointAddress创建ServiceClient,则即使服务器地址与ServiceReference相同,也会引发EndpointNotFoundException。
我认为app.config和web.config文件可能存在问题,但我不知道在哪里。
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服务)
<?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);
感谢您的意见
答案 0 :(得分:0)
如果我想在运行时更改地址,我将使用channelFactory而不是生成的serviceClient。 Becouse从web.config生成一个提取思想,这在运行时更改是不好的。
像这样的东西。请更改为真实的设置。
//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();