问题
I have a problem connecting to WCF Service with custom endpoint.
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.
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.
I think there might be a problem in app.config and web.config files, but I dont know where.
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 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>
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);
Thank you for your opinions
回答1:
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();
来源:https://stackoverflow.com/questions/27782919/wcf-change-address-at-runtime-exception