我的WCF服务可以在任何服务器上运行。我的客户 - 是控制台应用程序。在命令行参数中,我想要设置我的WCF服务的地址。 在配置客户端中我当前有:
...
<spring>
<context>
<resource uri="assembly://MyAssembly.Console/MyAssembly.Console/ServerWeb.xml"/>
</context>
</spring>
...
<system.serviceModel>
<client>
<endpoint behaviorConfiguration="Default" name="serverWebDataServiceEndpoint" address="http://localhost/mydata/DataService.svc"
binding="basicHttpBinding" bindingConfiguration="basicHttpBinding1" contract="MyData.Contracts.IDataService"/>
</client>
...
文件ServerWeb.xml是:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:wcf="http://www.springframework.net/wcf">
<wcf:channelFactory id="serverWebDataService"
channelType="VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes.Contracts.IDataService, VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes"
endpointConfigurationName="serverWebDataServiceEndpoint" />
</objects>
在应用程序中,我使用下一个代码,用于呼叫服务的方法:
IApplicationContext _ctx = ContextRegistry.GetContext();
IDataService _dataService = _ctx["serverWebDataService"] as IDataService;
var rule = _dataService.GetRuleById(ruleId);
如何从命令行使用另一个WCF服务地址?
答案 0 :(得分:1)
尝试类似的东西:
<wcf:channelFactory id="serverWebDataService"
channelType="VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes.Contracts.IDataService, VimpelCom.Fmrad.Theseus.WcfDataLayer.CommonTypes"
endpointConfigurationName="serverWebDataServiceEndpoint">
<!-- You can use classic DI to configure the ChannelFactory<T> instance -->
<wcf:property name="Endpoint.Address">
<object type="System.ServiceModel.EndpointAddress, System.ServiceModel">
<constructor-arg name="uri" value"${serviceUrl}"/>
</object>
</wcf:property>
</wcf:channelFactory>
您可以使用IVariableSource抽象从命令行获取属性值。见: http://www.springframework.net/doc-latest/reference/html/objects.html#objects-variablesource
<object type="Spring.Objects.Factory.Config.VariablePlaceholderConfigurer, Spring.Core">
<property name="VariableSources">
<list>
<object type="Spring.Objects.Factory.Config.CommandLineArgsVariableSource, Spring.Core">
<property name="ArgumentPrefix" value="--" />
<property name="ValueSeparator" value="="/>
</object>
</list>
</property>
</object>
在命令行中设置变量,如下所示: program.exe --serviceUrl = http://localhost/Service.svc